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
Sorry if there are some typos or whatever, and I wish you all a merry end of year celebration of your choice!
Thanks guys!
Parsing anonymous function as parameter
Re: Parsing anonymous function as parameter
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.Wiwiums wrote:Local self
Re: Parsing anonymous function as parameter
I guess you'd like to do something like this:
The function which will later on be the action is passed through the initialization.
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()
Re: Parsing anonymous function as parameter
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.
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
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'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
Re: Parsing anonymous function as parameter
I'm fairly confident you just need two more characters.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!
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
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!
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
Re: Passing anonymous function as parameter
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):
and in main:
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!
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
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
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
Who is online
Users browsing this forum: Google [Bot] and 1 guest