[XP] More Self-Switches

Started by G_G, June 06, 2011, 02:19:50 am

Previous topic - Next topic

G_G

June 06, 2011, 02:19:50 am Last Edit: December 06, 2014, 09:47:04 pm by gameus
More Self-Switches
Authors: game_guy
Version: 1.5
Type: Event Modification
Key Term: Environment Add-on



Introduction

Ever need more than 4 self switches? With this small script, you can now have as many self switches you want. You aren't just limited to letters either. You can have names for them.


Features


  • More Self Switches

  • Name them whatever

  • Access/toggle them from wherever




Screenshots

Spoiler: ShowHide



Demo

N/A


Script

Spoiler: ShowHide
Code: ruby

#===============================================================================
# More Self-Switches
# Version 1.5
# Author game_guy
#-------------------------------------------------------------------------------
# Intro:
# Ever need more than 4 self switches? With this small script, you can now
# have as many self switches you want. You aren't just limited to letters
# either. You can have names for them.
#
# Features:
# -More Self Switches
# -Name them whatever
# -Access/toggle them at anytime
#
# Instructions:
# -First, lets create a self switch for our event. Anywhere in the event, add
# a comment and type this,
# Switch:switch_name.
# switch_name can be whatever you want to name the switch. Thats all you have
# to do to create a self switch for that page.
# There cannot be any spaces between the colon and the switch name.
# e.g. Switch: switch - Does not work.
# e.g. Switch:switch - Does work.
#
# -Now to turn this switch of or on, call this in a script call.
# self_switch("switch", true/false)
# switch is the switch name, this must be in double " " or single ' ' quotes.
# true/false tells the script whether to turn it off or on. true = on,
# false = off.
#
# -If you want to see if a self switch is on/off, use this script in a
# condtional branch.
# self_switch_state("switch") == true/false
# switch is the switch name, this must be in double " " or single ' ' quotes.
# true = on, false = off
#
# -Now you can toggle switches/check switch state of any event.
#  Just use these script calls:
#  self_switch("switch", true/false, event_id)
#  self_switch_state("switch", event_id)
#  The previous script calls still work without event_id, it just works within the event then.
#
# Compatibility:
# Not tested with SDK.
# Should work with anything.
#
# Credits:
# game_guy ~ For creating it.
# Thanks to KK20 for a Bug Fix
#===============================================================================

$gg_self_switches = 1.5

class Game_Event < Game_Character
 alias gg_init_more_switches_lat initialize
 def initialize(map_id, event)
   gg_init_more_switches_lat(map_id, event)
   @event.pages.each {|page| page.list.each {|command|
     if [108, 408].include?(command.code)
       command.parameters.each {|p| check_custom_switch(page, p) }
     end
   }}
   refresh
 end
 def check_custom_switch(page, code)
   a = code.split(':')
   return if a[0].nil?
   if a[0].downcase == "switch" && a[1] != nil
     page.condition.self_switch_ch = a[1]  
     page.condition.self_switch_valid = true
   end
 end
end

class Interpreter
 def self_switch(switch, state, event_id = @event_id)
   key = [$game_map.map_id, event_id, switch]
   $game_self_switches[key] = state
   $game_map.need_refresh = true
 end
 def self_switch_state(switch, event_id = @event_id)
   key = [$game_map.map_id, event_id, switch]
   return $game_self_switches[key]
 end
end


VX Version Here


Instructions

In the script.
Place above main, below Scene_Debug, the normal.


Compatibility

Not tested with SDK.
Should work with anything.


Credits and Thanks


  • game_guy ~ For creating it.

  • Thanks to KK20 for a Bug Fix




Author's Notes

Enjoy! :)

G_G

Erm, sorry for the double post, updated the script, forgot a couple of things in the script. :S

LiTTleDRAgo

... umm, I don't see how this will be useful, :???:
after all we can use self switches unlimitedly

$game_self_switches[[$game_map.map_id, @event_id, 'STRING']] = true


and it's not just 'A', 'B', 'C', 'D'

G_G

June 06, 2011, 11:42:54 am #3 Last Edit: June 06, 2011, 02:59:15 pm by Captain Falcon
Yea but then people would have to go through the work of figuring out how to set the event's page to that switch. This makes it easier for those non scripters to create new self switches, set them on and off, and set an events page to that self switch.

EDIT:
While this is true:
$game_self_switches[[$game_map.map_id, @event_id, 'STRING']] = true

People with little to no scripting knowledge would not know how to set an event's page to that custom switch they just turned on. This makes it easy because all you have to do is place a comment in the page.
Switch:custom_switch

