Triggers and Examples

From Off Grid Wiki
Jump to: navigation, search

Setting up a Trigger

Triggers are an important part of modding because they allow the level to react and change depending on the player's interactions with the level. They have many uses, including but not limited to: conversation prompts, objective updates, and some sounds which may be activated by the player like a floorboard creak or security barrier beep.

Setting up a trigger is a fairly simple process. Firstly in the Hierarchy tab, click Create and navigate and click on Trigger as shown below, this will place the trigger into your level.

How to create the trigger

Now in the Inspector view you will see that there are already some components added into the trigger object. At this point in time you might want to change the shape of the trigger to fit a doorway, or corridor either in the 3D View or in the Transform component of the trigger - you will want to bear in mind that as soon as the player has touched one part of the trigger zone, it will activate its function, hence make sure that it is suitably shaped so that it will only trigger where it is intended to be interacted with. You should also give the trigger an appropriate name relating to its function, for example I've called this trigger (see below) 'OfficeObjectiveStart', because as the name suggests, it will start the Office Objective when the player walks into the trigger zone. I've also made sure that 'Trigger Only Once' is ticked because I don't want the player to reactivate the objective if they have already completed it, nor do I want the objective to keep appearing on the player's screen.

An example of a possible objective trigger

Now the trigger is set up, when the player walks into the trigger zone the trigger will be activated. The only problem at this point is that the trigger isn't actually set to do anything. For us to give it a function we will need to use some Lua, the Lua we use differs for what you are trying to achieve so look at the next section for specific triggers.

Examples of Triggers

Triggering an Objective

For this we'll use the trigger that we set up earlier in this guide, 'OfficeObjectiveStart'. In the Mission Script within the mission table you should have a table called objectives with code like the following:

52 -- Mission objectives:
53 	objectives = {
54 		enterTheBuilding = {
55 			name = "Enter the building",
56 			onStart = function()
57 				print("Player must now enter the building")
58 			end,
59 			onCompleted = function()
60 				print("The player is now in the building!")
61 			end,
62 		},
63 	},

To start let's change the code to match what our objective a bit more. To start we can change the enterTheBuilding table to something like enterOffice. We can also change some of the text to match our objective of entering the office. Once you have personalised the code a bit, you should have something like what is shown below. The lines where text is printed is mainly for testing purposes and doesn't actually do anything that the player will see, as when the objective is started, only Enter the building will be outputted to the player's screen, and Player must now enter the building will instead be outputted to the console.

52 -- Mission objectives:
53 	objectives = {
54 		enterOffice = {
55 			name = "Enter the office",
56 			onStart = function()
57 				print("Player must now enter the office")
58 			end,
59 			onCompleted = function()
60 				print("The player is now in the office!")
61 			end,
62 		},
63 	},

The objective is now created, we only have one thing to do now, and that is to link the trigger we created in our level with this objective, and to do that, it only requires a small section of code which you can see below.

184 MissionObjects["OfficeObjectiveStart"].OnTriggerEnter = function(name)
185 	if name == Player.GetName() then
186 		Mission.StartObjective(mission.objectives.enterOffice)
187 	end
188 end

The trigger that would be used in the code above would be OfficeObjectiveStart which is the one I created earlier. If you then wanted to add another objective trigger to follow this objective, you only need to change a small part to the code template which you can see below. We'll be assuming that this Alleyway Objective follows the Office Objective - remember to create the enterAlleyway table has been created in your Mission Script!

190 MissionObjects["AlleywayObjBegin"].OnTriggerEnter = function(name)
191 	if name == Player.GetName() then
192 		Mission.CompleteObjective(mission.objectives.enterOffice)
193 		Mission.StartObjective(mission.objectives.enterAlleyway)
194 	end
195 end

As you can see, when the player enters the 'AlleywayObjBegin' trigger, it will check if your character is the one entering the trigger zone, and if it is your character it will firstly complete the Office Objective, and then begin the Alleyway Objective.

Triggering Conversations and Sounds

If you are interested in creating triggers for conversations and sounds, you might want to read up on the following pages.