[XP] Event Range Conditions

Started by ForeverZer0, May 02, 2011, 12:37:27 am

Previous topic - Next topic

ForeverZer0

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


  • Conditions based on distance radius

  • Conditions based on if they are ON/OFF screen

  • Conditions based on linear distance on each axis

  • Conditions based on whether they are above, below, left, or right in relation to player or other event

  • Simple to use




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


  • ForeverZer0, for the script




Author's Notes

Please report any bugs/issues you find.
Hope you enjoy!
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Shalaren

"#   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.

ForeverZer0

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.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Sacred Nym

I love you. No homo.

That is all.
Quote昨日の自分に「さようなら」
Say "Goodbye" to who you were yesterday.

yuhikaru

Whoa, that's a real time saver, thank you! 
If it interests anyone, aparently works fine with cogwheel's pixel movement :)

Susanm

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...

ForeverZer0

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
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Susanm

Thanks a bunch! It worked -)  kudos!  :shy:  now u have a new version LoL :haha:

xTsukihime

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?

ForeverZer0

Just use the inverse condition on the page that is activated by the first page.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

xTsukihime

Got it. Didn't know "not" would do the trick.  :)

horus02

November 02, 2011, 04:11:34 am #11 Last Edit: November 05, 2011, 10:05:56 am by horus02
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

Vexus

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.
Current Project/s:

ForeverZer0

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 or this variation of it that allows for using event names.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Vexus

December 13, 2011, 10:03:09 pm #14 Last Edit: December 13, 2011, 10:14:06 pm by Vexus
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
Current Project/s:

ForeverZer0

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.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Vexus

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.
Current Project/s:

ForeverZer0

Create an event that tracks the coordinates of the first event and have the second pathfind to it.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Vexus

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 :).
Current Project/s:

ForeverZer0

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.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.