Randomly moving event reluctant to touch player

This is something I think I've never fully understood, despite reading both the official manual thoroughly and reading some threads.

I have a very normal wandering NPC that is set to Event Touch, with random movement with high frequency. And it seems to never want to touch the player to trigger the event. In fact, if I corner the NPC, where it has nowhere to go, it will only very reluctantly touch the player, and only after waiting an inordinate amount of time.

The desired behavior is for the random movement to be truly random, and for touching the player (to trigger a battle in this case) to be equally as random. I certainly don't want for the player to have to actively hunt the NPC down to trigger this battle, and I don't want to use a "player chase" plugin because it's unthematic behavior for this NPC.

I guess I just don't really understand how the engine is handling movement. Would anyone have any advice on how to create a randomly wandering enemy that has a normal chance of touching the player to trigger a battle? Or even just help me understand a bit better why I'm encountering this issue?

Thank you!! :LZScheeze::LZScheeze:
Well randomness is randomness.
Which means there's even a chance that within a 10 minute time frame there is a chance, albeit small, that the event never touches the player at all.

You need to provide screen shots of the event and move route so we can see if you have set it up incorrectly.
If you truly believe that it's not behaving correctly, trying setting up the same NPC in a new project with no plugins and see if the problem persists.
In random it first checks to see if it can move there, then if it can, it does. But it can't move on the same tile as the player so it doesn't. This is how I understand it anyways and I could be totally off base.

You can pull a sneaky and make a custom move route that is something like: move at random, move at random, move toward player and set it to repeat.
Well randomness is randomness.
Which means there's even a chance that within a 10 minute time frame there is a chance, albeit small, that the event never touches the player at all.

You need to provide screen shots of the event and move route so we can see if you have set it up incorrectly.
If you truly believe that it's not behaving correctly, trying setting up the same NPC in a new project with no plugins and see if the problem persists.

Here's a representative test -- the settings for movement are very close to default for new event:

2024-06-13 17_19_55-Window.png


And here's a super representative video of the behavior. Notice that the roach is really reluctant to touch me, and just backs away. I could keep it pinned there forever and it won't touch me.

Meanwhile the other roach -- with the same settings, did indeed feel free to move towards me and instantly trigger. So, there's certainly something to the randomness as you mentioned @TheAM-Dol.



But the "running away" behavior is easily replicable. Events seem to weight NOT touching the player if possible.
Events seem to weight NOT touching the player if possible.
They don't.

Code:
Game_Character.prototype.moveRandom = function() {
    var d = 2 + Math.randomInt(4) * 2;
    if (this.canPass(this.x, this.y, d)) {
        this.moveStraight(d);
    }
};

It chooses one of the four directions at random. There's nothing in there that references or knows where the player is at all, except - as @Slaughty said - it won't move onto a tile that has the player, that blocks passability.
A little confused still --

If, in random, it's checking to see if a tile can be occupied, then how does it decide to touch the player when in "Event touch" mode?
Because that's just for the random movement setting.
You could still set, for example, a patrol route.
That's the thing it doesn't decide, you are going to have to tell it, hence my suggestion with a custom move route.
If, in random, it's checking to see if a tile can be occupied, then how does it decide to touch the player when in "Event touch" mode?
That's actually my mistake - I thought event touch triggered when the event became adjacent to the player.

Slaughty is correct, you'll have at some point either make the event try to move into the player's space; or take the movement out of the autonomous movement and put it into the event commands and check after each movement whether it's adjacent to the player.

The other half of the point stands, there's nothing to make the event avoid or run away from the player.
I definitely get how the random movement works now, but as you can see from the video, a randomly wandering event, in "event touch" mode, WILL at some point "bump" into the player and trigger the event page.

At the end of that video, the other roach has identical settings -- random, event touch -- and that one does wander over and touch the player, triggering the battle.

So, a custom move route doesn't seem to be strictly necessary, just probably advisable if you want to really assure interaction. But random moves certainly do it sometimes 🙂

I'm wondering how on earth the event is triggered, given how the random movement code works. Is there something in the event touch code that makes it possible?
You could do:
Turn random direction
1 step forward
Instead.
My theory is it happens sometimes because usually the player is moving while the event is moving, so event checks square, it is unoccupied and passable, so it moves there but in the 6 or whatever frames it takes to move there, the player is also moving either there or in path of the event so they touch.
My theory is it happens sometimes because usually the player is moving while the event is moving, so event checks square, it is unoccupied and passable, so it moves there but in the 6 or whatever frames it takes to move there, the player is also moving either there or in path of the event so they touch.

Heheh ... but if it were that simple, then it wouldn't sometimes work when I'm standing still 🙂

In any case there's certainly options I suppose. And turning "frequency" up seems to greatly increase the likelihood of the random collision, which comports with a truly random model.
I'm wondering how on earth the event is triggered, given how the random movement code works. Is there something in the event touch code that makes it possible?
every movement a random movement event takes isnt handled by that fucntion above. a random movement event when it makes its move has a 2/5 chance to use moveRandom() and a 3/5 chance to moveForward(). moveRandom will only turn and move the event if the event could walk in that direction.

if the event is event touch and it does moveForward while facing a direction that it cannot pass into, the game checks if the player is in front of the event and runs it if so. keep in mind that it cannot turn into a direction it cant walk into as the only way it turns is with moveRandom, which checks for that.

so if the event is pinned in a way that it has nowhere to go and the player is behind or to the side, it will never trigger. but also, if the event is pinned in a way that its facing the player and has nowhere to go, it can still trigger on the 60% roll to moveForward

Spoiler: functions
Code:
Game_Event.prototype.updateSelfMovement = function() {
    if (!this._locked && this.isNearTheScreen() &&
            this.checkStop(this.stopCountThreshold())) {
        switch (this._moveType) {
        case 1:
            this.moveTypeRandom();
            break;
        case 2:
            this.moveTypeTowardPlayer();
            break;
        case 3:
            this.moveTypeCustom();
            break;
        }
    }
};

Game_Event.prototype.moveTypeRandom = function() {
    switch (Math.randomInt(6)) {
    case 0: case 1:
        this.moveRandom();
        break;
    case 2: case 3: case 4:
        this.moveForward();
        break;
    case 5:
        this.resetStopCount();
        break;
    }
};

Game_Character.prototype.moveForward = function() {
    this.moveStraight(this.direction());
};

Game_CharacterBase.prototype.moveStraight = function(d) {
    this.setMovementSuccess(this.canPass(this._x, this._y, d));
    if (this.isMovementSucceeded()) {
        this.setDirection(d);
        this._x = $gameMap.roundXWithDirection(this._x, d);
        this._y = $gameMap.roundYWithDirection(this._y, d);
        this._realX = $gameMap.xWithDirection(this._x, this.reverseDir(d));
        this._realY = $gameMap.yWithDirection(this._y, this.reverseDir(d));
        this.increaseSteps();
    } else {
        this.setDirection(d);
        this.checkEventTriggerTouchFront(d);
    }
};
@Robro33 that is a perfect answer and explains everything. Thank you!!
This thread is closed due to being reported as solved. If you need it reopened, let us know. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/randomly-moving-event-reluctant-to-touch-player.169496/