Parsing anonymous function as parameter

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Wiwiums
Prole
Posts: 9
Joined: Tue Aug 12, 2014 8:52 am

Parsing anonymous function as parameter

Post by Wiwiums »

So I have a problem with a function not being called after parsing it into a function.
Essentially I am making a Simple button interface for my project, and thought it'd be simple enough to give the button an action as an anonymous function specified at creation.

So essentially I have a button object, and the main love interface; in which I create the button, with its desired action, and on mouse click call button.action and it should call the function. Except it doesn't seem to work!

To test this I made a generic global variable, glob, and I passed a the function "function () glob = glob + 1".

The button class looks something like:

Button = {}
Function button(button-action)
Local self
...
Local Action = button-action
...
Function self.get-button-action() return action end
Return self
End

And I call something like
B = button.new(function () glob = glob + 1 end )

And on mouse press:
B.get-button-action()

However global does not change after that!
Is there something obvious I am missing or what would the problem be?
Unfortunately I'm already on holidays and away from my PC and I am typing this on my phone, however the question is pestering me and I can't not think about it :P
Sorry if there are some typos or whatever, and I wish you all a merry end of year celebration of your choice!
Thanks guys!
Pewpew
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Parsing anonymous function as parameter

Post by Azhukar »

Wiwiums wrote:Local self
I will need to see your code. Your example is useless and only hints at misunderstanding of the self variable with unlikely relation to your button behavior.
User avatar
rok
Prole
Posts: 36
Joined: Wed Dec 24, 2014 9:12 am
Location: Slovenia

Re: Parsing anonymous function as parameter

Post by rok »

I guess you'd like to do something like this:

Code: Select all

local button = {}

button.number = 0

function button.init(anon)
	button.action = anon
end

function button.act()
	button.action() -- run the action
end

-- do the work

button.init(function() print(tostring(button.number + 1)) end)

button.act()
The function which will later on be the action is passed through the initialization.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Parsing anonymous function as parameter

Post by s-ol »

From your weird listing above it looks like you would need to do

B.get-button-action()()

one () to call the function get-button-action, and one () to call what it returns - the action function.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Wiwiums
Prole
Posts: 9
Joined: Tue Aug 12, 2014 8:52 am

Re: Parsing anonymous function as parameter

Post by Wiwiums »

Ug yeah I guess this beach holiday is getting to my head; too much sunlight can't be good for you!
I'll actually paste the actual code when I get home, however I return in a day or so, so yeah.
Sorry about the completely unclear crap I wrote before :/
But thanks for the feed back so far anyway!
Pewpew
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Parsing anonymous function as parameter

Post by s-ol »

Wiwiums wrote:Ug yeah I guess this beach holiday is getting to my head; too much sunlight can't be good for you!
I'll actually paste the actual code when I get home, however I return in a day or so, so yeah.
Sorry about the completely unclear crap I wrote before :/
But thanks for the feed back so far anyway!
I'm fairly confident you just need two more characters.

But why have a method get-button-action? It just reads and returns a value.


And also I don't understand what all this has to do with parsing... Did you mean passing?

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Wiwiums
Prole
Posts: 9
Joined: Tue Aug 12, 2014 8:52 am

Re: Parsing anonymous function as parameter

Post by Wiwiums »

Oh god yeah I totally screwed that one up; I meant passing, not parsing. For some reason I had that in my head at the time of writing it and previously I had been parsing strings completely unrelated to this; I probably just need to stay away from computer things while on holidays.

I have a feeling my code originally acted the way rok described, however it was not working for whatever reason. It may have been a typo or something; and I think I also felt the need to add a return statement rather than just running that action.
My original idea was for the button action to create another button, which could not be performed from inside the button object. (or something like that)
Tomorrow I will just paste the original code lol. Make this easier for everyone and myself.
Anyway thanks for the help so far!
Pewpew
User avatar
Wiwiums
Prole
Posts: 9
Joined: Tue Aug 12, 2014 8:52 am

Re: Passing anonymous function as parameter

Post by Wiwiums »

Okay thanks to both S0lll0s and rok my problem was fixed!
I will post up the original ode if anyone is ever interested anyway (paraphrasing):

Code: Select all

button = {}
function button.new(act)
local self = {}
...
if act ~= nil then
  local action = act
else 
  --throw exception
end
...
function self.action()
  action()
end
...
function self.get_action()
  return action
end
return self
end
and in main:

Code: Select all

global = 0
b1 = button.new

function love.mousereleased(x,y,button)
  --if mouse is on button, etc then
  b1.get_action()()--thanks S0III0s
  --or simply b1.action()--thanks rok
end
In my own code i have a menu class that maintains menu items that contain the buttons, so the a single menu holds several buttons and all calls pass through that which would make things look more complicated here so i removed it for readability.

Anyway, problem solved, and I thank all you guys that commented.
All i really needed to add was those '()'!
which seems like a really obvious mistake to make :?
Cheers!
Pewpew
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests