Not understanding the timer manual?

I’ve messed around with the timer.delay() function as i would like for a subroutine to wait for 3 seconds and then update a variable. The reason i’m doing this is to give my player a small period of invincibility so that the player can only lose 1 life every 3 seconds.

looking at the manual i think i need to use the image function.

would i be correct in saying that this will automatically create a timer without any extra code required.
Also i understand the delay will be the number of seconds in the delay (for me this will be 3), repeat will be set to false because i only want to run this timer when the subroutine is called, But what is the callback for?

below is how i thought the code should be implimented but it must be incorrect because i’m getting an error becasue delay is set to a nil value?

local function update_damage(self, handle, time_elapsed)
	self.enemy_damage = false
end


local function player_damage(self, normal, distance)
	self.enemy_contact = true
	msg.post("player#player_hit", "play_sound")
	msg.post("/health#health", "remove_life")
	timer.delay(3, false, update_damage(self, handle, time_elapsed))
end

I’ve tried a few different things and looked at online videos but can’t seem to understand how it works. Can anyone tell me how the timer.delay() function works?

1 Like

That’s not how callbacks work.
The update_damage(self, handle, time_elapsed) is a function call, and will return a value.

An alternative would be to do:

timer.delay(3, false, function (self, handle, time_elapsed)
        print("hello")
        self.enemy_damage = false
    end)
5 Likes

You just used a function call as Mathias said, instead of passing a function :wink: You can pass a variable name holding a function in Lua, as I imagine you tried, so only pass variable name:

timer.delay(3, false, update_damage)

Instead of using the variable name with () which means you will call it (update_damage() is a call)

The reason you can just pass variable as a callback is that Lua treats functions as first class citizens - so exactly like this - you can pass variable “containing” a whole function. As this 3rd parameter in ‘timer.delay’ is a callback and expects such function - you suspect it is called inside.

This is so powerful feature of Lua, it’s worth learning :wink: If you are familiar with some other language, e.g. C++ you can check out how lambdas are used as callback functions. Mathias shown an example with anonymous callback (defined in a moment of passing to function call with function() end), here is also a proposition with named callback, similar to yours:

local my_callback = function(param1)
    print(param1
end
local some_function_like_timer_delay(param, callback)
     callback(param)
end

some_function_like_timer_delay("Pawel", my_callback)

Here a call to ‘some_functuon_like_timer_delay’ will print “Pawel”, because it calls given callback with given param As first argument.

This is powerful, but also cheeky! :sweat_smile:

You notice the fu action responsible for calling the callback is eventually passing parameters to the function call. When you check out timer.delay it’s callback is described as a function with parameters ‘self, handle, time_elapsed’ and its responsibility of ‘timer.delay’ to pass this parameters to the callback call.

You, as a user of ‘timer.delay’ need only to provide an anonymous function (‘function() end’) or a “named function” or in other words a variable that is “containing such function” as a callback - that eventually take those parameters and uses them in their body.

4 Likes