Chaos Project

RPG Maker => RPG Maker Scripts => RMXP Script Database => Topic started by: ForeverZer0 on May 02, 2011, 12:37:27 am

Title: [XP] Event Range Conditions
Post by: ForeverZer0 on May 02, 2011, 12:37:27 am
Event Range Conditions
Authors: ForeverZer0
Version: 1.1
Type: Event Add-On
Key Term: Game Utility



Introduction

Allows you to set up conditional branches in events that will be based off the event's coordinates in relation to the player's coordinates without having to create any variables for the X and Y of each.


Features




Screenshots

None.


Demo

None.


Script

Click here for the script.
Spoiler: ShowHide

#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
# Event Range Conditions
# Author: ForeverZer0
# Date: 5.1.2011
# Version: 1.1
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
#
# Explanation:
#
#   Allows you to set up conditional branches in events that will be based off
#   the event's coordinates in relation to the player's coordinates without
#   having to create any variables for the X and Y of each.
#
#   Here are the new features you can use a conditional branch. Just type these
#   into the script box for the branch.
#
#   range?(RANGE, EVENT_ID) - Will be true if player is anywhere in the radius
#                             defined by RANGE from the event with EVENT_ID
#
#   on_screen?(EVENT_ID)    - Will be true if the event with EVENT_ID is within
#                             the visible screen
#
#   x_dist?(DIST, EVENT_ID) - Returns true if the player's x/y is within DIST
#            OR               of event's x/y with EVENT_ID. These are absolute
#   y_dist?(DIST, EVENT_ID)   values, meaning it doesn't matter which direction,
#                             it just uses the total distance in tiles for that
#                             axis. Use a DIST of 0 to check if that axis is
#                             equal.
#
#   player_above?(EVENT_ID) - Returns true when player is above event.
#   player_below?(EVENT_ID) - Returns true when player is below event.
#   player_right?(EVENT_ID) - Returns true when player is right of the event.
#   player_left?(EVENT_ID)  - Returns true when player is left of the event.
#
#   For all of these, if the conditional branch that is using them is within
#   the event that it applies to, you do not have to include the EVENT_ID, it
#   is assumed to be that event's ID unless otherwise defined.
#
#   You can use these as a condition for just about anything, such as having
#   an event say something, run away, or run toward the player if it is within a
#   specific distance, and it's much easier than using multiple branches and
#   game variables to set it up.
#
#   Remember that if you use a range condition with a parallel trigger, it will
#   continue to execute as long as the condition is met and if the player cannot
#   move during the event's code, the game will effectively be stuck.
#
# Compatability:
#
#   - You may encounter issues if using a Pixel Movement script, though it
#     should still work fine with an 8-Way Movement script. (Not tested)
#
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:

class Interpreter
 
  def range?(range = 4, id = @event_id)
    e = $game_map.events[id]
    radius = (Math.hypot((e.x - $game_player.x), (e.y - $game_player.y))).abs
    return (radius <= range)
  end
 
  def on_screen?(id = @event_id)
    x, y = $game_map.events[id].real_x, $game_map.events[id].real_y
    return ((x - $game_map.display_x + 64) / 4).between?(0, 640) &&
      ((y - $game_map.display_y) / 4).between?(0, 480)
  end
 
  def x_dist?(distance = 0, id = @event_id)
    x_dif = ($game_map.events[id].x - $game_player.x).abs
    return (x_dif <= distance)
  end
 
  def y_dist?(distance = 0, id = @event_id)
    y_dif = ($game_map.events[id].y - $game_player.y).abs
    return (y_dif <= distance)
  end
 
  def player_above?(id = @event_id)
    return ($game_map.events[id].y > $game_player.y)
  end
 
  def player_below?(id = @event_id)
    return ($game_map.events[id].y < $game_player.y)
  end
 
  def player_right?(id = @event_id)
    return ($game_map.events[id].x < $game_player.x)
  end
 
  def player_left?(id = @event_id)
    return ($game_map.events[id].x > $game_player.x)
  end
end



Instructions

Place script anywhere below the Interpreter scripts, and above "Main"
The different script calls are listed within the script.


Compatibility

Possible compatibility issues with pixel-movement scripts.


Credits and Thanks




Author's Notes

Please report any bugs/issues you find.
Hope you enjoy!
Title: Re: [XP] Event Range Conditions
Post by: Shalaren on May 02, 2011, 10:18:33 pm
"#   Remember that if you use a range condition with a parallel trigger, it will
#   continue to execute as long as the condition is met and if the player cannot
#   move during the event's code, the game will effectively be stuck."
I dont get that part S:, the game is stuck for me when I put a range condition, I cant move the player, I dont really get what is cousing it, S:.
can you explain that part just alittle bit better? like an example of what can cause that.
Title: Re: [XP] Event Range Conditions
Post by: ForeverZer0 on May 02, 2011, 10:27:17 pm
ex.

