Chaos Project

RPG Maker => RPG Maker Scripts => RMXP Script Database => Topic started by: G_G on June 06, 2011, 02:19:50 am

Title: [XP] More Self-Switches
Post by: G_G on June 06, 2011, 02:19:50 am
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




Screenshots

Spoiler: ShowHide
(http://decisive-media.net/gameguy/switch.png)



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 (http://rmrk.net/index.php/topic,42822.new.html#new)


Instructions

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


Compatibility

Not tested with SDK.
Should work with anything.


Credits and Thanks




Author's Notes

Enjoy! :)
Title: Re: [XP] More Self-Switches
Post by: G_G on June 06, 2011, 03:38:22 am
Erm, sorry for the double post, updated the script, forgot a couple of things in the script. :S
Title: Re: [XP] More Self-Switches
Post by: LiTTleDRAgo on June 06, 2011, 08:48:24 am
... 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'
Title: Re: [XP] More Self-Switches
Post by: G_G on June 06, 2011, 11:42:54 am
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.
Title: Re: [XP] More Self-Switches
Post by: G_G on July 29, 2011, 04:49:47 am
Fixed a bug where event wouldn't go to correct page on map transfers.
Title: Re: [XP] More Self-Switches
Post by: Treebonesteak on July 24, 2012, 01:39:02 pm
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)
Title: Re: [XP] More Self-Switches
Post by: KK20 on July 24, 2012, 05:05:35 pm
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?'.
Title: Re: [XP] More Self-Switches
Post by: Treebonesteak on July 25, 2012, 11:58:20 am
Thanks! It works :D
Title: Re: [XP] More Self-Switches
Post by: G_G on July 25, 2012, 03:53:56 pm
Thanks KK20 for fixing that.
Title: Re: [XP] More Self-Switches
Post by: G_G on September 21, 2013, 04:05:38 pm
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.
Title: Re: [XP] More Self-Switches
Post by: LiTTleDRAgo on September 21, 2013, 07:54:25 pm
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
Title: Re: [XP] More Self-Switches
Post by: G_G on September 21, 2013, 08:13:09 pm
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.
Title: Re: [XP] More Self-Switches
Post by: 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.

Title: Re: [XP] More Self-Switches
Post by: G_G on September 23, 2013, 10:46:22 am
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.
Title: Re: [XP] More Self-Switches
Post by: Blizzard on September 23, 2013, 02:53:16 pm
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.