aintaro Posted June 22, 2014 Posted June 22, 2014 (edited) Hello, How can I stop a timer within a timer, here is an example to make it clear : function test() local timer = 10 local theTimer = setTimer(function() if (timer > 0) then timer = timer - 1 else --KILLTIMER HERE (already tried killTimer within the function, did not work) end end, 1000, 0) end Edited June 22, 2014 by Guest
myonlake Posted June 22, 2014 Posted June 22, 2014 A timer within a timer? There's just one timer there, so just kill that. Make sure you pass theTimer as an argument to the timer function or your script won't be able to kill it.
aintaro Posted June 22, 2014 Author Posted June 22, 2014 When you kill a timer within it's function it does not work.
ADCX Posted June 22, 2014 Posted June 22, 2014 Variable theTimer is local to the function test, so you cannot use it inside the function within the timer. Pass it with a timer and then use it.
cheez3d Posted June 23, 2014 Posted June 23, 2014 local Timer = nil; Timer = setTimer(function() killTimer(Timer); end,1000,1);
aintaro Posted June 23, 2014 Author Posted June 23, 2014 local Timer = nil; Timer = setTimer(function() killTimer(Timer); end,1000,1); thanks man, this is what I needed
Moderators Citizen Posted June 23, 2014 Moderators Posted June 23, 2014 aintaro said: Cheez3D said: local Timer = nil; Timer = setTimer(function() killTimer(Timer); end,1000,1); thanks man, this is what I needed This is dirty !!! + it will produce bugs if you are on the server side (since the exact same global will be used by every players and possibly at the exact same time for 2 of them). This is what you should do (as myonlake sugested): function test() local timer = 10 local theTimer = setTimer(function( selfTimer ) -- receive theTimer as selfTimer if (timer > 0) then timer = timer - 1 else killTimer(selfTimer) end end, 1000, 0, theTimer) --give theTimer as function parameter end
cheez3d Posted June 24, 2014 Posted June 24, 2014 Citizen said: This is dirty !!! + it will produce bugs if you are on the server side (since the exact same global will be used by every players and possibly at the exact same time for 2 of them). This is what you should do (as myonlake sugested): function test() local timer = 10 local theTimer = setTimer(function( selfTimer ) -- receive theTimer as selfTimer if (timer > 0) then timer = timer - 1 else killTimer(selfTimer) end end, 1000, 0, theTimer) --give theTimer as function parameter end That is not going to work unless you declare the Timer variable one line before. local Timer = nil; Timer = setTimer(function(self) outputChatBox(self); --> userdata; end,1000,1,Timer); local Timer = setTimer(function(self) outputChatBox(self); --> nil; end,1000,1,Timer);
Moderators IIYAMA Posted June 24, 2014 Moderators Posted June 24, 2014 That is not going to work unless you declare the Timer variable one line before. local Timer = nil; Timer = setTimer(function(self) outputChatBox(self); --> userdata; end,1000,1,Timer); local Timer = setTimer(function(self) outputChatBox(self); --> nil; end,1000,1,Timer); Yup/(correct), The variable "Timer" received it's value after the setTimer function has returned it's result. Which means the code inside of the function has been loaded before the returning and the Timer variable doesn't exist till then.
Moderators Citizen Posted June 24, 2014 Moderators Posted June 24, 2014 Oh yeah ofc. I'm a bit slow these days :facepalm: ^^
aintaro Posted June 24, 2014 Author Posted June 24, 2014 (edited) Code not working Edited June 24, 2014 by Guest
aintaro Posted June 24, 2014 Author Posted June 24, 2014 Cheez3D said: Citizen said: This is dirty !!! + it will produce bugs if you are on the server side (since the exact same global will be used by every players and possibly at the exact same time for 2 of them). This is what you should do (as myonlake sugested): function test() local timer = 10 local theTimer = setTimer(function( selfTimer ) -- receive theTimer as selfTimer if (timer > 0) then timer = timer - 1 else killTimer(selfTimer) end end, 1000, 0, theTimer) --give theTimer as function parameter end That is not going to work unless you declare the Timer variable one line before. local Timer = nil; Timer = setTimer(function(self) outputChatBox(self); --> userdata; end,1000,1,Timer); local Timer = setTimer(function(self) outputChatBox(self); --> nil; end,1000,1,Timer); Dude, did you even test your code? it's not working at all. stop posting code that you didn't even bother to test. Thanks
ADCX Posted June 24, 2014 Posted June 24, 2014 This is actually impossible to do, because the passed variable will always be the one before the timer declaration. You should just put the function outside the timer.
cheez3d Posted June 25, 2014 Posted June 25, 2014 This is actually impossible to do, because the passed variable will always be the one before the timer declaration. You should just put the function outside the timer. It's not impossible. You can do it as I posted.
Moderators IIYAMA Posted June 25, 2014 Moderators Posted June 25, 2014 Well it Works, only the outputChatBox doesn't work. Userdata's must be convert to a string before they can be showed in the chat.
Moderators IIYAMA Posted June 25, 2014 Moderators Posted June 25, 2014 They are actually auto-converted. Like in print(). Not for me, I get warnings with userdata's which aren't converted with tostring,
Moderators Citizen Posted June 25, 2014 Moderators Posted June 25, 2014 Dude, did you even test your code? it's not working at all. stop posting code that you didn't even bother to test.Thanks Hehehe, I don't test my code 90% of the time because my code works at first try 9 times out of 10. So thanks for the advice but I'll just ignore it. Good luck with your problems in the future.
ADCX Posted June 25, 2014 Posted June 25, 2014 Why not just do this, instead of all of these complications. function MyFunction() setTimer(function() -- Your code here end,1000,10) end
aintaro Posted June 25, 2014 Author Posted June 25, 2014 Why not just do this, instead of all of these complications. function MyFunction() setTimer(function() -- Your code here end,1000,10) end let's say you want a certain value to be higher than 0 to keep executing the the timer something like this : setTimer(function() if (health > 0 and health < 50) then --execute code here else -- kill timer here end end, 1000, 0) Somethimes you need to kill a timer within it's function as you do not know how many times the timer has to tick
myonlake Posted June 25, 2014 Posted June 25, 2014 What are you using the script for even - if you're using it server-side and it's used individually for specific players, you should know that you'd potentially have up to 4096 simulatenous timers running at the same time each second. Can you explain what exactly are you doing that timer for? And could it be moved to client-side, as that would be efficient for the server performance. Besides you can just use getTickCount( ) client-side.
ADCX Posted June 25, 2014 Posted June 25, 2014 local Timer = nil Timer = setTimer(function(myTimer) if (health > 0 and health < 50) then --execute code here else killTimer(myTimer) end end, 1000, 0,Timer) Try this, but I'm not sure if it'll work, because it doesn't say if setTimer uses live variable, or just what that variable represented for that time.
arezu Posted June 25, 2014 Posted June 25, 2014 Dude, did you even test your code? it's not working at all. stop posting code that you didn't even bother to test. Thanks Did you even test it? I tested it and it works. Here's my test (ran server-side): local timer = nil timer = setTimer(function() outputDebugString("update!") killTimer(timer) end, 1000, 0) Closures in lua uses reference to outside variables, and if the scope ends before closure is called, then the variable is saved until the closures have been "free'd" from memory.. I guess that is kinda how closures work in lua. As a test, I made a script like this: local timer = nil local count = 0 timer = setTimer(function() outputDebugString(tostring(count)) count = 2 end, 1000, 1) count = 1 setTimer(function() outputDebugString("second timer: "..tostring(count)) count = 3 end, 3000, 1) The output was: 1 second timer: 2
aintaro Posted June 25, 2014 Author Posted June 25, 2014 Citizen said: aintaro said: Cheez3D said: local Timer = nil; Timer = setTimer(function() killTimer(Timer); end,1000,1); thanks man, this is what I needed This is dirty !!! + it will produce bugs if you are on the server side (since the exact same global will be used by every players and possibly at the exact same time for 2 of them). This is what you should do (as myonlake sugested): function test() local timer = 10 local theTimer = setTimer(function( selfTimer ) -- receive theTimer as selfTimer if (timer > 0) then timer = timer - 1 else killTimer(selfTimer) end end, 1000, 0, theTimer) --give theTimer as function parameter end local Timer = nil; Timer = setTimer(function() killTimer(Timer); end,1000,1); this is not dirty at all, Whenever the function gets called, local Timer gets a new slot in the memory, so it's never the same timer, even if multiple players execute the function. So please test code before you comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now