Difference between revisions of "CSGO Vscript Examples"

From Valve Developer Community
Jump to: navigation, search
m (Added syntax highlighting for the previous 2 examples)
Line 1: Line 1:
 +
{{otherlang2
 +
|ru=Примеры_скриптов_CSGO
 +
}}
 
{{csgo}} The following are example [[VScript|vscripts]] for [[Counter-Strike: Global Offensive]].
 
{{csgo}} The following are example [[VScript|vscripts]] for [[Counter-Strike: Global Offensive]].
  

Revision as of 08:35, 5 January 2019

Русский

<Counter-Strike: Global Offensive> The following are example vscripts for Counter-Strike: Global Offensive.

Find the closest player to a known entity

buttonReference <- Entities.FindByName( null, "button_01" ); // find the in-game entity "button_01" and store its targetname
	
player <- Entities.FindByClassnameNearest( "player", buttonReference.GetOrigin(), 512 ); // find the nearest player within 512 hammer units to the button's origin and create a variable.

Turn Decoys into Nukes

//The following script needs to be attached to something like a logic_script,
//but any other entity would work as the script will function regardless,
//as long as the entity is alive and the think function is set to Think()

EXPLOSION_SOUND <- "c4.Explode"; //This can be found in hammer's sound picker

function Precache() //If this is defined it runs automatically when the script is created
{
	self.PrecacheScriptSound( EXPLOSION_SOUND );
}

//Every 0.1 seconds this checks if any decoy_projectiles exist on the map
//When found it compares the velocity of the projectile with a 0,0,0 vector
//If true the decoy stopped moving so we can run the rest of the script.
//An env_explosion is created and configured, it's moved to the decoy's
//position and then triggered. The decoy is then killed.

function Think()
{
	ent <- null;
	while ((ent = Entities.FindByClassname(ent, "decoy_projectile")) != null)
	{		
		if(ent.GetVelocity().Length() == Vector(0,0,0).Length())
		{		
			owner <- ent.GetOwner();
			origin <- ent.GetOrigin();	
			
			exp <- Entities.CreateByClassname("env_explosion");
			exp.__KeyValueFromInt("iMagnitude", 2000);
					
			ent.EmitSound(EXPLOSION_SOUND);
									
			exp.SetOrigin(origin);
			exp.SetOwner(owner);
									
			EntFireByHandle(exp, "Explode", "", 0.1, owner,owner);						
			ent.Destroy();						
			DispatchParticleEffect("explosion_c4_500", origin, origin);
		}		
	}
}

Create a Timer to Fire Off an Assigned Think Function Independently

thinkTimer <- null;

//name <- function() has to be used here instead of function Think() 
//when making a function to put into a different scope as a variable.
TimerThink <- function() 
{
	//put your think function here
	//since this will be used the timer's scope self will be the timer handle.
	//imagine as if this function is in a separate script that's attached to the timer.
	
	print("Tick - ");
}

function OnPostSpawn()
{
	if(thinkTimer == null)
	{
		thinkTimer = Entities.CreateByClassname("logic_timer");		//create timer
		thinkTimer.ValidateScriptScope(); 							//ensure it's scope is created 
		local timerScope = thinkTimer.GetScriptScope(); 			//get the scope
		
		timerScope.Think <- TimerThink; 							//function Think in the timer scope is now a copy of TimerThink from this script scope
		EntFireByHandle(thinkTimer, "AddOutput", "OnTimer !self:RunScriptCode:Think():0:-1", 0, null, null); 
																	//ent_fire convention you can use in console or in hammer 
																	//ent_fire name Addoutput "OutputName Target:Input:Delay:RepeatTimes"
		
		thinkTimer.__KeyValueFromFloat("RefireTime", 0.1); 			//set the frequency of the timer. You get RefireTime when disabling smartedit in hammer.
		EntFireByHandle(thinkTimer, "Enable", "", 0, null, null); 	//finally, start the timer
	}
}

Attach a Think Function to an Entity at Runtime Using a Timer

thinkTimer <- null;

function OnPostSpawn()
{
	//Creates the timer and sets it up to run the function Think in this script.
	if(thinkTimer == null)
	{
		thinkTimer = Entities.CreateByClassname("logic_timer");			//create timer
		thinkTimer.ValidateScriptScope(); 								//ensure it's scope is created 
		local timerScope = thinkTimer.GetScriptScope(); 				//get the scope											
									
		timerScope.mainScriptScope <- self.GetScriptScope();			//save the scope of this script to a variable in timer's scope
		timerScope.DoThink <- function() { mainScriptScope.Think() };	//create a DoThink function in the timer's scope to call the Think in this script
		
		thinkTimer.ConnectOutput("OnTimer", "DoThink"); 				//Run DoThink on every timer tick
		
		thinkTimer.__KeyValueFromFloat("RefireTime", 0.1); 				//set the frequency of the timer. You get RefireTime when disabling smartedit in hammer.
		EntFireByHandle(thinkTimer, "Enable", "", 0, null, null); 		//finally, start the timer
	}
}

//Think function the timer will run
function Think()
{
	print("Tick - ");
}

To do

See also

External links