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
345
Reaction score
232
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

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,113
Reaction score
10,545
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
345
Reaction score
232
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,113
Reaction score
10,545
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
345
Reaction score
232
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!
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

You guys want more tutorials? Well not only are we doing new video tutorials with Driftwood, we've also brought in two contributing authors for the blog to get a new tutorial every week! First one here from @Avery
https://blog.rpgmakerweb.com/tutorials/tutorial-mapping-interior/

Next week we'll get something from @hiddenone
Husband has roped me into playing one of his PSO2 characters. Productivity is now negative.
Tileset A is lava! Don't burn D:
as MZ come out what you guys think about.....Some particles engine :3c?
Who wants to sing this song with me?~

Forum statistics

Threads
100,708
Messages
978,565
Members
132,320
Latest member
LavaWave
Top