BrandonReese wrote:Would you be able to re-declare functions by reloading the file?
There are no "declarations" in Lua (except for the `local` keyword).
When you define a function like this:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
The compiler actually sees something like this:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
That is, you are assigning a
value of type "function" to the (global, in this case) variable `foo`. When you execute the first code snippet again, it's again assigning to global `foo`.
To get to what I think is your question, you
can change the values of variables holding functions (`foo` in the example) at runtime by simply assigning to them a new function value.
But the problem is how you use it in your code.
If you do something like this:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
minetest.register_whatever("foo", {
some_func = foo,
})
Then the field `some_func` in the definition holds the value of `foo` at that point, so if you later change the value at `foo` (by "reloading" or "redeclaring" the function), it won't get reflected in `some_func` unless you explicitly set it to the new value too.
If you do something like this:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
minetest.register_whatever("foo", {
some_func = function(...)
return foo(...)
end,
})
The function stored at `some_func` will read the value of `foo` at the point the function `some_func` is actually executed. So changing `foo` always reflects the change.
Tell me if something's not very clear and I'll try to explain it.
Edit: Also, it can't be stressed enough, but the
Lua Manual and
Programming in Lua are excellent resources.