I need that when event A passes close to event B, event B behaves differently

Urashima09

Villager
Member
Joined
Apr 27, 2017
Messages
29
Reaction score
5
First Language
Portuguese
Primarily Uses
RMXP
Greetings

I need that when event A passes close to event B, event B behaves differently.

Specifically, event B is fixed, and A is an event that only travels one route. When the distance between A and B (the hypotenuse of the triangle formed by this distance and the coordinate axis) is less than 5.0, event B must call a common event.

Event B needs to do the same thing for many other events besides A. I use Yanfly's Event Spawner Plugin (http://www.yanfly.moe/wiki/Event_Spawner_(YEP), and it creates all the events that the B must react. If the distance from event A is greater than 5.0, it tries again for the next event, and so on. If he tries everyone and everyone is out of range, he starts over and checks if event A is back in range.

The events created by this plugin start with the ID 1000, so all "type A" events will have this ID or greater. Some useful commands are:

$gameMap.FirstSpawnedEvent()
- This will grab the first available spawned event as an object.
- If the first event has been despawned, the next on in the list will be returned as an object. If there are no spawned events left, this script call will return an undefined value.

$gameMap.FirstSpawnedEventID()
- This will grab the first available spawned event's ID as a number.
- If the first event has been despawned, the next on in the list will be returned as a number. If there are no spawned events left, this script call will return a value of 0.

$gameMap.LastSpawnedEvent()
- This will grab the last available spawned event as an object.
- If the last event has been despawned, the previous event in the list will be returned as an object. If there are no spawned events left, this script call will return an undefined value.

$gameMap.LastSpawnedEventID()
- This will grab the last available spawned event's ID as a number.
- If the last event has been despawned, the previous event ID on in the list will be returned as a number. If there are no spawned events left, this script call will return a value of 0.
 

 Masked 

Assistant
Veteran
Joined
Oct 28, 2015
Messages
112
Reaction score
292
First Language
Portuguese
Primarily Uses
RMMZ
Here's an idea:
Code:
const first = $gameMap.FirstSpawnedEventID();

// If there's no spawned events, don't do anything.
if (first == 0) {
  return;
}

const last =  $gameMap.LastSpawnedEventID();

const thisEvent = $gameMap.event(this.eventId());

// Reset the switch so we don't trigger anything if there's no events in range
$gameSwitches.setValue(42, false);

for (let i = first; i <= last; i++) {
  const thatEvent = $gameMap.event(i);

  // Obs.: Acessing private variables is bad, but unfortunately we do not have access
  // to these values in any other way that I know of
  const dx = thisEvent._realX - thatEvent._realX;
  const dy = thisEvent._realY - thatEvent._realY;

  // Taking square roots is expensive, so we just compare the squares
  if (dx*dx + dy*dy <= 25) {
    // Activate the switch and finish
    $gameSwitches.setValue(42, true);
    break;
  }
}

You can run this in parallel on your event B and it will activate switch 42 whenever a spawned event gets close enough to it (5 tiles in euclidean distance).

The issue with this approach is that running this every frame for many events might be a bit expensive, so it could cause lag if you spawn hundreds of events. If you really need to handle some extreme cases, there are some pretty neat algorithms and data structures for this kind of problem (some pretty darn efficient, specially since B is stationary) that could be used to improve it, but that would be a whole plugin on itself.
 

Urashima09

Villager
Member
Joined
Apr 27, 2017
Messages
29
Reaction score
5
First Language
Portuguese
Primarily Uses
RMXP
Hey @Masked
Então.. não aconteceu nada xD

Did not work
I simply copied this to a stationary event in a parallel process, and used the Yanfly plugin to create an event that moves to the end of the map and back, passing rigth in front of that event. Nothing happens :c

Would it be too much to ask to test it on a demo, please?
All I changed was to delete some line breaks and comments, as the editor only allows 11 lines of code
Here is a picture:

1609817321041.png
1609817366508.png
 

 Masked 

Assistant
Veteran
Joined
Oct 28, 2015
Messages
112
Reaction score
292
First Language
Portuguese
Primarily Uses
RMMZ
Hey \o

Sorry, I completely forgot about that inconvenience xd

Actually from the screenshots it seems like you've modified the code quite significantly, to the point of breaking it (I'm surprised it didn't give you a fatal error or something). Specifically, you've removed the for loop, which basically makes the whole thing work. I've also realized that return statements don't work on script calls (they shouldn't, really haha).

Anyway, I made a working shortened version:

JavaScript:
const first = $gameMap.FirstSpawnedEventID();
const last = first !== 0 ? $gameMap.LastSpawnedEventID() : -1;
const e1 = $gameMap.event(this.eventId());
$gameSwitches.setValue(42, false);
for (let i = first; i <= last; i++) {
  const e2 = $gameMap.event(i);
  const dx = e1._realX - e2._realX;
  const dy = e1._realY - e2._realY;
  if (dx*dx + dy*dy <= 25) {
    $gameSwitches.setValue(42, true); break;
  }
}

The logic is the same, it's just fewer lines and syntactically valid. I tested it with hardcoded values (didn't want to have to setup the Event Spawner xd) and it worked fine here.

Here's my setup:

Ym2hFeD.png

And here's the event that detects approximation of the other two (i changed the distance to 3 and hardcoded the event IDs for convenience):

e7ZMy8c.png

Also, here's the common event that it runs:

EOwfkjj.png

Notice that I set it up to run automatically when the switch activated by the detecting event (#42 in this case) is on.

Edit: A little bit off topic, but the system actually works quite nicely as a digital signal generator:

Ytqincf.gif

(the wave form is showing the value of the switch, up is ON, down is OFF)
 
Last edited:

Latest Threads

Latest Profile Posts

Sometimes you Just need to realise someone is beyond help... Still hard to walk away tho :(
100 Horror/Tragedy Sample Maps, Penguin Mech Sprite, & TileD Templates | RPG Maker News #70

Continuing working on abs. So far I have done the workout twice. I am excited to see the results after a couple of weeks. How is everyone doing? :)
MZ: oh boy imma work today
Me, excited: Really?
MZ, doesn't do what I tell It too
Me: I Thought-
MZ: I am Working!
Oof, this "side project" of mine is starting to get as complex as a VS plugin, at least as far as plugin parameters are concerned. I think I now understand why a plugin like this doesn't already exist(that I know of). Learning a lot while trying to make it work though!

Forum statistics

Threads
112,215
Messages
1,066,599
Members
145,821
Latest member
Adra
Top