I want to do somthing like this
doThis()
function doThis()
print("hello world")
end
I want to do somthing like this
doThis()
function doThis()
print("hello world")
end
Why though. Just define it before. It doesn’t make sense for Lua(u) to call something that doesn’t exist yet.
I want to put all my functions at the bottom
--Main loop
while true do
print()
end
function print()
print("yes")
end
You’ll have to define them before. And since the loop is non-terminating, Lua(u) wouldn’t even get to defining the function in the first place.
Functions should always go at the top of the script, just below constants/variables.
Functions use the variable environment at time of calling, not time of definition.
function foo()
print(bar)
end
local bar = "hello"
foo() --> hello
Luau or any most programming languages for that matter can’t use something that doesn’t exist.
is it possible to do something like this in lua
But there are quite a few languages that initialize all functions before running anything.
Forward declaration? Yes, but it won’t have the same effect.
local meow()
function main()
meow()
end
-- calling `main` here will error
function meow()
print("meow")
end
-- calling `main` here will work.
But in this case that is unnecessary. The variable will exist whenever you call main
, but if you call main
before the meow
at the bottom of the script, it will error since meow
will still be nil. To my knowledge this is also true in C, but I’m likely wrong.
Hey, I have been looking for the solution for this issue too, but couldn’t find really anything helpful, but I managed to predefine it. If anyone is also looking how to do it, here is how I did it:
local meow = function() end
local main = function()
for i=0,3,1 do
meow()
end
end
meow = function()
print("meow")
end
main()
Sadly we can’t do like local function meow()
as we already defined the function, so at least there is a way.
local table = {}
table.foo()
function table.foo()
end
???
local func
function main()
func()
end
func = function()
print("hi")
end
main() --prints hi
Lua is dynamic, the func identifier can be initially set to anything (nil in this case), and changed later. When the main function is declared, it doesn’t capture the value of func. Instead it captures the identifier, that is, it remembers the variable named func. It doesn’t matter if at that moment, func is still nil. Afterwards, func is set to refer to a function, so when main() is called, it looks up the identifier called “func”, see that it now refers to a function, and calls that function.
If you are familiar with C, identifiers in lua are kind of like an “any-pointer” or “any-reference”. When the local func identifier is created, think of a pointer being created, with the thing its pointing at initially being nil. The main function remembers the address of this pointer, and remembers that its job is to go to the thing its pointing at and attempt to call it later. Afterwards, the func identifier is set to point to an anonymous function, and when main() is called, it goes looks up where func points to, goes to that function, and calls it. If func had not been set to a function and had remained as nil, when main() is called, it would see that func points to nil, realize that it can’t call a nil value, and throw an error.