menu = {}
test = 0
function love.update(dt)
_G["menu:Open"]() -- Made of pure evil.
--menu:Open() -- Works just fine!
end
function menu:Open()
test = 1
end
function love.draw()
love.graphics.print(test, 0, 0) -- If I *were* able to get to the function, this
-- would read "1".
end
EDIT: Because it might help,
main.lua:5: attempt to call field 'menu:Open' (a nil value)
When Lua reads your file, it reads from start to finish in order. When you are calling menu:Open (commented out) in your love.update function, it hasn't yet read the function menu:Open. There are two ways around this. You can declare the menu:Open variable above love.update, or you can move the entire function above love.update.
menu = {}
test = 0
function love.draw()
love.graphics.print(test, 0, 0) -- If I *were* able to get to the function, test
-- would read "1".
end
function menu:Open()
test = 1
end
function love.update(dt)
_G["menu:Open"]() -- Made of pure evil.
--menu:Open() -- Works just fine!
end
(To make this post a little more functional, could you also explain the benefit of these types of functions? They just seem better for organizing to me, but surely they have more purpose than that..?)
_G["menu:Open"]() is incorrect syntax. You want to do this: _G["menu"]:Open() . I'm not sure why you want to do this though. You already defined menu as a global variable. What's wrong with using menu:Open()?
Ah, thank you! This worked like a charm. Did I mention how much i love this forum?
dreadkillz wrote:What's wrong with using menu:Open()?
Big edit: Whoa I'm tired. Lemme try to answer that again. I'm using _G[]() instead of things like menu:Open because menu:Open can be shortened even further. I could use an if then else statement to call it depending on what state it is in:
if state == "normal" then
normal:draw()
elseif state == "menu" then
menu:draw()
elseif ....
end
but it just occurs to me that I can do "_G[state]:draw()" and just define state as what state the game is in and define a function named "menu:draw()", "normal:draw()" and whatnot. It makes it a little easier on the eyes, but other than that, neither is any better, I don't think.
I guess it was too late and I didn't understand what you were asking and just assumed you wanted to use "menu:Open()" but i t was failing for you. And then I did a really poorly executed test and gave you bad advice.