NPC Random Movement

Excluding the existing “transit systems” in Deadline, Suspect, Suspended etc what would be a good way to code randomised movement into an NPC?


The way I might try to tackle this is to have basically an interrupt routine which perhaps either looks at a global variable (for when to move the npc. or if it is time to move them) and maybe have it randomly select from a table of all of the places that npc moves to.

    . . .

Then, it would only be a matter of invoking the RANDOM routine (with the maximum index into this table) and take the room at that index, from this table. Then Viola! You have an NPC which randomly moves between these points. Now there are other ways of course, if it is a small map, you could have this interrupt routine just manually move the npc, in a more hardcoded way, to some specific locations. (For example, maybe the doctor only goes between his office and his examination room?)

Of course, these are merely suggestions, I’m positive that there could be several ways one could achieve this.

1 Like

Seems logical enough! Thank you! :+1:

I’m a beginner, but I used this to code a ghost that teleports randomly around a specific region of the game world, and it seems to work well for my purposes.

A ghost is a kind of person. The phantom is a ghost in the Dining Hall.
The First Floor is a region. The Dining Hall is a room in the First Floor.

A room is either haunted or unhaunted. A room is usually unhaunted. 

Definition: A room is haunted if it has a ghost in it.

When play begins:
	the phantom teleports in four turns from now.
At the time when the phantom teleports:
	if Phantom is in a room (called the current phantom space):
		let the next phantom space be a random not haunted room that is in the First Floor;
		if the phantom is visible, say "The phantom disappears in a flash!";
		now the phantom is in the next phantom space;
		if the phantom is visible, say "The phantom appears in a flash!";
	the phantom teleports in four turns from now.

Now this is a specific case, as it’s a creature that doesn’t need to move into intermediate room - but what I like about doing this is it makes it easy to code other NPCs’ varying behavior depending on whether or not the phantom is present. It’s also easy to add additional reactions to the Phantom’s entrances and exits within the teleport event.

1 Like

I see the logic… but I think that’s Inform 7? I’m using ZIL :slightly_smiling_face: Thanks though for taking the time to reply :+1:

1 Like

Have you used random NPC movement much before?

In my small attempts, I’ve found I lose touch with how the game is supposed to play. I find it very difficult to test if it’s triggering with the right frequency to be effective.

Right now I have an NPC who patrols and offers hints when he meets the player. And I’m struggling to get that right from a narrative point of view, even when this interaction is more predictable than a random one.

Ah, my bad :: still learning how everything works here.


If you look under the thread title, you can see that this is the “ZIL” subsection of the “Authoring” section of the forum. That’s the easiest way to know which language/engine people are discussing in the thread. Some board sections have additional tags for clarification if they have more than one language that may be relevant to the engine (like Twine, for instance).

It’s easy for new users to assume that this board is all about Inform 7, but it actually caters to quite a few different engines and languages.


This is also not in ZIL (sorry), but the code is simple enough that I think you should be able to see the logic behind it. I was going to just explain how I did it, but I figured the code would be clearer. The code is in JavaScript and has a couple user functions not shown, but hopefully it all makes sense.

There are probably better ways, but this is a simple method that I cobbled together to avoid the feeling that the character was basically teleporting all over the map. It felt really jarring. This way feels like they’re naturally walking about the house.

function moveChar(char) {
	// only move characters half of the time
	if (!percentCheck(50)) return;
	// assign exits to locations
	var rooms = {
		"foyer": ["2F_hallway", "east_hallway"], 
		"2F_hallway": ["foyer"],
		"east_hallway": ["kitchen", "dining_room", "bar", "living_room", "back_hallway"],
		"kitchen": ["east_hallway", "dining_room"],
		"dining_room": ["east_hallway", "kitchen"],
		"bar": ["east_hallway"],
		"living_room": ["east_hallway"],
		"back_hallway": ["east_hallway", "theater", "pool", "veranda"],
		"theater": ["back_hallway"],
		"pool": ["back_hallway"],
		"veranda": ["back_hallway", "back_yard"],
		"back_yard": ["veranda"]

	// track where they were last
	var last_location = char.location; 
	// move character to random location accessible from their current location
	char.location = randomItem(rooms[char.location]);

	// print messages if you're able to see them enter or leave your location
	if (last_location === player_location) write( + " wanders off.");
	else if (char.location === player_location) write( + " wanders into the room.");
1 Like

If you want the NPC to wander around the map by picking random exits, instead of teleporting to random locations across the map, you can use MAP-DIRECTIONS to loop through the direction properties in the NPC’s location. Loop once to count the number of useful exits (probably limited to UEXITs), then generate a random number in that range and loop again to find the n’th exit.


Thank you! :slightly_smiling_face::+1:

I might as well post here too that I wrote an example for Adam the other day, showing how it might be done in ZIL if the game is small enough (he provided a map of what he had in mind).

I did try to incorporate a few different ideas. The NPC follows a non-random patrol route, but it can be affected by both global and local things. If you turn on a radio, he will walk towards it to turn it off, and there is a door that he will always close if he finds it open. (Which of course means there has to be an exception if the radio is behind that door. Isn’t conflicting goals fun?)

There is the gigantic caveat that I’m no ZIL expert by any stretch of imagination, but if someone wants to play around with it, improve it, host it elsewhere, etc. feel free.


You can use what’s been done in some Infocom games. The programmers used the PICK-ONE routine with a table of rooms to choose a room.

The original version of PICK-ONE would just pick a random entry out of a table (this was mentioned before). Subsequent calls could pick the same room. The newer version will only pick an entry if it has not been picked already. Once all the entries are picked, then all options are fair game again. So you can repeatedly call PICK-ONE and get “randomly” chose rooms without repeats.

If you need to walk a random path, then Jesse’s method is best. If you know the lowest numbered exit property (LOW-DIRECTION in some games), then you can have a routine that randomly picks a number out of all the directions (LOW-DIRECTION to 31). If that entry is zero or not the type of exit you want (like a door), then you can just have the routine pick another random number in that range.

FYI: New version of PICK-ONE From Zork I -

	 <SET L <- .L 1>>
	 <SET RND <RANDOM <- .L .CNT>>>
	 <SET CNT <+ .CNT 1>>
	 <COND (<==? .CNT .L> <SET CNT 0>)>
	 <PUT .FROB 0 .CNT>
1 Like