Making NPCs walk

NPC movement and reactions are governed by a set of rules known as Artificial Intelligence or AI. A NPC will walk, run, shoot, or find cover based on a multitude of external stimuli. Each NPC has different AI associated with it.

Making NPCs walk

You could place a generic NPC in your map to add some more immersion, yet without scripts, they won't do too much. As an example, combine soldiers and headcrabs will attack if they see you, but citizens will look at you and acknowledge your presence. These are just default behaviors, but thankfully, we don't have to fully rely on plain NPC AI. We can create scripts to get the NPCs to walk, talk more, and interact with the world. Making NPCs walk is quite easy. The path_corners entity can be used as navigation points for NPCs. There are two main ways to get an NPC to follow a path: with spawn activity and with an aiscripted_schedule.

Simple NPC movement

NPCs can be told to follow a set of path_corners, similar to a train, when they spawn. This is useful for populating, say, a busy city street; to add ambience; and simple, simulated random player movement. Controlling NPCs on path_corners is quite limited; however, you cannot control the speed of the NPC other than telling them to walk or run.

Place a citizen NPC (npc_citizen) in a room along with a path_corner entity. Name the path_corner path01 and copy it by holding Shift, clicking on it, and dragging the copy somewhere else three times to create a four node path.

Simple NPC movement

Assign the fourth path_corner a Next Stop Target of path01 so it makes a complete loop. The npc_citizen entity has a target path corner property that will specify where it will move to after being spawned. Once the NPC reaches the first path_corner, it will continue to follow the path until it is told to do something else. Because this behavior is automatic, it requires no triggering! As mentioned previously, NPCs follow path_corners similar to how a train does. The more path_corners you place in a turn, the smoother the movement will be. There are only four path_corners in this example, so the npc_citizen entity will reach a node, turn 90 degrees, and then continue on towards the next corner.

So we just set up a never-ending loop of mindless NPC movement. What if you want to specifically tell your NPC to move to a point based on a certain set of circumstances? There is no command in an npc_citizen input list that will ask it to walk to a specific area. In order to do this, we need to use an entity called aiscripted_schedule.

Controlled NPC movement

Aiscripted_schedules can be used for far more than just making NPCs walk. For the time being however, let's experiment with the link between an aiscripted_schedule and a set of path_corners. Create an npc_citizen and a loop of path_corner entities. Place a func_button on the wall and an aiscripted_schedule above the button. This is what we want: when the player uses the button, the NPC will start to walk along the path until it reaches the end. When the NPC reaches the last path_corner and finishes walking, the sequence can be restarted.

Controlled NPC movement

Since we want full control over the NPC, we should disable some of its AI control before we continue. Open the npc_citizen properties and check the following flags: Ignore Player Push and Not Commandable. Checking Not Commandable makes it so the NPC cannot be distracted by using it. Ignore Player Push tells the NPC not to move out of the way if you bump into it; the NPC will navigate around you to reach its goal.

The aiscripted_schedule properties

The aiscripted_schedule entity assigns goals to NPCs. You can assign enemies as goals, set entity locations as goals, and also set a path as a goal. Since we want our NPC to walk along a path, we set the Schedule to Run property to Walk Goal Path. This will make the NPC walk towards the path_corner specified in the Goal entity field and follow the path to the end. The NPC I have created for this script is an npc_citizen named meaghan and the path starts at meg_path_01. When activated, this aiscripted_schedule will make meaghan walk along the goal path starting with meg_path_01. The Repeatable flag is checked so the schedule can be activated multiple times.

The aiscripted_schedule properties

There are a few other parameters we can play with. The Search Radius is set to the default value of 0, which will ensure that it searches the entire map for the target NPC. If the NPC is not found, the script will not run. You can set an AI state for the NPC when the script runs. Leaving the AI state to set value at its default, None, will not change the current AI; however, you could force a Combat, Alert, or Idle state if you wish. The Interruptability controls how this script will stop running. In this case, only the death of meaghan will stop the script, but you can also stop the script if she takes any damage.

Triggering the schedule

In the example map, meaghan is locked in a cell. When the button is clicked, the following actions take place:

OnPressed > switch03_active > HideSprite > 0.00s Delay
OnPressed > switch03_not_active > ShowSprite > 0.00s Delay
OnPressed > meg_walk > StartSchedule > 0.00s Delay
OnPressed > button03 > Lock > 0.00s Delay
OnPressed > cell_door > Open > 0.00s Delay

There's a lot going on in that button's output, but from top to bottom, here's what's going on and why: there are two env_sprites (sprites) next to the button that will tell the player whether the button can be used or not. The first two outputs hide the green sprite and show the red sprite to let the user know that the button is not usable. The third output is the important one; it activates the aiscripted_schedule and sets the NPC on the path. The fourth output locks the button so it cannot be used, and the last output opens the cell door so meaghan can leave.

The sequence is complete when the door closes with meaghan in the cell. So this is the perfect place to enable it to be activated again. The very last path_corner tells the cell's door to close itself, and when the cell door is closed, the following events happen:

OnFullyClosed > button03 > Unlock > 0.00s Delay
OnFullyClosed > switch03_not_active > HideSprite > 0.00s Delay
OnFullyClosed > switch03_active > ShowSprite > 0.00s Delay

When the door closes, the button is unlocked, allowing the player to use it again. The red sprite is hidden in the second output, and the green sprite is displayed in the third output letting the player know that the button can be used.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.70.170