Garry's Mod Wiki

Revision Difference

timer.Simple#565741

<function name="Simple" parent="timer" type="libraryfunc"> <description> Creates a simple timer that runs the given function after a specified delay. For a more advanced version that you can control after creation, see <page>timer.Create</page>. <warning>Timers use <page>Global.CurTime</page> internally. Due to this, they won't advance while the client is timing out from the server or on an empty dedicated server due to hibernation. (unless `sv_hibernate_think` is set to `1`).</warning> </description>⤶ ⤶ <warning>A previous message on this page stated that a delay of 0 would run the function on the next tick. This was partially an invalid assumption, and the true behavior is dependent on where `timer.Simple(0, func)` is called relative to `GarrysMod::Lua::Libraries::Timer::DoSimpleTimers`.⤶ ⤶ ⤶ ⤶ - If called *before* `DoSimpleTimers`, the callback will be executed on the same frame.⤶ ⤶ - If called *during* `DoSimpleTimers`, the callback will be executed on the same frame. **Note that calling timer.Simple(0, func) recursively (ie. a function that calls **`timer.simple(0, itself)`**) can lead to a hang!**⤶ ⤶ - If called *after* `DoSimpleTimers`, the callback will be executed on the next frame.⤶ ⤶ For more information on hook execution order, see <page>Lua Hooks Order</page>.⤶ ⤶ </warning>⤶ ⤶ </description>⤶ <realm>Shared and Menu</realm> <args> <arg name="delay" type="number">How long until the function should be ran (in seconds). Use `0` to have the function run in the next <page>GM:Tick</page>.</arg> <arg name="delay" type="number">How long until the function should be ran (in seconds). A value of `0` differs in behavior, depending on where you're calling this function.</arg> <arg name="func" type="function">The function to run after the specified delay.</arg> </args> </function> <example> <description>Print `Hello World` after 5 seconds.</description> <code>timer.Simple( 5, function() print( "Hello World" ) end )</code> <output> ``` Hello World ``` </output> </example> <example> <description>Spawns 5 zombies and creates a timer.Simple that removes them in 11, 12, 13, 14, and 15 seconds.</description> <code> for i = 1, 5 do local zombie = ents.Create( "npc_zombie" ) zombie:SetPos( Vector( i * 40, 0, 250 ) ) zombie:Spawn() timer.Simple( 10 + i, function() zombie:Remove() end ) end </code> <output> ``` -- 11 seconds into game 1st zombie disappears -- 12 seconds into game 2nd zombie disappears etc. ``` </output> </example>