Dota 2 Workshop Tools/Scripting/ThinkerFunctions: Difference between revisions
Jump to navigation
Jump to search
Warning:
(added a warning about game pauses) |
(→Examples: added a count down example) |
||
| Line 135: | Line 135: | ||
</tr> | </tr> | ||
</table> | </table> | ||
=== Example 3 === | |||
Count down from 25 when <code>Activate</code> is called while respecting pauses: | |||
<source lang="lua"> | |||
-- scripts/vscripts/addon_game_mode.lua | |||
COUNT_DOWN_FROM = 25 | |||
function round (num) | |||
return math.floor(num + 0.5) | |||
end | |||
function Activate () | |||
local endTime = round(GameRules:GetGameTime() + COUNT_DOWN_FROM) | |||
GameRules:GetGameModeEntity():SetThink(function () | |||
local delta = round(endTime - GameRules:GetGameTime()) | |||
if delta > 0 then | |||
print(tostring(delta)) | |||
return 1 | |||
end | |||
end) | |||
end | |||
</source> | |||
{{shortpagetitle}} | {{shortpagetitle}} | ||
[[Category:Dota 2 Workshop Tools]] | [[Category:Dota 2 Workshop Tools]] | ||
Revision as of 07:38, 15 September 2014
Template:Otherlang2
The SetThink function can be used to
- run a function every x seconds,
- schedule a function to be run x seconds from now.
Passing a function to SetThink is called "registering a think function". It is possible to have multiple think functions running on an entity at the same time, but this does require the think functions to be named so as not to overlap.
SetThink does not respect game pauses. You can use the Timer library by BMD to create think functions that respect game pauses.Signature
SetThink is a method on the CBaseEntity class. It can be used in two ways:
void SetThink(function thinkFn, string thinkName, float initialDelay)
thinkFn- The function that you want to run.
The other two arguments are optional:
thinkName- The name of the think slot. Use this if you want to register multiple thinkers on the same entity.
initialDelay- Delay in seconds before the thinker should start.
void SetThink(string thinkFnName, table context, string thinkName, float initialDelay)
thinkFnName- The name of the function you want to run.
context- A table/object. The function that will be registered is
context[thinkFnName]. In other words,SetThinkuses the first argument as an index into the second argument. thinkName,initialDelay- These are optional. Their descriptions are as above.
Your function should return a number or nil:
- Returning a number sets the next think time.
- Returning
nilwill unregister the thinker and stop it.
Examples
Example 1
Print a message to the console every 5 seconds:
| Class method | Ordinary function |
|---|---|
-- scripts/vscripts/addon_game_mode.lua
if CustomGameMode == nil then
CustomGameMode = class({})
end
function CustomGameMode:PrintHello ()
print("Hello world!")
return 5
end
function CustomGameMode:InitGameMode ()
GameRules:GetGameModeEntity():SetThink("PrintHello", self)
end
function Activate ()
GameRules.CustomAddon = CustomGameMode()
GameRules.CustomAddon:InitGameMode()
end
|
-- scripts/vscripts/addon_game_mode.lua
function PrintHello ()
print("Hello world!")
return 5
end
function Activate ()
GameRules:GetGameModeEntity():SetThink(PrintHello)
end
|
Example 2
Print a message to the console 15 seconds after Activate is called:
| Class method | Ordinary function |
|---|---|
-- scripts/vscripts/addon_game_mode.lua
if CustomGameMode == nil then
CustomGameMode = class({})
end
function CustomGameMode:PrintHello ()
print("Hello world!")
return nil
end
function CustomGameMode:InitGameMode ()
GameRules:GetGameModeEntity():SetThink("PrintHello", self, 15)
end
function Activate ()
GameRules.CustomAddon = CustomGameMode()
GameRules.CustomAddon:InitGameMode()
end
|
-- scripts/vscripts/addon_game_mode.lua
function PrintHello ()
print("Hello world!")
return nil
end
function Activate ()
GameRules:GetGameModeEntity():SetThink(PrintHello, 15)
end
|
Example 3
Count down from 25 when Activate is called while respecting pauses:
-- scripts/vscripts/addon_game_mode.lua
COUNT_DOWN_FROM = 25
function round (num)
return math.floor(num + 0.5)
end
function Activate ()
local endTime = round(GameRules:GetGameTime() + COUNT_DOWN_FROM)
GameRules:GetGameModeEntity():SetThink(function ()
local delta = round(endTime - GameRules:GetGameTime())
if delta > 0 then
print(tostring(delta))
return 1
end
end)
end