You have an event with a parallel trigger. There is "range?(4)" condition that will show a text box, which the player cannot move while it is displayed. If you do not set a switch use a self switch or something to stop this checking this condition, the second the player exits the message, the event condition is re-checked, and since the player is still in the same location since control was lost, the condition is still true and the message pops up again. It will continue to do this.

It can easily be stopped by a self-switch or something to switch to stop checking after it is first executed, you just need to be conscious  of these possible scenarios when building your events.
Title: Re: [XP] Event Range Conditions
Post by: Sacred Nym on May 03, 2011, 03:05:58 am
I love you. No homo.

That is all.
Title: Re: [XP] Event Range Conditions
Post by: yuhikaru on May 19, 2011, 08:14:03 am
Whoa, that's a real time saver, thank you! 
If it interests anyone, aparently works fine with cogwheel's pixel movement :)
Title: Re: [XP] Event Range Conditions
Post by: Susanm on August 10, 2011, 05:15:21 am
range?(RANGE, EVENT_ID) 

I know that this line only checks for if EVENT_ID is in defined range distance away from PLAYER then it returns true.... Is there a way to use this line or perhaps another line in the script so it can be use for EVENT_ID checks another EVENT_ID if within range and not the player? Im trying to make an event check the distance range with another event and if within specified range between both events then condition is true and will say "Hi!" between both events...
Title: Re: [XP] Event Range Conditions
Post by: ForeverZer0 on August 10, 2011, 07:26:04 pm
Add this method to the script.  Its pretty self-explanatory, just use this call:

event_range?(EVENT1_ID, EVENT2_ID, RANGE)

  def event_range?(event1_id, event2_id, range)
    e1, e2 = $game_map.events[event1_id], $game_map.events[event2_id]
    radius = (Math.hypot((e1.x - e2.x), (e1.y - e2.y))).abs
    return (radius <= range)
  end
Title: Re: [XP] Event Range Conditions
Post by: Susanm on August 10, 2011, 08:22:45 pm
Thanks a bunch! It worked -)  kudos!  :shy:  now u have a new version LoL :haha:
Title: Re: [XP] Event Range Conditions
Post by: xTsukihime on September 28, 2011, 03:53:54 pm
When the player approaches an event, a self-switch will be set.
When the player moves away from the event, the self-switch should be turned off.

Currently the first page uses range condition to activate the self-switch, but then I do all sorts of things just to get the self to turn off when I move away.