And bam, that event's page self switch is set to "custom_switch".
Then you just call this:
self_switch("custom_switch", true)

To turn that self switch on.

G_G

Fixed a bug where event wouldn't go to correct page on map transfers.

Treebonesteak

I need help D:

I used this script after I build a couple of maps.
The script works fine, but on only one Map it always gives me a error message:

QuoteScript 'More Self Switches' line 59: NoMethodError occurred.
undefined Method 'downcase' for nil:NilClass


Hope you can help me  :uhm:

(But all in all it works great :D)

KK20

The reason for the error is that you have a line break in one of your comments. Example is
This is my comment.

I just made a line break.

Here's a fix for that:
Spoiler: ShowHide
class Game_Event < Game_Character
  alias gg_init_more_switches_lat initialize
  def initialize(map_id, event)
    gg_init_more_switches_lat(map_id, event)
    @event.pages.each {|page| page.list.each {|command|
      if [108, 408].include?(command.code)
        command.parameters.each {|p| check_custom_switch(page, p) }
      end
    }}
    refresh
  end
  def check_custom_switch(page, code)
    a = code.split(':')
    return if a[0].nil?
    if a[0].downcase == "switch" && a[1] != nil
      page.condition.self_switch_ch = a[1] 
      page.condition.self_switch_valid = true
    end
  end
end

class Interpreter
  def self_switch(switch, state)
    if @event_id > 0
      key = [$game_map.map_id, @event_id, switch]
      $game_self_switches[key] = state
    end
    $game_map.need_refresh = true
  end
  def self_switch_state(switch)
    key = [$game_map.map_id, @event_id, switch]
    return $game_self_switches[key]
  end
end


To G_G (who just recently changed his name?  :???:) the fix is in 'def check_custom_switch'. I added a 'return if a[0].nil?'.





   


   
   


   
   

Other Projects
RPG Maker XP AceUpgrade RMXP to RMVXA performance!
XPA TilemapTilemap rewrite with many features, including custom resolution!



Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Treebonesteak


G_G


G_G

As requested by Heretic, you can now change the switches and get the switches current state from anywhere. e.g. In event id 5, you can check for event id 6's self switches now.

LiTTleDRAgo

there is a mistake in your code

Spoiler: ShowHide
Quote
class Interpreter
  def self_switch(switch, state)
    self_switch(switch, state, @event_id) if @event_id > 0
  end
  def self_switch(switch, state, event_id)
    key = [$game_map.map_id, event_id, switch]
    $game_self_switches[key] = state
    $game_map.need_refresh = true
  end
  def self_switch_state(switch)
    return self_switch_state(switch, @event_id)
  end
  def self_switch_state(switch, event_id)
    key = [$game_map.map_id, event_id, switch]
    return $game_self_switches[key]
  end
end


I believe you want it to be like this

Spoiler: ShowHide
Quote
class Interpreter
  def self_switch(switch, state, event_id = @event_id)
    key = [$game_map.map_id, event_id, switch]
    $game_self_switches[key] = state
    $game_map.need_refresh = true
  end
  def self_switch_state(switch, event_id = @event_id)
    key = [$game_map.map_id, event_id, switch]
    return $game_self_switches[key]
  end
end

G_G

September 21, 2013, 08:13:09 pm #11 Last Edit: September 21, 2013, 08:14:29 pm by gameus
Yeah, forgot that works. Sorry, been working with C#, and before .NET 4.0, you can't have default arguments. Still, the code works lol.

EDIT: Nonetheless, fixed.

ForeverZer0

Quote from: gameus on September 21, 2013, 08:13:09 pm
...been working with C#, and before .NET 4.0, you can't have default arguments...


Not true, works in .NET 2.0.  I haven't used any version lower than that, but it's been around for a while.

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.

G_G

Oh really?
Quote from: ForeverZer0 on September 23, 2013, 07:18:34 am
Quote from: gameus on September 21, 2013, 08:13:09 pm
...been working with C#, and before .NET 4.0, you can't have default arguments...


Not true, works in .NET 2.0.  I haven't used any version lower than that, but it's been around for a while.




I'll rephrase it then. Anything before Visual Studio 2010 e.g. before the existence of C# 4.0, you could not do it. I don't care for Visual Studio 2010, and I have difficulties getting XNA to work properly in 2012, so I tend to work with 2008 when I'm working with XNA.

Blizzard

You are making the mistake that C# are .NET version are not the same. gameus is right, C# is v4.0 in VS 2010 and since then they have added default arguments, regardless of whether you use .NET v2.0 or v4.0.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.