How to make an event move towards an event with same name that is the closest?

eluukkanen

Game Dev
Veteran
Joined
Feb 16, 2016
Messages
354
Reaction score
242
First Language
Finnish
Primarily Uses
N/A
Hello anyone with knowledge of MV scripting :kaojoy:

I am trying to achieve a system and I am almost complete. However, this issue has been something I have been unable to make into reality. I know this is possible, but I just cannot seem to get my head around the best possible solution. Anyone who knows how to help, I appreciate it.

First Problem

upload_2019-7-11_10-41-58.png

Basically I want to know how to make an event move (with script command) towards and event with the same name that is the closest. I figured out that a way to do this is to make a parallel process event that checks constantly x and y of event with the same name. That I have been able to achieve thx to help of this same forum.

However, how possibly I could make so that this specific event (Bandit guy in the picture) will see what event id is the closest and will move towards that. And if there are two in the same distance, it will pick the one randomly that is in the same distance.

I am using the bandit guy as an example in the picture, but I am planning to use this system for a scene in a game where villagers should run towards and event that randomly appears on spots to a map. If you would always make it specific, I could easily just setup regular coordinates to make it seem like events run towards the character, but with random element (changing location) that is all but more difficult.

Second Problem

upload_2019-7-11_10-30-28.png

I have tried to figure out how to make a character to only move towards event with the same name if they are close to one. This has been a headache, but I know it is possible to make. My scripting skill is still limited, but I know that in theory this should be possible. First I should calculate eventx and y or the one who moves to see if it is 7 tiles close to events with the same name x and Y and that would activate command for the event to move towards events with the same name. I am still figuring out the numbers. I know there are talented scripting people here. Thank you anyone who might help with this!

This is a problem as locations of all events in the operation will be changing and that makes figuring out the process bit difficult as I am still very novice when it comes to rpg maker scripting.

Thank you for your time
 

Attachments

  • upload_2019-7-11_10-29-52.png
    upload_2019-7-11_10-29-52.png
    22.8 KB · Views: 3
  • upload_2019-7-11_10-30-5.png
    upload_2019-7-11_10-30-5.png
    130 KB · Views: 3
  • upload_2019-7-11_10-30-12.png
    upload_2019-7-11_10-30-12.png
    130 KB · Views: 3

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,125
Reaction score
10,644
First Language
Czech
Primarily Uses
RMMV
I didn't check it, but theoretically it might work. Syntactically it is correct, no worries there. If you need only 10 lines, you may want to delete some enters. That should be fine as long as you delete only these enters and nothing else.
Code:
var arr = [];
//let's say our event's ID is 5. I want to find the events with same names as event 5.
for (var i in $gameMap.events()) {
if (i+1 == 5) continue;
if ($dataMap.events[i+1].name == $dataMap.events[5].name) arr.push (i+1);
}

//Now let's find which one is the closest. I'll find it by averaging the x and y distance.
var closestEvent = null;
var closestEventDistance = 200; //or some other unreal distance
for (var i in arr) {
var curEvent = $gameMap.event(arr[i]);
var xDistance = Math.abs(curEvent.x - $gameMap.event(5).x);
var yDistance = Math.abs(curEvent.y - $gameMap.event(5).y);
if (xDistance > 3 || yDistance > 3) continue; //if the event is too far in either axis, ignore it
var curEventDistance = (xDistance + yDistance) / 2;
if (curEventDistance < closestEventDistance) {
closestEventDistance = curEventDistance;
closestEvent = curEvent;
}
}
if (closestEvent) $gameMap.event(5).moveStraight($gameMap.event(5).findDirectionTo(curEvent.x, curEvent.y));

Btw. If you don't want to average the distance like this, but just want to find a total distance of squares the character has to walk through regardless whether it's x or y, then just swap
var curEventDistance = (xDistance + yDistance) / 2;

with
var curEventDistance = xDistance + yDistance;
 
Last edited:

eluukkanen

Game Dev
Veteran
Joined
Feb 16, 2016
Messages
354
Reaction score
242
First Language
Finnish
Primarily Uses
N/A
I didn't check it, but theoretically it might work. Syntactically it is correct, no worries there. If you need only 10 lines, you may want to delete some enters. That should be fine as long as you delete only these enters and nothing else.
Code:
var arr = [];
//let's say our event's ID is 5. I want to find the events with same names as event 5.
for (var i in $gameMap.events) {
if (i+1 == 5) continue;
if ($dataMap.events(i+1).name == $dataMap.events[5].name) arr.push (i+1);
}

//Now let's find which one is the closest. I'll find it by averaging the x and y distance.
var closestEvent = null;
var closestEventDistance = 200; //or some other unreal distance
for (var i in arr) {
var curEvent = $gameMap.event(arr[i]);
var xDistance = Math.abs(curEvent.x - $gameMap.event(5).x);
var yDistance = Math.abs(curEvent.y - $gameMap.event(5).y);
if (xDistance > 3 || yDistance > 3) continue; //if the event is too far in either axis, ignore it
var curEventDistance = (xDistance + yDistance) / 2;
if (curEventDistance < closestEventDistance) {
closestEventDistance = curEventDistance;
closestEvent = curEvent;
}
}
if (closestEvent) $gameMap.event(5).moveStraight($gameMap.event(5).findDirectionTo(curEvent.x, curEvent.y));

Btw. If you don't want to average the distance like this, but just want to find a total distance of squares the character has to walk through regardless whether it's x or y, then just swap
var curEventDistance = (xDistance + yDistance) / 2;

with
var curEventDistance = xDistance + yDistance;

Thank you very much, Poryg! I need to test this right away! The script you posted looks something that can work. Either way, excellent work!
 

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,125
Reaction score
10,644
First Language
Czech
Primarily Uses
RMMV
I actually had two typos there in terms of bad brackets. They're now fixed.
 

eluukkanen

Game Dev
Veteran
Joined
Feb 16, 2016
Messages
354
Reaction score
242
First Language
Finnish
Primarily Uses
N/A
I actually had two typos there in terms of bad brackets. They're now fixed.
Thanks for the fixes! I am terrible in scripting even though I have been trying to learn a ton recently. You seem to be knowledgeable in this!
 

Latest Threads

Latest Posts

Latest Profile Posts

I'm moving on from RMXP finally. I've been stuck in a difficult decision between XP and MZ, and I've given up and moved on to MZ. Overall I'm really liking it, I wish tilesets were easier to setup like XP, but it's actually not that bad!
Have you ever gone full 'Ship of Theseus' while frankenspriting? Because I just did.
I saw my new eye doctor today, he reminds me of Satoru Iwata for some reason
I've been using a modified version of a script called Multiple Messages. It makes my text boxes look like a Mario & Luigi game! There is something about it that bothers me a little bit, but I shouldn't worry about that now.

Side note, Mist's walking animation is almost perfectly in sync with Loose Yourself by Eminem.
Starting work on some stuff for the community. Hopefully it will be fun, and useful to somebody.

Forum statistics

Threads
107,773
Messages
1,032,040
Members
139,915
Latest member
nick7626
Top