Is there an easy way to deal with this?
Title: Re: [XP] Event Range Conditions
Post by: ForeverZer0 on September 28, 2011, 04:31:23 pm
Just use the inverse condition on the page that is activated by the first page.
Title: Re: [XP] Event Range Conditions
Post by: xTsukihime on September 28, 2011, 05:44:21 pm
Got it. Didn't know "not" would do the trick.  :)
Title: Re: [XP] Event Range Conditions
Post by: horus02 on November 02, 2011, 04:11:34 am
Hello, I'm kinda new and a total scripting Noob...
I wanted to use your Range Conditions with your Custom Event Triggers (http://forum.chaos-project.com/index.php/topic,6893.0.html) but it never works. I'm too dumb to get it to run I think. Could you please tell me how I should set it up? I tried something like this:

Comment: Custom Trigger
           : player_above?(14)

But it always gives me an error like this: "undefined method `player_above?ยด for ..."

edit
I got it to work for me.
/edit
Title: Re: [XP] Event Range Conditions
Post by: Vexus on December 13, 2011, 07:31:46 pm
I'm using this great script in my project and it's doing wonders! Can make events conflict with other events  :evil:

Anyway would it be possible to make an event follow/chase another event?

It would be very great if it would be possible.

Thanks for the help.
Title: Re: [XP] Event Range Conditions
Post by: ForeverZer0 on December 13, 2011, 07:58:41 pm
I suppose you could use this for chasing, though it would require a bit of an expansive event. The easiest way would be to have an event set a variable determining the direction it is from the second event, then branch to determine what direction to make the chasing event turn, then use "One Step Forward".

Or you can just use a pathfinding script like this (http://forum.chaos-project.com/index.php/topic,9784.0.html) or this (http://forum.chaos-project.com/index.php/topic,10661.0.html) variation of it that allows for using event names.
Title: Re: [XP] Event Range Conditions
Post by: Vexus on December 13, 2011, 10:03:09 pm
Ok thanks for the help will try one of them tomorrow.

(Trying to make the wildlife fight each other if x beast gets near y beast territories, etc)

Ok tried the variation one using the example in the thread and getting an error like the last guy posted on the thread without getting a response :S
Title: Re: [XP] Event Range Conditions
Post by: ForeverZer0 on December 13, 2011, 11:49:27 pm
The first is my original script. The second link is my script which someone else modified, and the error appears to be with his edits. You can always use the original, just won't be able to call using the event's name.
Title: Re: [XP] Event Range Conditions
Post by: Vexus on December 14, 2011, 07:07:29 am
I could but I didn't understand how to do it on your version (Even on the other one if it wasn't for the example he gave) as the description doesn't clearly say how to make x event follow y event but only go on z location or maybe I got it wrong don't know.
Title: Re: [XP] Event Range Conditions
Post by: ForeverZer0 on December 14, 2011, 12:32:01 pm
Create an event that tracks the coordinates of the first event and have the second pathfind to it.
Title: Re: [XP] Event Range Conditions
Post by: Vexus on December 14, 2011, 08:37:43 pm
But the type I wanted to do was not on an event that stood still waiting for the other event to reach it that's the problem.

Say for example there's a cat on the fields and a mouse gets too close the cat starts chasing it but the mouse won't stand still and that script (Checked the demo) only makes x event/player move to y coordinates.

Anyways if it's too trouble no need to do it and thanks for the help nonetheless :).
Title: Re: [XP] Event Range Conditions
Post by: ForeverZer0 on December 14, 2011, 11:01:17 pm
Its a single parallel process event and two variables. I didn't think it was too difficult unless you are just misunderstanding what I suggested.
Title: Re: [XP] Event Range Conditions
Post by: Vexus on December 15, 2011, 06:57:04 am
Yes and sorry for flooding your thread with something about this script.

I don't really get what you mean and I don't use much variables usually it's from tutorials or something as I kinda get confused on them.
Title: Re: [XP] Event Range Conditions
Post by: Vexus on February 01, 2012, 04:20:51 am
Sorry foreverzer0 for bugging you again for this script.

The only way to make the conditions is by parallel process for the events to spot you when your near? because I tried making custom movement and adding the line of script but after a second of playtesting I get an error undefined method range? for game_event:0x9288f50.

Having parallel process for every npc would probably lag the game.
Title: Re: [XP] Event Range Conditions
Post by: Poe on February 01, 2012, 03:53:35 pm
Quote from: Vexus on February 01, 2012, 04:20:51 am
Sorry foreverzer0 for bugging you again for this script.

The only way to make the conditions is by parallel process for the events to spot you when your near? because I tried making custom movement and adding the line of script but after a second of playtesting I get an error undefined method range? for game_event:0x9288f50.

Having parallel process for every npc would probably lag the game.

script lines in custom move route don't use interpreter methods.

put it in game_map for instance to call it with $game_map.range?(range,id) like this:
class Game_Map
 
 def range?(range = 4, id = @event_id)
   e = $game_map.events[id]
   radius = (Math.hypot((e.x - $game_player.x), (e.y - $game_player.y))).abs
   return (radius <= range)
 end
end

just add this below the script, however it won't automatically put in the event's id anymore so you'll always have to add it.
Title: Re: [XP] Event Range Conditions
Post by: ForeverZer0 on February 01, 2012, 05:23:58 pm
class Game_Character
 
  def range?(range = 4, id = @id)
    e = $game_map.events[id]
    radius = (Math.hypot((e.x - $game_player.x), (e.y - $game_player.y))).abs
    return (radius <= range)
  end
end


Add that, and you can should be able to use the method normally in move routes.  * I think *
Title: Re: [XP] Event Range Conditions
Post by: Vexus on February 01, 2012, 05:25:31 pm
Thanks will try it out hopefully it works.
Title: Re: [XP] Event Range Conditions
Post by: Zexion on August 04, 2013, 10:02:43 am
Dang, I was hoping that this was made by someone else :s Well anyone can answer it though.
In the features section of the post, it says
"Conditions based on whether they are above, below, left, or right in relation to player or other event"
But I don't see anything in the script about checking an event's range to x event.

By simply using this script is this possible?
Title: Re: [XP] Event Range Conditions
Post by: LiTTleDRAgo on August 04, 2013, 06:11:53 pm
Spoiler: ShowHide
#==============================================================================
# ** Game_Character
#------------------------------------------------------------------------------
#  This class deals with characters. It's used as a superclass for the
#  Game_Player and Game_Event classes.
#==============================================================================
class Game_Character
  #--------------------------------------------------------------------------
  # * New method: character_above?
  #--------------------------------------------------------------------------
  def character_above?(character)
    return (character.y > @y)
  end
  #--------------------------------------------------------------------------
  # * New method: character_below?
  #--------------------------------------------------------------------------
  def character_below?(character)
    return (character.y < @y)
  end
  #--------------------------------------------------------------------------
  # * New method: character_right?
  #--------------------------------------------------------------------------
  def character_right?(character)
    return (character.x < @x)
  end
  #--------------------------------------------------------------------------
  # * New method: character_left?
  #--------------------------------------------------------------------------
  def character_left?(character)
    return (character.x > @x)
  end
end


event1 = get_character(1) # event with ID 1
event2 = get_character(2) # event with ID 2
$game_switches[1] = event1.character_above?(event2)