Random Movement - NPCs and Enemies Walking Around
How to make NPCs and enemies walk around their position, configured per object in the admin panel.
How it works in the game
An object with random movement waits a random time, picks a random walkable tile around its original position and walks there, then waits again. The movement never leaves the area of maxTiles tiles around the original tile:
- Only targets reachable with a path that stays inside the area are used.
- An object pushed outside the area (for example by a collision) walks back to its original tile.
- When the path is blocked by a wall or another body, the object drops it and picks a new target on the next move.
- An NPC stops while any player has its dialog open, and continues when the dialog is closed or the player leaves the room.
- An enemy stops while it is in battle with any player.
In the sample data, the NPC Alfred in the town (npc_1) walks up to 5 tiles around his position, the aggressive enemies of the forests up to 3 tiles and the non-aggressive ones up to 8 tiles.
Setting up random movement in the admin panel
- Open Game Objects > Objects in the admin panel and edit the NPC or the enemies object.
- Add the randomMovement property to the Private Params JSON, for example "randomMovement":{"maxTiles":5}.
- For NPCs also add "hasState":true: the object needs a body with state to move (the enemies always have it). Without it the server logs "Random movement requires a body with state" and the NPC stays still.
- Click Save and restart the server.
For enemies, the setting goes in the multiple object that spawns them in the respawn area, and every spawned enemy uses it:
// NPC Alfred (npc type)
{"runOnAction":true,"playerVisible":true,"collisionType":4,"hasState":true,"randomMovement":{"maxTiles":5}}
// Forest enemies (multiple type, childObjectType 4 = enemy)
{"shouldRespawn":true,"childObjectType":4,"isAggressive":true,"interactionRadio":170,"randomMovement":{"maxTiles":3}}
Configuration reference
Properties of the randomMovement object (all optional):
- maxTiles (3) - size of the movement area, in tiles around the original tile.
- minDelay (3000) and maxDelay (8000) - the wait between two moves is a random number of milliseconds between these values.
- targetAttempts (10) - how many random tiles are tried on each move before skipping it.
Code integration (advanced)
- ObjectRandomMovement (lib/objects/server/object/object-random-movement.js) implements the behavior, using the room path finder with a grid that blocks the tiles around the movement area.
- The objects plugin (lib/objects/server/plugin.js) starts it on reldens.createdWorldObject through ObjectsManager.startObjectRandomMovement(), which only runs when randomMovement is an object and the body can move.
- The NPC object calls pauseForInteraction() when a player opens its dialog and resumeAfterInteraction() when it is closed.
See Objects Entity for the other object params and Enemy Respawn Flow for the respawn areas.
reldens