Defining Functions by String

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Defining Functions by String

Post by Helvecta »

Hey again! Sorry for all the questions, but I don't even know how I would search for this issue.

And I hardly even know how to describe the issue! Can anybody tell me why the following code fails and a solution to this?

Code: Select all

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)
"Bump." -CMFIend420
User avatar
paritybit
Citizen
Posts: 53
Joined: Thu Sep 06, 2012 3:52 am

Re: Defining Functions by String

Post by paritybit »

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.

When you write this:

Code: Select all

function menu:Open()
  test = 1
end
Lua interprets it this way:

Code: Select all

menu.Open = function(self) 
  test = 1
end
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: Defining Functions by String

Post by Helvecta »

B-But moving the function above love.update does nothing! Can you give a layman's example?

(Code now is:)

Code: Select all

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..?)
"Bump." -CMFIend420
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Defining Functions by String

Post by dreadkillz »

_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()?
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: Defining Functions by String

Post by Helvecta »

Ah, thank you! This worked like a charm. Did I mention how much i love this forum? :awesome:
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:

Code: Select all

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.
"Bump." -CMFIend420
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Defining Functions by String

Post by bartbes »

For that use I usually do something like:

Code: Select all

state = menu

-- further on
state:draw()
User avatar
paritybit
Citizen
Posts: 53
Joined: Thu Sep 06, 2012 3:52 am

Re: Defining Functions by String

Post by paritybit »

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.
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: Defining Functions by String

Post by Helvecta »

bartbes wrote:For that use I usually do something like:

Code: Select all

state = menu

-- further on
state:draw()
-- ...... This is even better! Why did I have to complicate things?! :|

And no big deal, Parity, it was like 4am, after all. Thanks for the assistance either way!

And to the rest of you, too: thanks!
"Bump." -CMFIend420
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 223 guests