Creating RPG-style NPCs

9

Index

Attached Files

The following files have been attached to this tutorial:

.c3p

npc-example.c3p

Download now 237.03 KB

Stats

7,503 visits, 13,404 views

Tools

Translations

This tutorial hasn't been translated.

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.

Set Path NPCs

This is where things start to get a little more complicated! The set path NPC (as the name suggests) will walk along a path dictated by a set of ‘mover’ objects. The NPC’s direction variable will be set by these objects to ensure it stays on the correct path. Because this section of code is quite large, I’ll be adding images of each event block as we go along, without the complete section at the end.

To start off, you’ll need to set up the same pairing events as the Static and Rotating NPCs. So, when the base is created, its Pair_ID is set to the UID of its corresponding sprite, and Every Tick the position of the sprite is set to that of the base.

Next, we can set up the States for the NPC. This movement type requires quite a few states on top of the standard “Idle”, “Walk” and “Talking”. Because we need the NPC to remember its path when intersected by the player, we’ll also be using a “Stopped” state for each direction. And because we’re already using the ‘Interrupted State’ instance variables in the “Talking” interactions, we’ll need a separate set of variables for when the player simply walks in front of these NPCs – name them InterruptedState2 and Interrupted Direction2.

This NPC also uses a pair of instance variables to allow you to set the speed of the tile movement behavior – make sure to add TileMovementX and TileMovementY as number instance variables to the SetPath_NPC_Sprite object.

Starting off with the “Idle” state, you’ll need the following:

Condition:

System ▶︎ For Each SetPath_NPC_Base

SetPath_NPC_Sprite ▶︎ State = “Idle”

Action:

SetPath_NPC_Sprite ▶︎ Set animation to “Idle” (Play from beginning)

Sub-Event Condition

SetPath_NPC_Sprite ▶︎ Direction = Right

Sub-Event Action

SetPath_NPC_Sprite ▶︎ Set animation frame to 3

As with the other NPC types, create three more sub-events for the remaining directions, making sure your animation frames line up correctly.

Next, the “Walk” state:

Condition:

System ▶︎ For Each SetPath_NPC_Base

SetPath_NPC_Sprite ▶︎ State = “Walk”

Action

SetPatch_NPC_Sprite ▶︎ Set animation to “Walk” & Self.Direction (play from beginning)

SetPath_NPC_Base ▶︎ Set TileMovement Enabled

SetPath_NPC_Base ▶︎ Set TileMovement speed to (SetPath_NPC_Sprite.TileSpeedX, SetPath_NPC_Sprite.TileSpeedY)

Sub-event Condition

SetPath_NPC_Sprite ▶︎ Direction = Down

Sub-event Action

SetPath_NPC_Base ▶︎ Simulate TileMovement pressing down

Repeat these sub-events for the remaining directions.

Keeping in the same “Walk” state event, we need another set of sub-events to control how the NPC acts when the player walks in front of them.

Sub-event Condition

SetPath_NPC_Sprite ▶︎ Is overlapping Player at offset (8,0)

SetPath_NPC_Sprite ▶︎ Direction = “Right”

Sub-event Action

SetPath_NPC_Base ▶︎ Set TileMovement Disabled

SetPath_NPC_Sprite ▶︎ Set animation frame to 0

SetPath_NPC_Sprite ▶︎ Set animation speed to 0

SetPath_NPC_Sprite ▶︎ Set InterruptedState2 to Self.State

SetPath_NPC_Sprite ▶︎ Set State to “StoppedRight”

Create three more of these sub-events for the remaining directions and offsets: Left (-8,0), Up (0,-8) and Down (0,8). And that covers all of the “Walk” related states.

The only state that’s left is “Talking”.

Condition:

System ▶︎ For Each SetPath_NPC_Base

SetPath_NPC_Sprite ▶︎ State = “Talking”

Action

SetPath_NPC_Base ▶︎ Set TileMovement Disabled

SetPatch_NPC_Sprite ▶︎ Set animation to “Idle” & Self.Direction (play from beginning)

