ULX

Author Topic: Lua Timers.  (Read 3564 times)

0 Members and 1 Guest are viewing this topic.

Offline LuaTenshi

  • Hero Member
  • *****
  • Posts: 545
  • Karma: 47
  • Just your ordinary moon angel!
    • Mirai.Red
Lua Timers.
« on: May 13, 2012, 07:47:12 PM »
[link]

 ::) Thats my code.

No matter what I do I cant seem to get the timers working, I have used simple timers, but as soon as they run out they just keep executing the command without waiting ever again.

The Timers that I have there do not return any errors to the server, they just don't work. Every thing works fine except the timers. Please help?

I would like this answered ASAP.
« Last Edit: May 13, 2012, 07:49:24 PM by HeLLFox_15 »
I cry every time I see that I am not a respected member of this community.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Lua Timers.
« Reply #1 on: May 13, 2012, 08:42:04 PM »
I'm not sure of any below point which might break timers. My experience with them is limited.
-You're using think functions to run the timer every frame (barring if statements)
-You're running the stop function within the timer. (Perhaps it doesn't work well this way)
-You're running the timer forever (0 reps = forever) (Why use timer rep forever AND in think statement running it every frame (barring if boolean)).
-Though the old timer.create example shows using the function() statement within a timer.create, perhaps due to locality of function, its not grabbing variables within it?
- ... all i can think of for now
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Lua Timers.
« Reply #2 on: May 13, 2012, 09:49:55 PM »
I believe you're resetting the timer every frame since you're creating it inside a think function.
Experiencing God's grace one day at a time.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Lua Timers.
« Reply #3 on: May 13, 2012, 09:59:54 PM »
-You're using think functions to run the timer every frame (barring if statements)

- You're resetting the timer every frame (Like Megiddo said)

There, fixed my original point. :P
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline LuaTenshi

  • Hero Member
  • *****
  • Posts: 545
  • Karma: 47
  • Just your ordinary moon angel!
    • Mirai.Red
Re: Lua Timers.
« Reply #4 on: May 16, 2012, 06:08:34 PM »
Yea, I realized that so I fixed it and now it works.

I have been able to pin point where the lag is coming from...

Code: [Select]
timer.Create( "WaterExtinguish", 0.5, 0, function()
    for _, ent in ipairs( ents.GetAll() ) do
        if ( ent:IsValid() and ent:IsOnFire() and ent:WaterLevel() > 0 ) then
            ent:Extinguish()
        end
    end
end)

Oh and don't worry its not connected to the think hook. :P
I cry every time I see that I am not a respected member of this community.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Lua Timers.
« Reply #5 on: May 16, 2012, 08:05:40 PM »
You're only calling that once, right?
Since it runs repeatedly, you'd only need once.

GetAll is a heavy lookup, especially on the server every half second.
You "might" squeeze some more time out of it by not using GetAll, and instead have a table updated when players join/quit/die/spawn whatever, but, that would only be me guessing, and, runs risk of missing some error condition.
(Imagine old badly written prop protects... "This item belongs to ..." and "..." left hours ago)

Even with that, you're looping a table, a somewhat costly endeavor.

My experience level really doesn't know much more to offer.

"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming