[XP] Proximity/Block Self Switch Activation

Proximity/Block Self Switch Activation Version: 1
By: Creative_AUG

Introduction

I don't know if people still use RPG Maker XP, but I'm posting this script because I was doing a passion project (honestly the reason why the script is sloppy) and thought this script might be useful to anyone still using the program.

This script basically allows you to turn on self switches of any event within a specified radius or block through another event's script call. I originally created this script to increase the collision size of an event. For example: Imagine a two tile hallway. placing an invisible, secondary event next to a primary so that you can't walk passed the primary event without triggering it. This allows you to cut down on usage of the ingame switches.

I've since added a lot of add-ons (see feature list) to the script to serve my own purposes, which allows this script to be very versatile and useful for creating more complex puzzles and dungeons. I'm excited to post this and maybe get feedback on how to make it better and excited to see if it can be used in other people's projects in ways I can't imagine.

Features
  • Turn off or on Event Self-Switches within a given proximity(radius) of an event.
  • Turn off or on Event Self-Switches within a given block (x and y values)
  • Protect Events so that their self-switch won't turn on (within the radius given)
  • Or Choose to only affect certain events (within the radius given)

Screenshots

Content Hidden Spoiler
Image
Image
Image


Demo

N/A sorry. I will try to post one if people are interested.

Script

script Spoiler
  1. class Game_Event
  2.   def proximity_self_switch(switch,proximity=1,only_affect_id=[],protect_ids=[],value=true)
  3.     #$DEBUG ? print(switch.to_s) : switch = switch
  4.     #convert switch integer to string
  5.     #Set Special Parameters
  6.     block_proximity = false
  7.     a = ["A","B","C","D"]
  8.     #b = ["Afalse","Bfalse", "Cfalse","Dfalse"]
  9.     #c = ["Ablock","Bblock", "Cblock","Dblock"]
  10.     if proximity.is_a?(Array)
  11.       block_proximity = true
  12.     end
  13.     if switch.is_a?(String)
  14.       if switch.include?("false")
  15.         value = false
  16.       end
  17.       if switch.include?("block")
  18.         block_proximity = true
  19.       end
  20.       for i in 0...a.size
  21.         if switch.include?(a[i])
  22.           switch = a[i]
  23.         end
  24.       end
  25.     end
  26.     if switch.is_a?(Integer)
  27.       if switch == 0
  28.         switch = "A"
  29.       elsif switch == 1
  30.         switch = "B"
  31.       elsif switch == 2
  32.         switch = "C"
  33.       elsif switch >= 3
  34.         switch = "D"
  35.       end
  36.     end
  37.     #preset parameters
  38.     only_affect_id != [] ? singular_affect = true : singular_affect = false
  39.     protect = false
  40.     if protect_ids != []
  41.       singular_affect = false
  42.       protect = true
  43.     end
  44.     #convert switch to array or integer
  45.     switch_id = switch
  46.     #Find characters around self
  47.     affected_events = []
  48.     for i in 0...($game_map.events.size+1)
  49.       if $game_map.events[i].is_a?(Game_Event) && $game_map.events[i].id != @id
  50.         if block_proximity == false
  51.           x = $game_map.events[i].x
  52.           y = $game_map.events[i].y
  53.           range_x = x - @x
  54.           range_y = y - @y
  55.           range = Math.hypot(range_x, range_y).abs
  56.           if range <= proximity
  57.             affected_events.push($game_map.events[i].id)
  58.           end
  59.         else
  60.           x1 = proximity[0][0]
  61.           x2 = proximity[1][0]
  62.           y1 = proximity[0][1]
  63.           y2 = proximity[1][1]
  64.           event_x = $game_map.events[i].x
  65.           event_y = $game_map.events[i].y
  66.           if event_x >= x1 && event_x <= x2
  67.             if event_y >= y1 && event_y <= y2
  68.               affected_events.push($game_map.events[i].id)
  69.             end
  70.           end
  71.         end
  72.       end
  73.     end
  74.     #print(affected_events.to_s)
  75.     #turn on array self switch
  76.     affected_events.each { |affected_ids|
  77.     if singular_affect == true && only_affect_id.include?(affected_ids)
  78.       key = [@map_id, affected_ids, switch_id]
  79.       $game_self_switches[key] = value
  80.       $game_map.refresh
  81.     end
  82.     if protect == true && !protect_ids.include?(affected_ids)
  83.       key = [@map_id, affected_ids, switch_id]
  84.       $game_self_switches[key] = value
  85.       $game_map.refresh
  86.     end
  87.     if singular_affect == false && protect == false
  88.       key = [@map_id, affected_ids, switch_id]
  89.       $game_self_switches[key] = value
  90.       $game_map.refresh
  91.     end
  92.     }
  93.   end
  94. end
