Is there any way to check and see if other events are around?

I have monster events that chase you in order to start a battle.

I also have crystals to heal you, and reset the monsters locations.

I was curious if It was possible to have a conditional branch check to make sure that my monster events are a certain distance away from my crystal event.

If there is let me know! Thanks!
It is possible to prevent your events to enter the area but the fastest way without using scripts would be using other events to block the path. Events cannot pass through other events unless they have the "Through" option enabled. Taking advantage of this if you put a blank event in the path that connects your chasing event to the crystal one other events won't be able to reach the crystal.
But if you have too many events, even if they are blank, you will increase the per-frame processing required.

Each time the player or any event takes a step, they check to see if something is on the next tile. To do this, EVERY event is checked to see what tile it's on. Note - this is checking EVERY event, for every event. So if you have 50 events plus the player, it's going to do around 2500 checks (50x50). In addition, on every frame, it's going to check every event to see if there's a command that needs to be executed. Even events that don't have commands will be checked in order to know that it doesn't have a command.

So yes, you could do it by blocking off the area with events, but your game's performance could take a hit if you do it a lot.

Another option is to look for Yanfly's Region Restrictions script, which allows you to "block off" areas using regions rather than events. There is still a lot of processing to be done, but not as much as using events, so it's more lightweight.
there is.
I've adapted a formula from Victor Sant's engine for myself.... let me look for it, because I don't know it from memory.

(highly depends on your implementation tho, but it's a simple check and grab, it'll return either true/false, or the lot of nearby events)


EDIT
here:
Code:
class Game_Map
def in_radius?(orig, targ, r = 1)
    ((orig.x - targ.x) ** 2 / r ** 2) + ((orig.y - targ.y) ** 2 / r ** 2) <= 1
end
end

put that declaration in a script call, in your event sequence (or directly into the plugin list)

for the check, set it to script
Code:
$game_map.events.values.select{|event| $game_map.in_radius?($game_player, event, radius)}

where "radius" is the distance away to check for.
I was curious if It was possible to have a conditional branch check
yes, but as said above it will create lag going that way.

However for checking a few events it might still be the better way - and because of the way you ask that question I assume that you only know the simple uses of the conditional branch command, not its advanced uses.

You need two commands and a variable.
The first command is either control variable (with the game data section that defaults to map id but has hundreds of other uses) or get location info. Both event commands load specific data into the selected variable, and then you can use a condional branch command to check which numbers were loaded into that variable.

There is almost no limit to those commands in combination - you can access and check most regular data that way.
That is also why you simply cannot make any more advanced game without understanding variables and their uses.
I see no relevant Lag problems with the stuff he wants. Bad Eventing could cause him lag, but that can always happen.
I searched a Script snippet for you, it will check if the Events 2 to 50 are in 5Tile Radius of Event1, if yes the SelfswitchA of the Event ID nearby will go on (One of those Events 2 to 50).
You use the Code in the Eventcommand Script.

Code:
for a in (2..50)
if $game_map.events[a] != nil
if ($game_map.events[a].x - $game_map.events[1].x ).abs +
($game_map.events[a].y - $game_map.events[1].y ).abs <= 5
$game_self_switches[[$game_map.map_id, a, 'A']] = true
end ; end
end
Code is Tested and seems to work.
Test Example:
Event1 with Graphic, Autonomous Moveroute, Towards Player, make it on Paralell for example and insert the above code + a 20Frames Wait command.
Create Event2 with 2 Eventpages, the second Eventpage Condition SelfswitchA is On? Choose 2 different Graphics for the Eventpages 1 and 2.
Now copy paste this Event 50 Times.
Load Testplay and as soon the Event which follows the player comes in radius of the events, they will activate. Maybve this is a approach that could help you. Let us know if you need further infos or adjustment.
I see no relevant Lag problems with the stuff he wants. Bad Eventing could cause him lag, but that can always happen.
I searched a Script snippet for you, it will check if the Events 2 to 50 are in 5Tile Radius of Event1, if yes the SelfswitchA of the Event ID nearby will go on (One of those Events 2 to 50).
You use the Code in the Eventcommand Script.

Code:
for a in (2..50)
if $game_map.events[a] != nil
if ($game_map.events[a].x - $game_map.events[1].x ).abs +
($game_map.events[a].y - $game_map.events[1].y ).abs <= 5
$game_self_switches[[$game_map.map_id, a, 'A']] = true
end ; end
end
Code is Tested and seems to work.
Test Example:
Event1 with Graphic, Autonomous Moveroute, Towards Player, make it on Paralell for example and insert the above code + a 20Frames Wait command.
Create Event2 with 2 Eventpages, the second Eventpage Condition SelfswitchA is On? Choose 2 different Graphics for the Eventpages 1 and 2.
Now copy paste this Event 50 Times.
Load Testplay and as soon the Event which follows the player comes in radius of the events, they will activate. Maybve this is a approach that could help you. Let us know if you need further infos or adjustment.

