View Single Post
Author Message
Chdata
Veteran Member
Join Date: Aug 2012
Location: Computer Chair, Illinois
Old 02-04-2015 , 23:57   [TUT/Snippet] Doing timed stuff without CreateTimers
Reply With Quote #1

hi i like frog

I feel like something similar may have been posted before or something, but here's a thing I set up for myself.

As opposed to using CreateTimer() to run code in the future, there are various scenarios where you could just use a single float to do things such as, preventing code from executing until a certain amount of time has passed.

For example, all of the "m_flNext..." variables Source uses work this way. TF2 has m_flNextPrimaryAttack. This is set to a time in seconds, which is compared to the GameTime, which can be retrieved in SM via GetGameTime().

m_flNextPrimaryAttack prevents a player from attacking with +attack until the GameTime has reached or passed whatever time is stored in m_flNextPrimaryAttack.

If m_flNextPrimaryAttack is 60.0, the GameTime must reach 60 seconds before the player can attack. If at this 60 second mark you then set m_fl to 95.0, the player has to wait another 35 seconds before attacking again.

One might use that netprop by setting its value to GetGameTime()+flTime.

It's a fairly simple comparison, and it has a lot of applications. So I made a simple enum to easily track these.

For our version, we'll use GetEngineTime(). GetGameTime() resets to 0.0 when the map changes, whereas the Engine Time will keep increasing forever from the point the server started up. (Seriously, the previous sentence isn't documented anywhere where you'll find it soon).

Stocks:
Spoiler


And here's an example of where I use it.
Note: This code is actually from Advanced Weaponiser which I imported to another plugin.
Spoiler

Another example:
Spoiler

And lastly:
Spoiler

All you need to do to add a new / separate "timer" is add another entry into one of the enums above. Simple.

The m_flNext naming convention doesn't matter and isn't meant to be meaningful.
I just did that cause there's no other place I'm going to name variables like that and it gives a similar feel to the netprops in Source.

You can also use this in things like OnGameFrame() or OnThink to have times faster than 100 milliseconds (0.1 seconds), depending on what you want to do. Of course then it's still restricted to however fast OnGameFrame/OnThinkHooks fire.
__________________

Last edited by Chdata; 04-16-2016 at 13:34.
Chdata is offline