I have a power-up button that I want to appear every given seconds, stay for a while, and then disappear.
I created a repeating timer that makes the button appear and inside this timer another timer that triggers a fading gui scale animation.
The timer that triggers the animation only works one time. Every other time the first timer is repeated the inside timer is ignored and the animation is triggered immeadiately.
local position = vmath.vector3(480, 320, 0)
local size = vmath.vector3(200, 100, 0)
local button = gui.new_box_node(position, size)
gui.set_enabled(button, false)
timer.delay(4, true, function()
gui.set_enabled(button, true)
timer.delay(4, false, function() -- This timer only works the first time
gui.animate(button, "scale", vmath.vector3(0, 0, 0), gui.EASING_LINEAR, 2, 0, function()
gui.set_enabled(button, false)
gui.set_scale(button, vmath.vector3(1, 1, 1))
end)
end)
end)
Are you sure the inside timer doesn’t work? I think it should work.
I think the problem is that after the first 4 seconds the timers get in sync so they get triggered at the same time. So basically every 4 seconds you get both the callback of outside timer and the callback of the inside timer, no delay between them.
1 Like
The inside timer is not repeating. It only runs once. But it should be called again everytime the outside timer is repeated.
Actually it’s getting called again since it’s triggering the animation. But every time it’s getting called with 0 seconds delay. It only gets the 4 seconds it’s supposed to the first time it’s called.
The moment the outside timer creates the inside timer, the outside timer also restarts itself so both timers start at the same time and both timers have the same delay of 4 seconds therefore both timers end at the same time. This means the GUI gets enabled and starts scaling down at the same time. If you’ll change the delay of the inside timer to for example 1 second probably you will see a delay.
What I’m trying to say is that the inside timer triggers 4 seconds after you create it(so it works) but it triggers 0 seconds after the outside timer(so you don’t get the delay that you want).
1 Like
I tried that it still doesn’t work. What is happening is that the first time the inner timer is called it respects wathever delay I set. But every repeat after that it’s getting 0 delay time.
I did a test.
I used the following code:
timer.delay(4,true,function()
print("outside : "..socket.gettime())
timer.delay(4,false, function() print("inside : "..socket.gettime()) end)
end)
I got the following result:
DEBUG:SCRIPT: outside : 1774308915.8448
DEBUG:SCRIPT: outside : 1774308919.8432
DEBUG:SCRIPT: inside : 1774308919.8596
DEBUG:SCRIPT: outside : 1774308923.8424
DEBUG:SCRIPT: inside : 1774308923.8585
DEBUG:SCRIPT: outside : 1774308927.8411
DEBUG:SCRIPT: inside : 1774308927.8581
You can see that between every “outside” and it’s corresponding “inside”(matching colors) there is a 4 seconds delay.
2 Likes
I see what you mean. When the outside timer repeats it cancels the inner timer but not the animation. If I give 6 seconds to the outside and 2 to the inside it works as intended. But somehow I was getting this error with 90 seconds on the outside and 2 on the inside. I changed the seconds for this post. Anyway it’s working now.
The outside timer doesn’t cancel anything, it enables the GUI and starts the inner timer
What I mean is, there has to be a period of time between the moment the outside timer expires and the moment the inside timer expires but there isn’t because of the timer periods you used (4s and 4s in the example). So in this example the delay is 4-4 = 0.
3 Likes
Anyway my original code is working now. Thanks
1 Like