Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[REVOLVER]Need a method; Algorithm provided

Well, actually I need two, BUT THEY ARE VERY SIMILAR. They must be written for RPG Maker XP.

I'm afraid I don't know RGSS, but I do know how programming works, so I am able to provide the algorithms necessary in pseudocode.

Baisically, the first method, takes a value n, calculates X and Y distance between a method and the player, and will return TRUE if the player is within n the modulus of X and Y distance of the event (I am aware that this will form a square, but I'm okay with that)(Modulus, in case you don't know, is the positive value, so 1 is one and -1 is 1. Don't confuse with MOD, ie %). So the method call will have three inputs, the X and Y of a method and value n. I will be using this in the Conditional Branch of RMXP.

The second method is exactly the same, except that the X and Y values of the event are added together before comparing it to n, thus forming a Diamond area of detection. It'll make more sense once you look at the pseudocode.

Square Algorithm;
Code:
Square(Event, n){

  Return((|Player.X-Event.X|>= n )AND((|Player.Y-Event.Y|>= n ));

}
Diamond Algorithm;
Code:
Diamond(Event, n){

  Return(( |Player.X-Event.X| + |Player.Y-Event.Y| ) >= n);

}

Thankyou very much :D
 
Try this...

Square Algorithm;
Code:
def square(event_x, event_y, n)

  return ($game_player.x - event_x).abs <= n  and ($game_player.y - event_y).abs <= n

end
Diamond Algorithm;
Code:
def diamond(event_x, event_y, n)

  return ($game_player.x - event_x).abs + ($game_player.y - event_y).abs <= n)

end
 
I would just put them in a module, so...

Code:
module WITHIN_RANGE

  def self.square(event_x, event_y, n)

    return (($game_player.x - event_x).abs <= n  and ($game_player.y - event_y).abs <= n)

  end

 

  def self.diamond(event_x, event_y, n)

    return (($game_player.x - event_x).abs + ($game_player.y - event_y).abs <= n)

  end

end

then use... (event commands)

Control Variables: [0001] = This event's Map X
Control Variables: [0002] = This event's Map Y
Conditional Branch: Script: RANGE::square($game_variables[1], $game_variables[2], 5)
<whatever happens here>
:Branch End
Wait: 4 frame(s)

change the '5' to the range you want
 
Isn't there a shortcut way, in the script call, to use the same X and Y values as the event that's calling it? Or am I making that up?

Also, It's giving me a syntax Error on Line 3...

EDIT: Nevermind, fixed it. The code was missing some brakets, but maybe that's cause you edited my pseudocode and I left out some brackets or summat...
 
yep, I did. :scruff: I fixed the code above too.

I also changed the module name, after I remembered there's already a RANGE module (for arrays)

I'd also put a 'wait' of at least 4 frames to reduce lag. (it takes at least 4 frames to move one tile)

[edit] You could use $game_map.events[@event_id].x instead of the game variables, so...

WITHIN_RANGE::square($game_map.events[@event_id].x, $game_map.events[@event_id].y, 3)

then you don't need to set the game variables
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top