How to pass some parameters to gui.animate() complete function? (SOLVED)

I have this code:

gui.animate(node, gui.PROP_SCALE, scale, gui.EASING_LINEAR, duration, 0, do_something)

and:

local function do_something(self, node, count)
    -- some code
    if count == 10 then
    -- some code
    end
end

I would like to pass the parameter “count” to the do_something function, and this parameter has to be inherent to the instance to the node in use (so I can’t use self.count or global variables ecc). I have read an answer that suggests to write this:

gui.animate(node, gui.PROP_SCALE, scale, gui.EASING_LINEAR, duration, 0, function() 
    do_something(count) 
end)

but it doesn’t work for me: the count parameter has always value=NIL when I define the following:

local function do_something(self, node, count)
    -- some code
    if count == 10 then
    -- some code
    end
end

Thanks, Marco

You need to set the variable count in the scope of the animate call:

local count = 4711
gui.animate(node, gui.PROP_SCALE, scale, gui.EASING_LINEAR, duration, 0, function() 
    do_something(count) 
end)

Each function, named or anonymous, has access to all local variables in the scope it is defined in.

1 Like

I don’t understand; in the below code, the value printed is count=nil

local function do_something(self, nodo_lettera_volante, count)
	pprint(count)
end


function on_message(self, message_id, message, sender)
local count=123
if message_id == hash("go_fly_letter") then		
		gui.animate(nodo_lettera_volante, gui.PROP_POSITION, gui.get_position(nodo_lettera_volante), gui.EASING_NONE, 0.5, 0.0, function () 
			do_something(count) 
		end)
		
	end
end

If you are only interested in count, change the method signature to;

local function do_something(count)
    pprint(count)
end

or you can pass on all the args you are interested in from the callback;

local count = 123
gui.animate(node, gui.PROP_SCALE, scale, gui.EASING_LINEAR, duration, 0, function(self, node) 
    do_something(self, node, count) 
end)
1 Like

Thanks, I’ve understood.
Solved for me.

2 Likes