# NPC Movement

**URL:** <https://forum.choiceofgames.com/t/npc-movement/100869>\
**Category:** ChoiceScript Help\
**Tags:** tips, coder\
**Created:** [May 20, 2021, 4:05pm UTC](https://forum.choiceofgames.com/t/npc-movement/100869 "2021-05-20T16:05:59Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![Sam\_Ursu](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/sam_ursu/32/111478_2.png) [@Sam\_Ursu](https://forum.choiceofgames.com/u/Sam_Ursu)\
**Post date:** [May 21, 2021, 9:29am UTC](https://forum.choiceofgames.com/t/npc-movement/100869/11 "2021-05-21T09:29:35Z")

</div>

My last game [Stab the Assassin!](https://forum.choiceofgames.com/t/stab-the-assassin/100692) has 10 different NPCs wandering around a ship, and all of them have to follow the physical layout of the ship (instead of randomly appearing/disappearing) when moving from one room to another.

The way I did it was to create one file called “router.txt” that lists all possible routes that can be taken from any given room (although the player doesn’t ever see it, all rooms in the game are numbered, for the sake of making the coding easier. But this works equally well if rooms have LOWERCASE names with no spaces, i.e. “dungeon\_room”).

Then, a \*rand operation determines which room the monster moves to next from the _available_ exits in the current room.

router.txt is split into labels with numbers, such as:

```
*label 1

*rand diceroll 1 3

*if dieroll = 1
  *set monster_route 13
 
*if dieroll = 2
  *set monster_route 5

*if dieroll = 3
  *set monster_route 11

*return

*label 2

*rand diceroll 1 4

*if diceroll = 1
  *set monster_route 6
 
*if diceroll = 2
  *set monster_route 1

*if diceroll = 3
  *set monster_route 27

*if diceroll = 4
  *set monster_route 5

*return

```

Each label (such as “1”) in route.txt is the route information for a particular room. Notice how Room #1 has three possible exits while Room #2 has four possible exits.

Then, in your main action scene, you write:

`*gosub_scene router {current_room}`

Where $current\_room is the room the monster is in right now. This subroutine then identifies which room the monster can move to next based on where the monster is right now.

The next line in your action scene is:

`*set current_room monster_route`

The monster has now moved from its current room to the new room thanks to just **two** lines of code.

And if you wanted to, you could shorten it to just one line of code by putting the \*set current\_room monster\_route line inside of route.txt.

PS - To see if the monster is in the same room as the player, just add \*_if player\_room = current\_room_ to trigger the text/choices for interacting with the monster.

---

_[View the full topic](https://forum.choiceofgames.com/t/npc-movement/100869)._
