Is there a distance variable?

● ARCHIVED · READ-ONLY
Started by CrazyCrab 6 posts View original ↗
  1. Hi everyone,

    In my game I want to have a couple of events that trigger as you approach things and while I know the inefficient way of doing that - spam

    player touch events all around the map, I'd rather use something like a distance variable as it's far less messy and way more practical.

    Is it possible? I used Game Maker before and I would just calculate the distance between the two instances, I'm guessing that there might be a way of doing that for player + event that doesn't require heavy scripting.

    Thanks!
  2. It doesn't require heavy scripting, but you can always use the classic distance formula:

    sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))

    Where you replace (x1 y1) with the coordinates of the first event, and (x2 y2) with the coordinates of the second event.

    If you do this, update the distance calculation every time your target event moves.  It would be very CPU intensive and unnecessary to recalculate this every update cycle.
  3. No easy way to do it without scripting.  It's not as hard as you might imagine to do the scripting for, though.  Have a parallel process event run every few frames that compares your x and y coordinates against each event that you want to trigger the events for.

    The simplest way is to use ($game_player.x - $game_map.events[n]).abs where n is the Event ID of the event you want to check the player's distance from.  The Game_Character class gives you some more refined ways to do this, like calling the distance_x_from method, in case you're using options like Wrap Horizontally.

    If you want to measure the exact distance, use the pythagorean theorem to get the square root of the square of the difference of x coordinates, and the square of the difference of y coordinates, compare that to the maximum distance at which the event will trigger, and trigger it if applicable.  If you want to use a cruder "tile-based" distance, just add the difference of x's and difference of y's together to compare to the max distance at which the event will trigger.
  4. you have to calculate the distance yourself, based on position (that can be set into variables for player and any event) and pythagoras.


    However, that doesn't trigger anything - you still need a trigger (or a parallel process creating its own lag)


    Edit: double ninja'd ;-)
  5. Great, thanks for all the replies.

    I'll experiment with these for a bit and report back when something goes wrong.  ;)
  6. Look at the Game_Event.near_the_player? method - might give you some ideas.


    Otherwise, use Whitesphere's formula to judge the distance, but instead of finding the square root of the result (which is the CPU heavy part), instead square the distance you're interested in and compare that.


    So if you're after something 3 tiles away, you'd do this:


    if (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) <= 9