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.

[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

jnpyWoc.jpg

Lh9JmzN.jpg

gmkJfKG.jpg

Demo

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

Script

Code:
class Game_Event

  def proximity_self_switch(switch,proximity=1,only_affect_id=[],protect_ids=[],value=true)

    #$DEBUG ? print(switch.to_s) : switch = switch

    #convert switch integer to string

    #Set Special Parameters

    block_proximity = false

    a = ["A","B","C","D"]

    #b = ["Afalse","Bfalse", "Cfalse","Dfalse"]

    #c = ["Ablock","Bblock", "Cblock","Dblock"]

    if proximity.is_a?(Array)

      block_proximity = true

    end

    if switch.is_a?(String)

      if switch.include?("false")

        value = false

      end

      if switch.include?("block")

        block_proximity = true

      end

      for i in 0...a.size

        if switch.include?(a[i])

          switch = a[i]

        end

      end

    end

    if switch.is_a?(Integer)

      if switch == 0

        switch = "A"

      elsif switch == 1

        switch = "B"

      elsif switch == 2

        switch = "C"

      elsif switch >= 3

        switch = "D"

      end

    end

    #preset parameters

    only_affect_id != [] ? singular_affect = true : singular_affect = false

    protect = false

    if protect_ids != []

      singular_affect = false

      protect = true

    end

    #convert switch to array or integer

    switch_id = switch

    #Find characters around self

    affected_events = []

    for i in 0...($game_map.events.size+1)

      if $game_map.events[i].is_a?(Game_Event) && $game_map.events[i].id != @id

        if block_proximity == false

          x = $game_map.events[i].x

          y = $game_map.events[i].y

          range_x = x - @x 

          range_y = y - @y

          range = Math.hypot(range_x, range_y).abs

          if range <= proximity

            affected_events.push($game_map.events[i].id)

          end

        else

          x1 = proximity[0][0]

          x2 = proximity[1][0]

          y1 = proximity[0][1]

          y2 = proximity[1][1]

          event_x = $game_map.events[i].x

          event_y = $game_map.events[i].y

          if event_x >= x1 && event_x <= x2

            if event_y >= y1 && event_y <= y2

              affected_events.push($game_map.events[i].id)

            end

          end

        end

      end

    end

    #print(affected_events.to_s)

    #turn on array self switch

    affected_events.each { |affected_ids|

    if singular_affect == true && only_affect_id.include?(affected_ids)

      key = [@map_id, affected_ids, switch_id]

      $game_self_switches[key] = value

      $game_map.refresh

    end

    if protect == true && !protect_ids.include?(affected_ids)

      key = [@map_id, affected_ids, switch_id]

      $game_self_switches[key] = value

      $game_map.refresh

    end

    if singular_affect == false && protect == false

      key = [@map_id, affected_ids, switch_id]

      $game_self_switches[key] = value

      $game_map.refresh

    end

    }

  end

end

Instructions

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

Understanding the Script Call:
Understanding the Script Call:
Code:
s=$game_map.events[@event_id]

s.proximity_self_switch(switch,proximity,affect_ids,protect_ids,value)

Code:
s=$game_map.events[@event_id]

s.proximity_self_switch("A",1,[],[],true)

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:
Code:
s=$game_map.events[@event_id]

s.proximity_self_switch("A")

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

Code:
s=$game_map.events[@event_id]

b1 = [8,19]

b2 = [12,29]

s.proximity_self_switch("A",[b1,b2])

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.


Code:
s=$game_map.events[22]

g=[]

p=[5,2,10]

s.proximity_self_switch("C",21,g,p)

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.
 

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