Expand to see the code.


Instructions

This may be a little complicated. the script call is to be used inside an event Script Call:

Understanding the Script Call:
Content Hidden Spoiler
Understanding the Script Call:
  1. s=$game_map.events[@event_id]
  2. s.proximity_self_switch(switch,proximity,affect_ids,protect_ids,value)
Expand to see the code.


  1. s=$game_map.events[@event_id]
  2. s.proximity_self_switch("A",1,[],[],true)
Expand to see the code.


the first line calls the event that you're calling the script from.
the second line uses that event to turn on the switches that you want (the actual script)
(It's broken up to prevent the script call from going to into the next line. You may have to learn how to use temporary variables such as s is in the example script call)

@event_id is the event id in which the radius of the script call is coming from. If it isn't changed, the event id will be the event id of the event you call the script from. If you change it, the radius will come from the event with the id you specify.

switch is the switch that you want to turn on or off. There are multiple ways to use this parameter.
"A" - Turns A on. (works with "B","C", and "D")
"Afalse" - Turns A off. (works with "Bfalse", "Cfalse" and "Dfalse")
0 - Turns A on (works with 1 as "B", 2 as "C" and 3 as "D")

proximity is an optional parameter. If nothing is there, the only events that will be affected are those right next to the event. There are multiple ways to use this parameter.
A number - sets a radius around the event you call the script from.
An Array [[x1,y1],[x2,y2]] - This will only affect events within the x and y value block that is given.

affect_ids is an optional parameter. If ids are given, only the events with specified ids will be affected. If nothing is there, the script will assume all events within radius or block should be affected.
affect_ids should be used as an array. For example putting [10,25,8] in this parameter will make sure that only the three events with the ids; 10, 25, and 8 will be affected by the script call

protect_ids is an optional parameter. If ids are given, affect_ids will be ineffective. It will also protect the events with the event ids given from the affects of the proximity self switch script call. If nothing is there, the script will assume all events within radius or block should be affected.

value is an optional parameter. if nothing is put there or true is written, the script will assume that you want the given self switch to be turned on. If false is written, the script will turn the self switch off. This parameter is ineffective if "false" is written in the switch parameter.



How I've used the script call:
Content Hidden Spoiler
  1. s=$game_map.events[@event_id]
  2. s.proximity_self_switch("A")
Expand to see the code.


This code will turn on the A self switch for an event within a 1 tile radius.


Content Hidden Spoiler
  1. s=$game_map.events[@event_id]
  2. b1 = [8,19]
  3. b2 = [12,29]
  4. s.proximity_self_switch("A",[b1,b2])
Expand to see the code.


This code will turn on the A self switch for all events between map coordinates (8,19) and (12,29)
b1 sets the top left barrier of the block.
b2 sets the lower right barrier of the block.
if the values were to be reversed the script would exclude the block and affect everything else.



Content Hidden Spoiler
  1. s=$game_map.events[22]
  2. g=[]
  3. p=[5,2,10]
  4. s.proximity_self_switch("C",21,g,p)
Expand to see the code.


This code will turn on the C self switch around all events within a 21 tile radius of EVENT 22. The code will protect events with the ids: 5,2 and 10. g specifies that I don't want any events to be the only events affected. p specifies that I want to protect the events inside of the array.


I hope these examples help. I will try to capture screenshots right after I post this.


FAQ

N/A

Compatibility

I don't use the SDK or anything special so I'm not 100% sure if it's compatible within of them. This should just be a simple add-on that should work with anything.

Credits and Thanks

I was studying scripts that were useful in the making of this one. ForeverZer0's Proximity Icons. literally copied and pasted a line from his script. "range = Math.hypot(range_x, range_y).abs" It also taught me how to use the "each" command.

Author's Notes

-Posting Screenshots as soon as I can.

Terms and Conditions

If any puzzle games are made with this script I'd love to play them. That's all.
Don't worry, XP is easily the biggest engine here. :thumb:

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=78891