Restrict event from going offscreen

I have an event that the player is able to take control of (i.e. when the player pushes Left, the event moves left, and so on), while the player character and other events remain unmoving (Escape key exits this process). It's working fine, but there's one thing I need to tweak, and I haven't been able to manage it through eventing.

I would like to prevent the controllable event from going offscreen; e.g. if it's at the top of the screen and the player presses Up, even if there's more map in that direction, the event does not move. Obviously I can restrict the controllable event's movement based on tiles/regions/other events/etc., but what I need is a restriction based on the current edges of the screen.

Thank you for reading my request!
mathematics

you'll use four control variables to get the position of both the player and the event, then compare how far they are away before deciding to allow movement or not.

It's a bit tricky if the player is near the end of the map (because then his coordinates are off-center), but it can be done this way.
mathematics

you'll use four control variables to get the position of both the player and the event, then compare how far they are away before deciding to allow movement or not.

It's a bit tricky if the player is near the end of the map (because then his coordinates are off-center), but it can be done this way.

Thanks for the reply. I can make it work with variables if the player character is centered onscreen, but I cannot for the life of me figure out how to get it to work if they aren't. Any suggestions?
First you'll need to check if the player is offcenter, either by comparing coordinates to map size or by checking screen coordinates. Using screen coordinates is easier for the check, but even in that case you'll need the coordinate difference to add to the distance allowed for the events move - so it might be better to use those coordinates directly.

Two things to keep in mind when doing this:
1) you only need to limit the opposite direction if the player is off-center near the map edge. because where he is nearer to the map edge, the event can't leave the map anyway and doesn't need extra calculation. That spares you half the effort on special number calculation.
2) on two sides (toward the lower map coordinates) you can not only identify the offscreen by the coordinates being smaller than the minimum center position, you can also simply addthe difference between center and true coordinate to the mathematical distance limit.

Actually, I think it would be easier to use the display X and display Y instead of the player position.
$gameMap.displayX()
$gameMap.displayY()
Those 2 methods gives you the upper left tile of the current viewport.

EDIT: Sorry, didn't notice it's VX Ace instead of MV. It's been a long time since I've programmed in Ruby RGSS3, so I don't really remember the functions. It's probably something like $game_map.display_x or something similar. It would be nice if someone checks.

EDIT 2: Acutally I think I got it right.
$game_map.display_x and $game_map.display_y
Andar, thank you so much for your input. I think I have this figured out now.

Acutally I think I got it right.
$game_map.display_x and $game_map.display_y

I checked, and that is correct, MushroomCake28. I don't know that I'm going to use it here, but it's nice to have in case I need it in future. Thanks!

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/restrict-event-from-going-offscreen.96383/