Triggers and Examples

From Off Grid Wiki
Revision as of 16:48, 25 July 2017 by Dominic (talk | contribs) (finished section)

Jump to: navigation, search

Setting up Triggers

Triggers are an important part of modding because they allow the level to react and change depending on the player's actions and interactions and 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 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 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.

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 HUD, 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, and make sure that the enterAlleyway table has been created in the Mission Script!

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

As you can see, when the "AlleywayObjBegin" trigger is entered, it will check to see if your character is the one entering, and if it is, it will firstly complete the Office Objective, and then begin the next 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.

http://wiki.offgridthegame.com/index.php?title=Conversations

http://wiki.offgridthegame.com/index.php?title=Triggering_Sounds