Sub-event Condition

SetPath_NPC_Sprite ▶︎ Direction = Down

Sub-event Action

SetPath_NPC_Base ▶︎ Set animation frame to 0

As with the other states, add three more sub-events to cover left, right and up.

And that’s all of the states defined. So, now we can start adding events to decide what happens when an NPC is in a specific state.

First, we’ll set up what happens when an NPC lands on a mover object. When this happens, we need the NPC to pause, pick a new direction as dictated by the mover and then continue along the path. To do this, use the following:

Condition:

SetPath_NPC_Base ▶︎ is overlapping NPC_Mover

SetPath_NPC_Sprite ▶︎ Direction ≠ NPC_Mover.Direction

SetPath_NPC_Sprite ▶︎ State = “Walk”

Action

SetPath_NPC_Base ▶︎ Set State to “Idle”

SetPatch_NPC_Sprite ▶︎ Start Timer “PauseWalk” for Self.StateTimer

You can also set the timer to run for a standard amount of time, but using the instance variable here is a nice way to add some variation to your NPCs. Next, we need to define what happens when that timer completes:

Condition:

SerPath_NPC_Sprite ▶︎ On Timer “PauseWalk”

SetPath_NPC_Sprite ▶︎ is overlapping NPC_Mover

Action

SetPath_NPC_Sprite ▶︎ Set Direction to NPC_Mover.Direction

Sub-event Action

SetPath_NPC_Sprite ▶︎ Set State to “Walk”

That should now allow your NPC to follow a defined path quite nicely. That is, until the player walks in front of them! When the player intersects the NPC, we already have the events to switch the state to one of the “Stopped” states. However, the NPC currently has no way to return to the variables that tell it to carry on walking, so that needs to be set up:

Condition:

SerPath_NPC_Sprite ▶︎ State = “StoppedRight”

SetPath_NPC_Sprite ▶︎ is NOT overlapping Player at offset (8,0)

SetPath_NPC_Sprite ▶︎ InterruptedState2 ≠ Self.State

System ▶︎ Trigger once while true

Action

System ▶︎ Wait 0.3 seconds

SetPath_NPC_Base ▶︎ Set TileMovement Enabled

SetPath_NPC_Sprite ▶︎ Set animation speed to 5

SetPath_NPC_Sprite ▶︎ Set State to Self.InterruptedState2

SetPath_NPC_Sprite ▶︎ Set InterruptedState to “”

These events reset the instance variables to how they were before the player walked in front of the NPC, and they can carry on along their path as normal! If you wanted to add more variation to the NPCs, you could add another variable to change their animation speeds. Repeat this event for the remaining “Stopped” states.

The last thing we need to implement is what happens when the player actually “talks” to our Set Path NPC. It’s the same code as we’ve used previously, so it should be familiar if you’ve been following this whole tutorial:

Condition

SetPath_NPC_Base ▶︎ State = “Talking”

Action

Function ▶︎ Call NPCTalk (NPCUid: SetPath_NPC_Sprite.UID)

System ▶︎ Wait for signal “EndInteraction”

SetPath_NPC_Sprite ▶︎ Set opacity to 100%

Set_Path_NPC_Sprite ▶︎ Set Direction to Self.InterruptedDirection

Set_Path_NPC_Sprite ▶︎ Set State to Self.InterruptedState

Player ▶︎ Set State to “Normal”

And that’s it! You should now have an NPC type which can follow a path, be intersected by the player and then carry on as it was and be talked to by the player!

One more movement type to go – Random Path!

  • 3 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • It's a bad idea to use Wait, about 10% of the times for complex projects it bugs and doesn't work, especially in triggers. Using a global timer would be better, though a bit clunkier.

  • Nice! This will also help the user get familiar with functions and passing parameters through which is always daunting to think about when you haven't used them.

      • [-] [+]
      • 2
      • Laura_D's avatar
      • Laura_D
      • Construct Team Community Manager
      • 2 points
      • (0 children)

      Tell me about it! I'd been putting off using functions for ages because I had no idea what to do with them!