Difference between revisions of "CS:GO VScript Examples"

From Valve Developer Community
Jump to: navigation, search
 
(9 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{csgo}} The following are example [[VScript|vscripts]] for [[Counter-Strike: Global Offensive]].
+
{{DISPLAYTITLE:CS:GO VScript Examples}}
 +
{{otherlang2
 +
|ru=Примеры_скриптов_CSGO
 +
}}
 +
{{csgo}} The following are example [[VScript]]s for [[Counter-Strike: Global Offensive]].
  
 
===Find the closest player to a known entity===
 
===Find the closest player to a known entity===
  
<source lang=cpp>buttonReference <- Entities.FindByName( null, "button_01" ); // find the in-game entity "button_01" and store its targetname
+
<source lang=cpp>
player <- Entities.FindByClassnameNearest( "player", buttonReference.GetOrigin(), 512 ); // find the nearest player within 512 hammer units to the button's origin and create a variable.
+
// find an entity named "button_01" and store its handle
 +
button <- Entities.FindByName( null, "button_01" )
 +
 +
// find the nearest player within 512 hammer units to the button's origin and store their handle.
 +
player <- Entities.FindByClassnameNearest( "player", button.GetOrigin(), 512 )
 
</source>
 
</source>
  
{{todo}}
+
=== Functions to distinguish [[bot]]s and human [[player]]s ===
 +
 
 +
To test players other than the activator, give the functions a parameter that replaces <code>activator</code>.
 +
{|
 +
|- style="vertical-align:top;"
 +
| <source lang=cpp>
 +
// returns true if the activator is a bot
 +
function IsBot()
 +
{
 +
if (!activator) return false
 +
 +
local ent = null
 +
while ( ent = Entities.FindByClassname(ent, "cs_bot") )
 +
{
 +
if (ent == activator) return true
 +
}
 +
return false
 +
}
 +
</source>
 +
| <source lang=cpp>
 +
// returns true if the activator is a human player
 +
function IsHuman()
 +
{
 +
if (!activator) return false
 +
 +
local ent = null
 +
while ( ent = Entities.FindByClassname(ent, "player") )
 +
{
 +
if (ent == activator) return true
 +
}
 +
return false
 +
}
 +
</source>
 +
|- style="vertical-align:top;"
 +
|colspan="2"| <source lang=cpp>
 +
// returns true if the activator is a human player or a bot
 +
function IsPlayer()
 +
{
 +
// both bots and human players have the classname "player" in this case
 +
return activator && activator.GetClassname() == "player"
 +
}
 +
</source>
 +
|}
 +
 
 +
=== Turn decoys into nukes ===
 +
 
 +
<source lang=cpp>// The following script is a think script, it can be attached to any entity.
 +
// The Think function will be executed
 +
// as long as the entity is alive and has its thinkfunction set to Think
 +
 
 +
// This can be found in hammer's sound picker
 +
const EXPLOSION_SOUND = "c4.Explode"
 +
 
 +
// Called after the entity is spawned
 +
function Precache()
 +
{
 +
self.PrecacheScriptSound( EXPLOSION_SOUND )
 +
}
 +
 
 +
// Every 0.1 seconds this function checks for decoy_projectiles in the map
 +
// When found, it checks if the decoy is standing still
 +
// If true create an env_explosion, trigger it and kill the decoy
 +
 
 +
function Think()
 +
{
 +
local decoy = null
 +
while( ( decoy = Entities.FindByClassname( decoy, "decoy_projectile" ) ) != null )
 +
{
 +
if( decoy.GetVelocity().LengthSqr() == 0 )
 +
{
 +
local owner = decoy.GetOwner()
 +
local origin = decoy.GetOrigin()
 +
 
 +
local explosion = Entities.CreateByClassname( "env_explosion" )
 +
explosion.__KeyValueFromInt( "iMagnitude", 2000 )
 +
explosion.SetOrigin( origin )
 +
explosion.SetOwner( owner )
 +
 
 +
EntFireByHandle( explosion, "Explode", "", 0.1, owner, owner )
 +
DispatchParticleEffect( "explosion_c4_500", origin, origin )
 +
 
 +
decoy.EmitSound( EXPLOSION_SOUND )
 +
decoy.Destroy()
 +
}
 +
}
 +
}</source>
 +
 
 +
=== Create a timer to call a function independently ===
 +
 
 +
<source lang=cpp>
 +
timer <- null
 +
 
 +
function OnTimer()
 +
{
 +
print(".")
 +
}
 +
 
 +
// Called after the entity is spawned
 +
