Why isn't the timer callback function called? (SOLVED)

#1

Hello, I hope that everything is well.

I have run into some issues with the timer function:

if action_id == hash("key_x") then
	print("keyx")
	particlefx.play("/particle test#test")
	timer.delay(5, true, print("timer has run"))
end

This returns an error:
ERROR:SCRIPT: /test level/character/platypus script.script:215: bad argument #3 to ‘delay’ (function expected, got no value)
stack traceback:
[C]: in function ‘delay’

Any insight would be greatly appreciated!
Thanks,
V

0 Likes

#2

you need to create a local function and refer to that local function in argument #3 of timer.delay. likes this:


local function nowdothis(self)
print("timer has run")
end

if action_id == hash("key_x") then
	timer.delay(5, true, nowdothis)
end
4 Likes

#3

Or, equivalently,

timer.delay(5, true, function() print(“timer has run”) end)

2 Likes

#4

Ah, thank you I did not realise this!

0 Likes

#5

Thanks for the alternative method!

1 Like

#6

Ok, I ended up running into another issue…

if not canattack then
	timer.delay(10, false, function() canattack = true end)
	print("cant attack")
end

This code is set up to create an attack delay^^

if action.id == hash("key_z") and action.released and facingright and onground and canattack then
	print("attack recieved")
	print("attackright")
	local pos = go.get_position() + vmath.vector3(160, 30, 0) 
	local id = factory.create("#attack", pos)
	go.animate(id, "position.x", go.PLAYBACK_ONCE_FORWARD, pos.x + 1500, go.EASING_LINEAR, 0.8, 0, function()
		go.delete(id)
	end)
	canattack = false
end

This is the attack code^^

I did not get any errors but the gameplay is like this:

  • When I click “z” the character attacks.
  • 10 seconds pass until the character can attack again.
  • Once 10 seconds has passes the character is then able to attack for another 10 seconds rather than only being able to attack once, then the cycle repeating.

Any insight would be appreciated!
I may have explained my situation poorly, if you need more information please let me know.
Thanks,
V

0 Likes

#7

The problem is in your first piece of code:

if not canattack then
	timer.delay(10, false, function() canattack = true end)
	print("cant attack")
end

I assume this is in your update function. If it is, that means every frame where canattack is false it will create a new timer, resulting in a whole lot of timers. After 10 seconds, all of them start going off, setting canattack to true just about every frame until the last one expires after 10 seconds. Removing that block of code and changing the second one to this should fix it:

if action.id == hash("key_z") and action.released and facingright and onground and canattack then
	print("attack recieved")
	print("attackright")
	local pos = go.get_position() + vmath.vector3(160, 30, 0) 
	local id = factory.create("#attack", pos)
	go.animate(id, "position.x", go.PLAYBACK_ONCE_FORWARD, pos.x + 1500, go.EASING_LINEAR, 0.8, 0, function()
		go.delete(id)
	end)
	canattack = false
    timer.delay(10, false, function() canattack = true end)
    print("cant attack")
end
2 Likes

#8

Thanks very much!

Silly mistake on my part!:sweat_smile:

0 Likes