Static NPCs
The Static NPC is the easiest example in this project – they stand still in one spot, and only move when interacted with by the player. You could just as easily drop a single sprite in, but by using the base/sprite system, the option is there should you want to add movement later. Like if you want the NPC to walk up to the player when they see them for example.
In my project, our bald man is the Static NPC – paired with the yellow base. He has just two states, Idle and Talking. When his state is Idle, he simply faces in the direction assigned by his Direction instance variable. When his state is set to talking, the direction will be set so that it’s facing the player, and the interaction function is called.
So, onto the events themselves. The first bit of logic we need to implement should ensure that our bases and sprites are always paired together, and the sprite is always on top of the base:
Condition
Static_NPC_Base ▶︎ On created
Action
Static_NPC_Base ▶︎ Set Pair_ID to Static_NPC_Sprite.UID
Condition
System ▶︎ Every tick
Action
Static_NPC_Sprite ▶︎ Set position to (Static_NPC_Base.X, Static_NPC_Base.Y)
Next, we need to set up the logic for our Idle state – so we need to make sure we tell the sprite to play the correct animation and pick the correct frame according to its direction. The sprite has five animations, Idle, and a Walk in each direction. Idle’s animation speed is set to 0 so that we can set a frame and not have to worry about it changing.
Condition
System ▶︎ For Each Static_NPC_Base
Static_NPC_Sprite ▶︎ State = Idle
Action
Static_NPC_Sprite ▶︎ Set animation to “Idle” (Play from beginning)
Sub-Event Condition
Static_NPC_Sprite ▶︎ Direction = Right
Sub-Event Action
Static_NPC_Sprite ▶︎ Set animation frame to 3
Next, just make three more sub-events for the remaining three directions. Make sure your animation frame numbers line up with the appropriate directions!
For the “Talking” state, you can copy the “Idle” state events and just switch the value for the instance variable. Now we should take the opportunity to set up our placeholder function – you could replace this with a dialogue system for example.
On function NPCTalk – number parameter NPCUid
Condition
NPC_Sprites ▶︎ Pick instance with UID NPCUid
Action
NPC_Sprites ▶︎ Set opacity to 50%
System ▶︎ Wait 2 seconds
System ▶︎ Signal “EndInteraction”
This function mainly exists to test whether or not the interaction is working – when the Z key is pressed and the conditions are met, the NPC should face the player and become more transparent (because we’re reducing the sprite’s opacity). That way we know the function has been triggered and the interaction completed.
We just need to add an extra event to trigger the “Talking” function:
Condition
Static_NPC_Base ▶︎ State = “Talking”
Action
Function ▶︎ Call NPCTalk (NPCUid: Static_NPC_Sprite.UID)
System ▶︎ Wait for signal “EndInteraction”
Static_NPC_Sprite ▶︎ Set opacity to 100%
Static_NPC_Sprite ▶︎ Set Direction to Self.InterruptedDirection
Static_NPC_Sprite ▶︎ Set State to Self.InterruptedState
Player ▶︎ Set State to “Normal”
So your final event block for the Static NPC should look like this:
And that’s it for the Static NPC!