function OnPostSpawn()
 +
{
 +
if( timer == null )
 +
{
 +
timer = Entities.CreateByClassname( "logic_timer" )
 +
 
 +
// set refire time
 +
timer.__KeyValueFromFloat( "RefireTime", 0.1 )
 +
 
 +
timer.ValidateScriptScope()
 +
local scope = timer.GetScriptScope()
 +
 +
// add a reference to the function
 +
scope.OnTimer <- OnTimer
 +
 
 +
// connect the OnTimer output,
 +
// every time the timer fires the output, the function is executed
 +
timer.ConnectOutput( "OnTimer", "OnTimer" )
 +
 
 +
// start the timer
 +
EntFireByHandle( timer, "Enable", "", 0, null, null )
 +
}
 +
}
 +
 
 +
</source>
  
 
==See also==
 
==See also==
*[[List of Counter-Strike: Global Offensive Script Functions]]
+
* [[logic_script]]
*[[Logic_script|logic_script]]
+
* [[VScript]]
*[[VScript|vscript]]
+
* [[VScript Fundamentals]]
 +
* {{sq}} [[Squirrel]]
 +
* [[List of Counter-Strike: Global Offensive Script Functions]]
 +
 
 +
==External links==
 +
* [http://en.wikipedia.org/wiki/Squirrel_(programming_language) Squirrel (programming language)] - Wikipedia Article on Squirrel
 +
* {{sq}}[http://www.squirrel-lang.org/doc/squirrel2.html Squirrel Reference Manual]
 
[[Category:Counter-Strike:_Global_Offensive]]
 
[[Category:Counter-Strike:_Global_Offensive]]
 
[[Category:Scripting]]
 
[[Category:Scripting]]

Latest revision as of 17:22, 18 September 2021

Русский

Counter-Strike: Global Offensive The following are example VScripts for Counter-Strike: Global Offensive.

Find the closest player to a known entity

// find an entity named "button_01" and store its handle
button <- Entities.FindByName( null, "button_01" )
	
// find the nearest player within 512 hammer units to the button's origin and store their handle.
player <- Entities.FindByClassnameNearest( "player", button.GetOrigin(), 512 )

Functions to distinguish bots and human players

To test players other than the activator, give the functions a parameter that replaces activator.

// returns true if the activator is a bot
function IsBot()
{
	if (!activator) return false
	
	local ent = null
	while ( ent = Entities.FindByClassname(ent, "cs_bot") )
	{
		if (ent == activator) return true
	}
	return false
}
// returns true if the activator is a human player
function IsHuman()
{
	if (!activator) return false
	
	local ent = null
	while ( ent = Entities.FindByClassname(ent, "player") )
	{
		if (ent == activator) return true
	}
	return false
}
// returns true if the activator is a human player or a bot
function IsPlayer()
{
	// both bots and human players have the classname "player" in this case
	return activator && activator.GetClassname() == "player"
}

Turn decoys into nukes

// The following script is a think script, it can be attached to any entity.
// The Think function will be executed
// as long as the entity is alive and has its thinkfunction set to Think

// This can be found in hammer's sound picker
const EXPLOSION_SOUND = "c4.Explode"

// Called after the entity is spawned
function Precache()
{
	self.PrecacheScriptSound( EXPLOSION_SOUND )
}

// Every 0.1 seconds this function checks for decoy_projectiles in the map
// When found, it checks if the decoy is standing still
// If true create an env_explosion, trigger it and kill the decoy

function Think()
{
	local decoy = null
	while( ( decoy = Entities.FindByClassname( decoy, "decoy_projectile" ) ) != null )
	{		
		if( decoy.GetVelocity().LengthSqr() == 0 )
		{
			local owner = decoy.GetOwner()
			local origin = decoy.GetOrigin()

			local explosion = Entities.CreateByClassname( "env_explosion" )
			explosion.__KeyValueFromInt( "iMagnitude", 2000 )
			explosion.SetOrigin( origin )
			explosion.SetOwner( owner )	

			EntFireByHandle( explosion, "Explode", "", 0.1, owner, owner )
			DispatchParticleEffect( "explosion_c4_500", origin, origin )

			decoy.EmitSound( EXPLOSION_SOUND )
			decoy.Destroy()
		}		
	}
}

Create a timer to call a function independently

timer <- null

function OnTimer() 
{
	print(".")
}

// Called after the entity is spawned
function OnPostSpawn()
{
	if( timer == null )
	{
		timer = Entities.CreateByClassname( "logic_timer" )

		// set refire time
		timer.__KeyValueFromFloat( "RefireTime", 0.1 )

		timer.ValidateScriptScope()
		local scope = timer.GetScriptScope()
		
		// add a reference to the function
		scope.OnTimer <- OnTimer

		// connect the OnTimer output,
		// every time the timer fires the output, the function is executed
		timer.ConnectOutput( "OnTimer", "OnTimer" )

		// start the timer
		EntFireByHandle( timer, "Enable", "", 0, null, null )
	}
}

See also

External links