CS:GO VScript Examples: Difference between revisions
Jump to navigation
Jump to search
m (Popcorn moved page CSGO Vscript Examples to CS:GO VScript Examples) |
(Added Equip players section.) |
||
Line 1: | Line 1: | ||
{{ | {{lang|CS:GO VScript Examples}} | ||
{{csgo}} The following are example [[VScript]]s for {{game name|csgo|icon=0|name=Counter-Strike: Global Offensive}}. | |||
}} | |||
{{csgo}} The following are example [[VScript]]s for | |||
===Find the closest player to a known entity=== | ===Find the closest player to a known entity=== | ||
<source lang=cpp> | <source lang=cpp> | ||
// find an entity named "button_01" and store its handle | // find an entity named "button_01" and store its handle (or null if not found) | ||
button <- Entities.FindByName( null, "button_01" ) | button <- Entities.FindByName( null, "button_01" ) | ||
// find the nearest player within 512 hammer units to the button's origin and store their handle | // find the nearest player within 512 hammer units to the button's origin and store their handle (or null if not found) | ||
player <- Entities.FindByClassnameNearest( "player", button.GetOrigin(), 512 ) | player <- Entities.FindByClassnameNearest( "player", button.GetOrigin(), 512 ) | ||
</source> | </source> | ||
Line 59: | Line 56: | ||
|} | |} | ||
=== | === Equip players === | ||
A slightly modified version from <code>csgo/scripts/vscripts/warmup/[[Setting up 1v1 Warmup Arenas|warmup_arena]].nut</code>. | |||
<source lang=cpp> | |||
function GiveGun( weapon, playerarray ) | |||
{ | |||
local equipper = Entities.CreateByClassname( "game_player_equip" ) | |||
// set flags and keyvalues | |||
equipper.__KeyValueFromInt( "spawnflags", 5 ) // "Use Only" and "Only Strip Same Weapon Type" | |||
equipper.__KeyValueFromInt( weapon, 0 ) | |||
// equipper.__KeyValueFromInt( "weapon_knife", 0 ) | |||
// equipper.__KeyValueFromInt( "item_kevlar", 0 ) | |||
equipper.ValidateScriptScope() | |||
for (player in playerarray) | |||
{ | |||
/ | EntFireByHandle( equipper, "Use", "", 0, player, null ) // each player "Use"s the equipper | ||
} | |||
EntFireByHandle( equipper, "Kill", "", 0.1, null, null ) // equipper is no longer needed, so remove it | |||
} | |||
</source> | |||
=== 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 <code>thinkfunction</code> keyvalue set to "Think". | |||
<source lang=cpp> | |||
// This can be found in hammer's sound picker | // This can be found in hammer's sound picker | ||
const EXPLOSION_SOUND = "c4.Explode" | const EXPLOSION_SOUND = "c4.Explode" | ||
Line 144: | Line 163: | ||
* [[VScript Fundamentals]] | * [[VScript Fundamentals]] | ||
* {{sq}} [[Squirrel]] | * {{sq}} [[Squirrel]] | ||
* [[List of Counter-Strike: Global Offensive Script Functions]] | * [[List of Counter-Strike: Global Offensive Script Functions|List of CS:GO Script Functions]] | ||
==External links== | ==External links== |
Revision as of 22:40, 19 November 2021
The following are example VScripts for Template:Game name.
Find the closest player to a known entity
// find an entity named "button_01" and store its handle (or null if not found)
button <- Entities.FindByName( null, "button_01" )
// find the nearest player within 512 hammer units to the button's origin and store their handle (or null if not found)
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"
}
|
Equip players
A slightly modified version from csgo/scripts/vscripts/warmup/warmup_arena.nut
.
function GiveGun( weapon, playerarray )
{
local equipper = Entities.CreateByClassname( "game_player_equip" )
// set flags and keyvalues
equipper.__KeyValueFromInt( "spawnflags", 5 ) // "Use Only" and "Only Strip Same Weapon Type"
equipper.__KeyValueFromInt( weapon, 0 )
// equipper.__KeyValueFromInt( "weapon_knife", 0 )
// equipper.__KeyValueFromInt( "item_kevlar", 0 )
equipper.ValidateScriptScope()
for (player in playerarray)
{
EntFireByHandle( equipper, "Use", "", 0, player, null ) // each player "Use"s the equipper
}
EntFireByHandle( equipper, "Kill", "", 0.1, null, null ) // equipper is no longer needed, so remove it
}
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
keyvalue 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
- Squirrel (programming language) - Wikipedia Article on Squirrel
Squirrel Reference Manual