Page 1 of 1

Parsing anonymous function as parameter

Posted: Fri Dec 26, 2014 4:37 am
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!

Re: Parsing anonymous function as parameter

Posted: Fri Dec 26, 2014 1:58 pm
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.

Re: Parsing anonymous function as parameter

Posted: Fri Dec 26, 2014 2:53 pm
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.

Re: Parsing anonymous function as parameter

Posted: Fri Dec 26, 2014 3:02 pm
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.

Re: Parsing anonymous function as parameter

Posted: Fri Dec 26, 2014 3:30 pm
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!

Re: Parsing anonymous function as parameter

Posted: Fri Dec 26, 2014 5:50 pm
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?

Re: Parsing anonymous function as parameter

Posted: Sat Dec 27, 2014 2:01 am
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!

Re: Passing anonymous function as parameter

Posted: Sun Dec 28, 2014 10:41 am
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!