“Hey, you! Let’s battle!” Adding a Line of Sight mechanic to your NPCs

13

Index

Attached Files

The following files have been attached to this tutorial:

.c3p

npc-los-example.c3p

Download now 255.98 KB

Stats

4,521 visits, 7,880 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Setup

The first thing we need to do in this project is to add some new functionality to our NPCs. To the NPC Base family, you’ll need to add the Line of Sight behavior. Within the behavior’s properties, set Obstacles to Custom. You can leave the range and cone of view properties as they are. The family also needs a new number instance variable – RayHitID. This instance variable will be used to tell the NPC what object has intersected its Line of Sight.

To the NPC Sprites family, you’ll need to add the Persist behavior – this means that when switching layouts, any instance variables will be preserved. This way, your NPCs will be exactly where you left them when you come back to the layout. However, you’ll also need to add a new variable to the NPC Sprites family – a Boolean variable called CanBattle. Otherwise, your poor player will end up in an endless loop of being spotted and challenged by the NPC!

Next, you’ll need to add two global variables. A string variable called CurrentLayout which we’ll use to store the name of the layout the player is on, and a Boolean variable called BattleInProgress – this allows us to tell the game whether or not the ‘battle scene’ is currently being displayed.

Given that we have a variable called CurrentLayout that would suggest that we’ll be using more than one layout. Which is true! And we need to add a new one, our ‘battle scene’. Create a new layout, (which won’t need an event sheet) and call it BattleLayout. On this new layout, add a button object and give it the text ‘Go Back’. This will just be used to send the player back to the first layout as part of this proof of concept.

When your NPC spots the player, we need something to show that they’ve been seen. So add a new Sprite object called ‘Exclamation’. Well, you can call it whatever you like, it’s just a quickly drawn speech bubble!

And that’s all of the new objects, variables and behaviors! In the next section, we’ll start adding the new logic required to make this mechanic work.

  • 2 Comments

  • Order by
Want to leave a comment? Login or Register an account!