• Random Timer
    10 replies, posted
Hello, I've been making this jumping entity and I want it to jump at different intervals instead of a constant interval. For example, there is a 3 second delay to each jump, so it would be like jump, wait 3 seconds, jump, wait 3 seconds... I want it to be different like jump, wait 2 seconds, jump, wait 5 seconds, jump, wait 4 seconds. I tried the following code ,but it doesn't work. [CODE]timer.Create( "jump_timer"..self:EntIndex(), i, 0, function() phys:SetVelocity(self.Entity:GetRight() * -100 + Vector(0,0,100)) end) local i = math.random(2,5)[/CODE]
Don't you have to define 'I' before you run the timer? [LUA] local i = math.random(2,5) timer.Create( "jump_timer"..self:EntIndex(), i, 0, function() phys:SetVelocity(self.Entity:GetRight() * -100 + Vector(0,0,100)) end) [/lua]
[QUOTE=ben434;41016249]Don't you have to define 'I' before you run the timer? [LUA] local i = math.random(2,5) timer.Create( "jump_timer"..self:EntIndex(), i, 0, function() phys:SetVelocity(self.Entity:GetRight() * -100 + Vector(0,0,100)) end) [/lua][/QUOTE] I did define I in the following code local i = math.random(2,5)
You defined it after you try to use it.
[QUOTE=FreeTacos.exe;41016267]I did define I in the following code local i = math.random(2,5)[/QUOTE] I know, but you have to put it before the timer. Not after.
[QUOTE=Agent766;41016304]You defined it after you try to use it.[/QUOTE] Thanks, but I am still not getting what im looking for. I am getting the same random interval. For example local i = math.random(2,5) jump, wait 3 seconds, jump, wait 3 seconds, jump, wait 3 seconds, ect. I want it to be like jump,wait 2 seconds, jump,wait 5 seconds, jump,wait 3 seconds, jump,wait 4 seconds, jump,
You need to have the timer delete itself and then remake it on every tick as there is no method to make a timer have dynamic increments. [lua] local i = math.random(2,5) local function JumpTimerResponse() i = math.random(2,5) phys:SetVelocity(self.Entity:GetRight() * -100 + Vector(0,0,100)) timer.Create( "jump_timer" .. self:EntIndex(),i, 1 ); end timer.Create( "jump_timer" .. self:EntIndex(), math.random(2,5), 1 ); [/lua] Something like that.
[lua]function ENT:Initialize() self.nextJump = CurTime() + math.random(2, 5); -- CurTime() is the time the script was ran at in seconds. Adding an amount to it basically means: *Amount* seconds from now end; function ENT:AttemptJump() if CurTime() >= self.nextJump then local phys = self:GetPhysicsObject(); if IsValid(phys) then phys:SetVelocity(self:GetRight() * -100 + Vector(0,0,100)); self.nextJump = CurTime() + math.random(2, 5); end; end; end; function ENT:Think() self:AttemptJump(); end;[/lua] Personally I hate using timers into entities. I don't know if it's just me, but my timers tend to screw up if they are working in a "complex" way. (Also, this method has the benefit to be able to get how long left until the jump occurs again) Example: [lua]function ENT:HowLongUntilJump() return math.Round(self.nextJump - CurTime(), 1); -- Rounds it to only one decimal digit, otherwise there might be a lot. end;[/lua]
nextJump should be initialized in your entity's Initialize function, otherwise you might as well initialize it to 0. Other than that, your code is the right way to do it. I only dislike this part: [lua];[/lua] The only time this should be used is when you want to separate two instructions on the same line. Or as a replacement for commas in table declarations. A lot of beginners tend to read code like that and then believe that semicolons are an obligation.
[QUOTE=_Kilburn;41018726]nextJump should be initialized in your entity's Initialize function, otherwise you might as well initialize it to 0. Other than that, your code is the right way to do it. I only dislike this part: [lua];[/lua] The only time this should be used is when you want to separate two instructions on the same line. Or as a replacement for commas in table declarations. A lot of beginners tend to read code like that and then believe that semicolons are an obligation.[/QUOTE] I was thinking about putting it into initialize, but I was afraid the Think function would start running before Initialize for some reason. Either way, it has always worked for me the way I do it. (Although I'll edit the post putting it into initialize for good practice)
Thank you ~Razor~. It works!
Sorry, you need to Log In to post a reply to this thread.