This is the closest thing to what I want, but sort of the opposite. I'd want Event 1's self switch to go off if events 2 - 50 are nearby. Like if the monsters (events 2 - 50) are nearby, have the crystal (event 1, which si where you heal) be like "You can't heal now!"
Than the Code need to look like this:
Code:
for a in (2..50)
if $game_map.events[a] != nil
if ($game_map.events[a].x - $game_map.events[1].x ).abs +
($game_map.events[a].y - $game_map.events[1].y ).abs <= 5
$game_self_switches[[$game_map.map_id, 1, 'A']] = true
end ; end
end
Code explanation:
for a in (2..50)
The code Loops from 2 to 50, each iteration for "a" the System enters a number from 2 to 50.
if $game_map.events[a] != nil
In case the Event ID does not exist on the Map, this Iteration will be skipped, to prevent game crash.
if ($game_map.events[a].x - $game_map.events[1].x ).abs ...
We use the "a" to Point to the current Event ID, the number "1" is directly referencing to "EventID 1"
<= 5
The Radius is "Less than or equal 5"
$game_self_switches[[$game_map.map_id, 1, 'A']] = true
$game_map.map_id = The current map Id, you can also enter a number if you want to turn on or off a selfswitch on another map.
"1" = The ID of the Event, i changed it from "a" to "1".
"A" = The Self Switch, A,B,C or D.

Iam not a Scripter and iam terrible with Syntax, but a Loop is very usefull and can be adjusted over time for different things.

In the Link you find a Link to a Scriptcall List, its good.
https://forums.rpgmakerweb.com/index.php?threads/script-call-collection-for-vxace.25759/

Let us know if you need more Adjustments.

Edit: If you talk to your Crystal with Action Button, than include the above code in that Event its enough to run it only 1 Time when needed(Atleast in Theory, let us know if something isnt working). Also in our case when player talks to crystal, you have to worry 0% about performance.
Like if the monsters (events 2 - 50) are nearby, have the crystal (event 1, which si where you heal) be like "You can't heal now!"

ahem.....

declare the evaluation function *first*, *once*
Code:
def in_radius?(orig, targ, r = 1)
((orig.x-targ.x)**2/r**2)+((orig.y-targ.y)**2/r**2) <= 1
end

and, turn the check itself into a function
Code:
def check_for_events(hash, radius)
hash.values.select{|event| event.id.between?(2, 50)}.any?{|event| in_radius?(hash[1], event, radius)}
end

(yes, you *can* declare functions in a script block!)

grab the lot of events, and perform the check through the declared functions, locally, once, all at the same time.


nuevo-4.JPG


one check, one result, one process triggered upon interaction, no switches, no nothing.
there's no point checking the whole 50 events if you default to true even if ONE is nearby.
This looks interesting, but what would he need to insert for "orig" and "targ" ?
Its those small details which is so difficult, bringing code to work as non scripter.
This looks interesting, but what would he need to insert for "orig" and "targ" ?
Its those small details which is bringing code to work so difficult as non scripter.
nothing.

in_radius() and check_for_events() are functions declared to be used within that event, when needed.
the one thing you need, is an answer to the question of "is anyone within range?".... that's what the condition is there to process.
the answer to that question requires two parameters: what range, and within whose reach.... and "within whose reach", in this case, requires another process, because "who" is an event, and events are stored in a collection of events, and from that collection you need both one very specific event, and a lot of specific events from a potentially larger lot which is of no interest to you.
grab the lot.
Code:
$game_map.events

from the lot, grab the range of events you care about
$game_map.events is a hash, so if you call events[X], it'll return [event], an array of length 1, so you go directly to the part of the hash storing the events, and pull out the container, whole: $game_map.events.values.
from there, filter by ID.
Code:
$game_map.events.values.select{|event| event.id.between?(2, 50)}

with the filtered lot, check for the range.
Code:
$game_map.events.values.select{|event| event.id.between?(2, 50)}.any?{|event| in_radius?(self, event, radius)}

"self" will center the check on the event being triggered (because all the instructions are written at event level), but you want a very specific event: event nº 1, from the lot of events.
Code:
$game_map.events.values.select{|event| event.id.between?(2, 50)}.any?{|event| in_radius?($game_map.events[1][0], event, radius)}

you can compact $game_map.events in it's own object, since it's *the same* object.
Code:
eventhash.values.select{|event| event.id.between?(2, 50)}.any?{|event| in_radius?(eventhash[1][0], event, radius)}

that returns a true/false, because of the any?() block, but there's no way *that* will fit in the conditional, so pack it as a function.
Code:
def check_for_event(eventhash, radius)
eventhash.values.select{|event| event.id.between?(2, 50)}.any?{|event| in_radius?(eventhash[1][0], event, radius)}
end

and pass the objects you replaced, as parameters.
Code:
Conditional Branch: Script: check_for_event($game_map.events, 5)
>
Else
>
End

Code:
check_for_event(batch, value)
-> def check_for_event(eventhash = batch, radius = value)
  -> |event| in_radius?(eventhash[1][0], event, radius)
    -> def in_radius(orig = eventhash[1][0], targ = event, rad = radius)
      -> targ.position - orig.position < radius? -> return boolean
    -> return boolean
  ->return boolean
