Left 4 Dead 2/Scripting/Script Functions/AddThinkToEnt: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (added '<br clear="all">' to fix text flow)
 
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
<h2>AddThinkToEnt()</h2>
Turns a function of an entity's script into a think function, which then the function automatically calls itself in an interval set by number returns if they exist, otherwise the <code>sv_script_think_interval</code> cvar is used (0.1 seconds by default). There can only be one think function for each entity at a time, so you can't chain think functions.


Turns a function of an entity's script into a think function, which is a function that constantly reruns again and again. By default, think functions attempt to run every 0.1 seconds, though the think function can configure its rethink rate by simplying returning a number. There can only be one think function for each entity at a time, so you can't chain think functions; You can however pass<code>null</code>as an argument under ''funcname'' to stop this entity from thinking.
To remove think functions, call this function with <code>null</code> as an argument under the ''funcname'' parameter. See this sub-section of the [[Entity_Scripts#Thinker_Functions|Entity Scripts]] page to know more about think functions.


See this sub-section of the [[Entity_Scripts#Thinker_Functions|Entity Scripts]] page to know more about think functions.
__TOC__


== Parameters ==
== Parameters ==
'''void AddThinkToEnt(CBaseEntity ''entity'',string ''invTable'')'''
'''void AddThinkToEnt(CBaseEntity ''entity'',string ''funcname'')'''
{| class="standard-table" style="width: 50%;"
{| class="standard-table" style="width: 50%;"
! Type
! Type
Line 18: Line 18:
| string
| string
| funcname
| funcname
| Given the string, find the function of the same name. Pass<code>null</code>to remove it.
| Given the string, find the function of the ''exact'' same name (no brackets allowed!). Pass<code>null</code>to remove it.
|}
|}


Line 24: Line 24:
=== Fastest Rethink Rate ===
=== Fastest Rethink Rate ===
[[File:L4D2ScriptFunctions_AddThinkToEnt-FastestRethinkRate.png|thumb|right|super|300px]]
[[File:L4D2ScriptFunctions_AddThinkToEnt-FastestRethinkRate.png|thumb|right|super|300px]]
When ran, the script will use [[List_of_L4D2_Script_Functions/ClientPrint|ClientPrint]] and constantly fill the chat with messages about how long the game has been running as fast as it can. When it sends the messages 30 times, the entity will then kill itself, stopping itself from thinking again.
When ran, an [[info_target]] will be spawned with a think function that automatically uses the fastest possible rethink rate (that's capped). The think function uses [[Left 4 Dead 2/Scripting/Script Functions/ClientPrint|ClientPrint()]] and fills the chatbox about how long the game has been running. When it has sent the messages 30 times, the entity will then delete itself, thus halting the think function.


Since {{l4d2}} runs on [[Source_Multiplayer_Networking#Servers_that_Support_Tickrate|30 ticks]] by default, think functions can never run faster than 0.03333... seconds. See this sub-section of the [[Entity_Scripts#Thinker_Functions|Entity Scripts]] page to know more about think functions.<br clear="all">
Since {{l4d2}} runs on [[Source_Multiplayer_Networking#Servers_that_Support_Tickrate|30 ticks]] by default, think functions can never run faster than 0.03333... seconds. See this sub-section of the [[Entity_Scripts#Thinker_Functions|Entity Scripts]] page to know more about think functions.
{{ExpandBox|
{{Expand|
<source lang=js>
<syntaxhighlight lang=js>
local timethinker = SpawnEntityFromTable("info_target", { targetname = "timethinker" } )
local timethinker = SpawnEntityFromTable("info_target", { targetname = "timethinker" } )
if( timethinker.ValidateScriptScope() )
if( timethinker.ValidateScriptScope() )
Line 40: Line 40:
ClientPrint(null, DirectorScript.HUD_PRINTTALK, "\x04"+"Current Time: "+"\x05"+Time().tostring())
ClientPrint(null, DirectorScript.HUD_PRINTTALK, "\x04"+"Current Time: "+"\x05"+Time().tostring())
timethinker.GetScriptScope()["PrintCount"]++
timethinker.GetScriptScope()["PrintCount"]++
return 0.01
return 0.01 // can't actually rethink this fast; quick way to ensure it rethinks at the rethink rate cap
}
}
else
else
Line 50: Line 50:
AddThinkToEnt(timethinker, "Think")
AddThinkToEnt(timethinker, "Think")
}
}
</source>}}
</syntaxhighlight>}}
 
=== Survivor Health Decay ===
=== Survivor Health Decay ===
[[File:L4D2ScriptFunctions_AddThinkToEnt-SurvivorHealthDecay.png|thumb|right|super|300px|Look at Rochelle, Ellis and Coach's health.]]
[[File:L4D2ScriptFunctions_AddThinkToEnt-SurvivorHealthDecay.png|thumb|right|super|300px|Look at Rochelle, Ellis and Coach's health.]]
This script applies a slow think function onto all players, which will then slowly decay their normal health, and turn it into temporary health.<br clear="all">
This script applies a slow think function onto all players, which will then slowly decay their normal health, and turn it into temporary health. It also implements a buffer system, a period where health decay attempts will not be allowed.
{{ExpandBox|
{{Expand|
<source lang=js>
<syntaxhighlight lang=js>
const HEALTH_DECAY_TIME = 2
const HEALTH_DECAY_TIME = 2
const HEALTH_DECAY_BUFFERCOUNT = 3
const HEALTH_DECAY_BUFFERCOUNT = 5
local player = null
local player = null
while( player = Entities.FindByClassname(player,"player") )
while( player = Entities.FindByClassname(player,"player") )
Line 99: Line 100:
printl("Applied health decaying to "+player)
printl("Applied health decaying to "+player)
}
}
</source>
</syntaxhighlight>
}}
}}
[[Category:Left 4 Dead 2]]
[[Category:Left 4 Dead 2]]
[[Category:Scripting]]
[[Category:Scripting]]

Latest revision as of 14:21, 15 September 2024

Turns a function of an entity's script into a think function, which then the function automatically calls itself in an interval set by number returns if they exist, otherwise the sv_script_think_interval cvar is used (0.1 seconds by default). There can only be one think function for each entity at a time, so you can't chain think functions.

To remove think functions, call this function with null as an argument under the funcname parameter. See this sub-section of the Entity Scripts page to know more about think functions.

Parameters

void AddThinkToEnt(CBaseEntity entity,string funcname)

Type Name Description
CBaseEntity entity Target entity to be applied a think function.
string funcname Given the string, find the function of the exact same name (no brackets allowed!). Passnullto remove it.

Code Samples

Fastest Rethink Rate

L4D2ScriptFunctions AddThinkToEnt-FastestRethinkRate.png

When ran, an info_target will be spawned with a think function that automatically uses the fastest possible rethink rate (that's capped). The think function uses ClientPrint() and fills the chatbox about how long the game has been running. When it has sent the messages 30 times, the entity will then delete itself, thus halting the think function.

Since Left 4 Dead 2 runs on 30 ticks by default, think functions can never run faster than 0.03333... seconds. See this sub-section of the Entity Scripts page to know more about think functions.

local timethinker = SpawnEntityFromTable("info_target", { targetname = "timethinker" } )
if( timethinker.ValidateScriptScope() )
{
	const MAX_PRINT_TIMES = 30
	timethinker.GetScriptScope()["PrintCount"] <- 0
	timethinker.GetScriptScope()["Think"] <- function()
	{
		if( timethinker.GetScriptScope()["PrintCount"] < MAX_PRINT_TIMES )
		{
			ClientPrint(null, DirectorScript.HUD_PRINTTALK, "\x04"+"Current Time: "+"\x05"+Time().tostring())
			timethinker.GetScriptScope()["PrintCount"]++
			return 0.01	// can't actually rethink this fast; quick way to ensure it rethinks at the rethink rate cap
		}
		else
		{
			ClientPrint(null, DirectorScript.HUD_PRINTTALK, "\x03"+"We printed enough times already.")
			self.Kill()
		}
	}
	AddThinkToEnt(timethinker, "Think")
}

Survivor Health Decay

Look at Rochelle, Ellis and Coach's health.

This script applies a slow think function onto all players, which will then slowly decay their normal health, and turn it into temporary health. It also implements a buffer system, a period where health decay attempts will not be allowed.

const HEALTH_DECAY_TIME = 2
const HEALTH_DECAY_BUFFERCOUNT = 5
local player = null
while( player = Entities.FindByClassname(player,"player") )
{
	// Team 4 is the L4D1 Passing Survivors; they're invincible!
	if( player.IsSurvivor() && NetProps.GetPropInt(player, "m_iTeamNum") != 4 )
	{
		if( player.ValidateScriptScope() )
		{
			local player_entityscript = player.GetScriptScope()
			player_entityscript["HealthDecayBufferCount"] <- HEALTH_DECAY_BUFFERCOUNT
			player_entityscript["HealthDecay"] <- function()
			{
				if( player_entityscript["HealthDecayBufferCount"] < 0 )
				{
					// Bots lose the think function when they die, anyways
					if( !self.IsIncapacitated() && !self.IsDead() && !self.IsHangingFromLedge() )
					{
						if( self.GetHealth() > 1 )
						{
							self.SetHealth( self.GetHealth()-1 )
							self.SetHealthBuffer( self.GetHealthBuffer()+1 )
							printl(GetCharacterDisplayName(self)+" is decaying health!")
						}
						else
							printl(GetCharacterDisplayName(self)+" isn't able to decay health anymore.")
					}
					else
						printl(GetCharacterDisplayName(self)+" is currently not allowed to decay health.")
				}
				else
				{
					player_entityscript["HealthDecayBufferCount"] -= 1
					printl(GetCharacterDisplayName(self)+" is in health decay buffer")
				}
				return HEALTH_DECAY_TIME
			}
		}
	}
	AddThinkToEnt(player, "HealthDecay")
	printl("Applied health decaying to "+player)
}