Triggers

umairazfar edited this page Nov 9, 2014 · 5 revisions
Clone this wiki locally

Triggers are events that trigger when a certain condition has been met. You might have come across them previously, so this tutorial takes a step forward and explains them with an example. When the map will start, we will have 2 rifle infantry attacking our base. Killing them both will Trigger an event that will call a transport helicopter and it will drop off a sniper and a general (You can however change what the helicopter will drop, I will explain).

Changing the map

Since we are using the same map as was in the previous tutorial, we go to the map directory and open map.yaml

Previously we had defined the IndiaRally point as this:

IndiaRally: waypoint
	Location: 31,27
	Owner: Neutral

What we will do is add two actors to the left and right of this rally point. so simply add these two actors below all the defined actors:

Attacker1: e1
	Location: 30,27
	Owner: INDIA
Attacker2: e1
	Location: 32,27
	Owner: INDIA

See the x-coordinate values and the Owner. If you are making this for RA mod, simply replace INDIA with soviet if you want to play as allies and vice versa. We name the two attackers as Attacker1 and Attacker2.

Close this file and open your tutorialmap.lua file.

Defining actors

We first define the actors that will take part in our little trigger

AttackersTeam = { Attacker1, Attacker2 }
InsertionHelicopterType = "tran"
SniperReinforcements = { "gnrl", "sniper" }

So AttackersTeam is the team that once we kill, the Insertion Helicopter, InsertionHelicopterType will appear, bringing the reinforcements, given here as SniperReinforcements. If you are making the map for RA mod, simply change gnrl with e7. It will give you Tanya.

** The triggers

Since we want the helicopter to appear when the team is killed, we add this line at the end of the WorldLoaded function:

Trigger.OnAllKilled(AttackersTeam, AttackersKilled)

This trigger says that if all members of the team, AttackersTeam are killed, call the function, AttackersKilled. But we have not defined that function, so we do just that. Define the following function in your code:

AttackersKilled = function()
	Trigger.AfterDelay(DateTime.Seconds(5), SendSnipers)
end

In this function we add the second trigger which calls the function, SendSnipers after 5 seconds. SendSnipers function will create the helicopter which will land at a rally point and deploy our reinforcements.

SendSnipers = function()
	local passengers = Reinforcements.ReinforceWithTransport(player, InsertionHelicopterType,
		SniperReinforcements, JeepReinforcementsPath, { PakEntry.Location })[2]	
	local vip = passengers[1]
	Trigger.OnKilled(vip, MissionFailed)
	vip.Stance = "Defend"
end

In the first line, we declare the variable that will hold all the passengers that are being transported. If you look at the line, you will understand that the reinforcements are for the player, they are being brought by a helicopter, they are the sniper reinforcements that we defined on top. The helicopter will follow the same path that the Jeeps took previously and it will go out of the map at the same location from where it entered.

the curious [2] in the end signifies that this function will be returning the passengers in the helicopter and giving them to the variable, passengers. If I change the [2] to [1], it will then return the helicopter itself which we do not want to do here.

In the next line we make the gnrl to be the vip, because we want the mission to fail if the general is killed. this is evident from the next line where a trigger is called if the vip is killed. Finally we set the stance of the vip to defend as soon as he is deployed so that he can get right to business. So that's about it. I will put the entire code here for your convenience if you missed something:

Complete code

JeepReinforcements = {"jeep", "jeep"}
JeepReinforcementsPath = { PakEntry.Location, PakRally.Location }
JeepDelay = 12
JeepInterval = 2


Force = { "e1", "e1", "e1", "e1", "e2"}
ForcePath = { IndiaEntry.Location, IndiaRally.Location }
ForceDelay = 15
ForceInterval = 2

ParadropUnitTypes = { "e1", "e1", "e1", "e3", "e3"}
ParadropWaypoints = { PakRally, IndiaRally}

AttackersTeam = { Attacker1, Attacker2 }
InsertionHelicopterType = "tran"
SniperReinforcements = { "gnrl", "sniper" }

