Left 4 Dead 2/Scripting/Expanded Mutation System/Appendix: Game Mode using Game Events

From Valve Developer Community
Jump to: navigation, search

Using a Game Event

In this example we are going to use the game event generated when a Boomer is killed to make the survivors laugh.

First, create a new mutation titled laughtrack. If you do not know how to make a new mutation definition check out the Creating A Simple Mutation tutorial and look under the heading Adding the Mutation Definition.

Next, create a new script file in the vscripts folder titled laughtrack.nut

The first thing we need to do is get the name of the game event we wish to act upon. By playing the game with display_game_events 1 set we can see that the game event boomer_exploded occurs when a Boomer pops. Alternatively, you can look through the game event .res file to find the game event name.

Now that we know the game event boomer_exploded occurs when a Boomer pops we know that our function must be called OnGameEvent_boomer_exploded( params )

Add the function to your script:

//=========================================================
// Called when a boomer explodes.  
// According to the modevents.res file boomer_exploded Params will contain:
//	"userid"        "short"   	// Boomer that exploded
//	"attacker"	"short"		// player who caused the explosion
//	"splashedbile"	"bool"		// Exploding boomer splashed bile on Survivors
//=========================================================
function OnGameEvent_boomer_exploded( params )
{
	// so funny!
	Laugh()
}


Next, implement the Laugh() function:

function Laugh()
{
	local playerEnt = null
	while ( playerEnt = Entities.FindByClassname( playerEnt, "player" ) )
	{
		if (playerEnt.IsSurvivor() )
		{
			// fire entity IO at "!activator" and pass the player ent as the activator
			EntFire( "!activator", "SpeakResponseConcept", "PlayerLaugh", 0, playerEnt )
		}
	}
}


Time to test! Fire up the game and load any coop map. Be sure to load with the Laughtrack mutation enabled (e.g., from the console type "map c5m2_park laughtrack" to load c5m2_park with the laughtrack mutation) Find a boomer (or type z_spawn boomer in the console to create one) and verify that your function gets called when the boomer explodes.