->return boolean
check_for_event(batch, value) => boolean.
whatever the value of boolean is, it's decided by in_radius(), based on batch and value, passed down from way above.
Thank you very much for the explanation. This will be helpfull in more advanced scripting for me, but at the moment its difficult to wrap my head around it. That will hopefully change in the future, with other stuff it did.
nothing.

in_radius() and check_for_events() are functions declared to be used within that event, when needed.
the one thing you need, is an answer to the question of "is anyone within range?".... that's what the condition is there to process.
the answer to that question requires two parameters: what range, and within whose reach.... and "within whose reach", in this case, requires another process, because "who" is an event, and events are stored in a collection of events, and from that collection you need both one very specific event, and a lot of specific events from a potentially larger lot which is of no interest to you.
grab the lot.
Code:
$game_map.events

from the lot, grab the range of events you care about
$game_map.events is a hash, so if you call events[X], it'll return [event], an array of length 1, so you go directly to the part of the hash storing the events, and pull out the container, whole: $game_map.events.values.
from there, filter by ID.
Code:
$game_map.events.values.select{|event| event.id.between?(2, 50)}

with the filtered lot, check for the range.
Code:
$game_map.events.values.select{|event| event.id.between?(2, 50)}.any?{|event| in_radius?(self, event, radius)}

"self" will center the check on the event being triggered (because all the instructions are written at event level), but you want a very specific event: event nº 1, from the lot of events.
Code:
$game_map.events.values.select{|event| event.id.between?(2, 50)}.any?{|event| in_radius?($game_map.events[1][0], event, radius)}

you can compact $game_map.events in it's own object, since it's *the same* object.
Code:
eventhash.values.select{|event| event.id.between?(2, 50)}.any?{|event| in_radius?(eventhash[1][0], event, radius)}

that returns a true/false, because of the any?() block, but there's no way *that* will fit in the conditional, so pack it as a function.
Code:
def check_for_event(eventhash, radius)
eventhash.values.select{|event| event.id.between?(2, 50)}.any?{|event| in_radius?(eventhash[1][0], event, radius)}
end

and pass the objects you replaced, as parameters.
Code:
Conditional Branch: Script: check_for_event($game_map.events, 5)
>
Else
>
End

Code:
check_for_event(batch, value)
-> def check_for_event(eventhash = batch, radius = value)
  -> |event| in_radius?(eventhash[1][0], event, radius)
    -> def in_radius(orig = eventhash[1][0], targ = event, rad = radius)
      -> targ.position - orig.position < radius? -> return boolean
    -> return boolean
  ->return boolean
->return boolean
check_for_event(batch, value) => boolean.
whatever the value of boolean is, it's decided by in_radius(), based on batch and value, passed down from way above.

Hey man thanks so much!!!

This entire script thing completely works!

But, If I get in battle with an enemy, and defeat him by my crystal, the event is still considered "there" so it won't let me heal.

Maybe on defeat, is there a script call I can use to move him back to his regular position on the map? Like from where he started?
I defined an 'if erased?' function and set that to determine if an event has been erased in an old script I used to re spawn enemies.

So maybe I could just insert the 'if erased, then go back to where you spawned' or something?
*that* will require a switch, or a self switch; because you'd be trying to control an event from another event or from the battle window.
but yes, you can do it.

trigger the battle, and on the win or lose blocks do the same check, but different:
*grab* whatever event is within radius.
Code:
lot = $game_map.events.values.select{|event| in_radius?(self, event, 5)}
and clear them
Code:
lot.each{|event| event.erase}
lot = nil
or, set them to an alternate page
Code:
lot.each{|event| $game_self_switches[[$game_map, event.id, "A"]] = true}
and let each event handle themselves.

add a check to the previous lot grab to skip any erased events
Code:
def check_for_event(eventhash, radius)
 eventhash.values.select{|event| event.id.between?(2, 50) && !event.erased?}.any?{|event| in_radius?(eventhash[1], event, radius)}
end
The Event which triggered the fight is allready running, he just need to put some eventcommand in the if statement for the BATTLE WIN.
Use Eventcommand: "Set Movement Route",choose "This Event" and use the Command "Script"
Code:
moveto(@event.x , @event.y)
This would teleport the Enemy Event back to its origin editor map position.
Or
You just use an extra condition to check a self switch to see if the Enemy is alive or dead, so your Crystal can still work when dead enemy is nearby.
The Event which triggered the fight is allready running, he just need to put some eventcommand in the if statement for the BATTLE WIN.
Use Eventcommand: "Set Movement Route",choose "This Event" and use the Command "Script"
Code:
moveto(@event.x , @event.y)
This would teleport the Enemy Event back to its origin editor map position.
Or
You just use an extra condition to check a self switch to see if the Enemy is alive or dead, so your Crystal can still work when dead enemy is nearby.

Thank you so much for your help.

I've got it working now. I appreciate both of you!!

Original thread: https://forums.rpgmakerweb.com/threads/is-there-any-way-to-check-and-see-if-other-events-are-around.104859/