Check Player Collision/Location, Compare to every event around player problem

Hi there,

I have an interesting problem that I can't find a quick solution to but hoping there is a quick solution in a way I may not have thought of. I'm using MV and have quite a few plugins and have ways to trigger the self switches albeit I have to create a few hundred which i'm trying to avoid.

One map is like a simcity sort of build. So imagine you have a city but you're able to call in say an asteroid to hit it. There are a lot of events on the map that (I cannot move or teleport other stuff in when it hits the ground). So I need something that will detect how many events are around the point of impact (say 4x4 squares) and activate the self switches on those events.

If possible, i'd rather have a way for the game to recognize collision as the player collider can be changed and may make it easier, but events only trigger when the player moves so it doesn't work in this aspect sadly or it would've solved everything for me. It makes no sense to make everything blow up only when the player moves manually after.

One of the conditional branches i'm using, is below to check player and events. However really hoping someone may have a better way than me making the game run a parallel and checking over a 100 of these scripts
$gameMap.isCollision(id,id)

Thanks for your time!
Assuming your 4x4 would actually be a 5x5 (since the event itself is the point of impact and thus you can't have a square centred on it that's an even number) you could do something like this:

Code:
var impactRadius = 4;
var affectedEvents = $gameMap.events().filter(function(event) { return Math.abs($gameMap.deltaX($gamePlayer.x, event.x)) <= impactRadius / 2 && Math.abs($gameMap.deltaY($gamePlayer.y, event.y)) <= impactRadius / 2 });
affectedEvents.forEach(function(event) { $gameSelfSwitches.setValue([$gameMap.mapId(), event.eventId(), 'A'], true)});

upload_2019-4-11_11-10-41.png


As you can see, when I run this using the player's X/Y (you'd have to change that in the code for the X/Y of the asteroid event) only the events in a 5x5 square around the player are knocked down.
You are amazing! Thank you soooooooooooooooo much!

Is there a way to change the radius into say a box shape and define x and y to be different lengths apart? So say if I needed the 1st corner of the box to be 2 squares higher and left of the player, it then stretches 5 squares across to the right, and the bottom stretches down for 6 squares.

And lastly, what would I need to change if I wanted an event to do this instead of a player in future?
Yup, ultimately there is no alternative but to loop/iterate: over all map events (checking position) or over all collision coordinates (checking event presence). I'd suggest the second option, since it probably minimises the number of checks. 😮‍💨

The asteroid launch is an event, right? So let's check the event explosions etc once, when it lands. I would suggest a parallel event (activated on launch, deactivated after impact), so that the player controls aren't locked between launch and impact:
Code:
◆Comment:Time between launching and landing
◆Wait:180 frames
◆Comment:BOOM now lets check which events were hit
Then if you're using self-switches you may as well go for a full-scripted solution like Trihan suggests. 🙏

Alternatively, if you're not keen on scripting, you can use Loop/Break Loop commands, additional game variables as loop iterators, and the Get Location Info event command to check the event ID at given coordinates (OK as long as you don't have multiple explodable events on one tile). You will still need a script call or plugin command to remotely toggle another event's self-switch, though. 🙂

One more observation: by default every self-switch toggle causes the map to refresh. This will probably be the biggest impact on framerate. There's probably some way to work around it if it becomes an issue, e.g. set the self-switch data directly and manually call a map refresh afterwards.
You are amazing! Thank you soooooooooooooooo much!

Is there a way to change the radius into say a box shape and define x and y to be different lengths apart?

And lastly, what would I need to change if I wanted an event to do this instead of a player in future?

If you want X and Y to be different sizes, just change impactRadius to 2 variables (like impactX and impactY) and replace the references to impactRadius in the comparison statements to the appropriate variable name.

If you want an event to check the impact instead of the player, just change $gamePlayer.x and $gamePlayer.y to the X and Y of the event. How you retrieve that will depend on where exactly this code is located, but you can do $gameMap.events()[id of asteroid event].x and $gameMap.events()[id of asteroid event].y from anywhere.
If you want X and Y to be different sizes, just change impactRadius to 2 variables (like impactX and impactY) and replace the references to impactRadius in the comparison statements to the appropriate variable name.

If you want an event to check the impact instead of the player, just change $gamePlayer.x and $gamePlayer.y to the X and Y of the event. How you retrieve that will depend on where exactly this code is located, but you can do $gameMap.events()[id of asteroid event].x and $gameMap.events()[id of asteroid event].y from anywhere.

I gave it a shot (I can only read/understand coding a bit sadly and maths is not my strong suit) and got the variables right I think. The picture attached is of the below code in use and does 4 on each side of the player. How would I be able to make only the Left or Right side of the player trigger the switches? Or from starting in the top left corner wall and ending at the player? Guessing i'd have to make an event tele there then change the $gameMap.events()[id] x/y to that of the event instead? I tried different maths symbols instead of <= but it horribly goes wrong

var impactX = 4;
var impactY = 0;
var affectedEvents = $gameMap.events().filter(function(event) { return Math.abs($gameMap.deltaX($gamePlayer.x, event.x)) <= impactX &&
Math.abs($gameMap.deltaY($gamePlayer.y, event.y)) <= impactY});
affectedEvents.forEach(function(event) { $gameSelfSwitches.setValue([$gameMap.mapId(), event.eventId(), 'A'], true)});

Thanks very much for your help again!
You don't need an event if it's going to start at the top left: you can just filter every event that's either to the left of or above the player.

Making it only affect the left/right side is fairly simple, but I assume this will be affected by the direction the player is facing, yes?
You don't need an event if it's going to start at the top left: you can just filter every event that's either to the left of or above the player.

Making it only affect the left/right side is fairly simple, but I assume this will be affected by the direction the player is facing, yes?

Whoops sorry! I'm not being too clear sorry i'm thinking from the perspective of one of the movement scripts for colliders how you can set it not directly over the player. So for instance if I wanted to make a whole bunch of meteors hit the ground around the player from a specific angle and not directly on top. Every road, building etc is an event so there's hundreds onscreen.

H34Qpos.png


Player direction isn't a matter, just if it's possible as it'll help me understand the code more as there's more than one event I would like to use it for throughout the game and some are different sizes. Say a laser beam for instance.
Argh! I've tried to edit this code so much but I can't get it to work right or it does something really weird. Would really appreciate any help still thanks!

Original thread: https://forums.rpgmakerweb.com/threads/check-player-collision-location-compare-to-every-event-around-player-problem.107835/