AttackersKilled = function()
	Trigger.AfterDelay(DateTime.Seconds(5), SendSnipers)
end

SendSnipers = function()
	local passengers = Reinforcements.ReinforceWithTransport(player, InsertionHelicopterType,
		SniperReinforcements, JeepReinforcementsPath, { PakEntry.Location })[2]	
	local vip = passengers[1]
	Trigger.OnKilled(vip, MissionFailed)
	vip.Stance = "Defend"
end

SendJeeps = function()
	Reinforcements.Reinforce(player, JeepReinforcements, JeepReinforcementsPath, DateTime.Seconds(JeepInterval))
	Media.PlaySpeechNotification(player, "ReinforcementsArrived")
end

SendIndianInfantry = function()
	local units = Reinforcements.Reinforce(india, Force, ForcePath, ForceInterval)
	Utils.Do(units, function(unit)
		BindActorTriggers(unit)
	end)
	Trigger.AfterDelay(DateTime.Seconds(5), SendIndianInfantry)
end

BindActorTriggers = function(a)
	if a.HasProperty("Hunt") then
		if a.Owner == india then
			Trigger.OnIdle(a, a.Hunt)
		else
			Trigger.OnIdle(a, function(a) a.AttackMove(Outpost.Location) end)
		end
	end
end

OutpostDestroyed = function()
	MissionFailed()
end

MissionAccomplished = function()
	Media.PlaySpeechNotification(player, "Win")
end

MissionFailed = function()
	Media.PlaySpeechNotification(player, "Lose")
	player.MarkFailedObjective(SurviveObjective)
	india.MarkCompletedObjective(DestroyObjective)
end

ParadropIndianUnits = function()
	local lz = Utils.Random(ParadropWaypoints).Location
	local start = Map.CenterOfCell(Map.RandomEdgeCell()) + WVec.New(0, 0, Actor.CruiseAltitude("badr"))
	local transport = Actor.Create("badr", true, { CenterPosition = start, Owner = india, Facing = (Map.CenterOfCell(lz) - start).Facing })

	Utils.Do(ParadropUnitTypes, function(type)
		local a = Actor.Create(type, false, { Owner = india })
		BindActorTriggers(a)
		transport.LoadPassenger(a)
	end)

	transport.Paradrop(lz)
	Trigger.AfterDelay(DateTime.Seconds(15), ParadropIndianUnits)
end



WorldLoaded = function()
	player = Player.GetPlayer("PAKISTAN")
	india = Player.GetPlayer("INDIA")
	
	Trigger.OnObjectiveCompleted(player, function(p, id)
		Media.DisplayMessage(p.GetObjectiveDescription(id), "Objective completed")
	end)

	Trigger.OnObjectiveFailed(player, function(p, id)
		Media.DisplayMessage(p.GetObjectiveDescription(id), "Objective failed")
	end)
	
	Trigger.OnKilled(Outpost, OutpostDestroyed)
	
	SurviveObjective = player.AddPrimaryObjective("The outpost must survive.")
	DestroyObjective = india.AddPrimaryObjective("The Pakistani outpost must be destroyed.")
	
	Trigger.AfterDelay(DateTime.Seconds(60), function()
		MissionAccomplished()
		player.MarkCompletedObjective(SurviveObjective)
		india.MarkFailedObjective(DestroyObjective)
	end)
	Trigger.AfterDelay(DateTime.Seconds(2), function() Actor.Create("camera", true, { Owner = player, Location = PakRally.Location }) end)
	Camera.Position = Outpost.CenterPosition
	Trigger.AfterDelay(DateTime.Seconds(5), SendJeeps) --Sending Allied reinforcements	
	SendIndianInfantry(IndiaEntry.Location, Force, ForceInterval) --Sending Indian ground troops every 15 seconds	
	ParadropIndianUnits()
	Trigger.OnAllKilled(AttackersTeam, AttackersKilled)
end
  • created by Umair Azfar Khan