Chaos Project

RPG Maker => RPG Maker Scripts => Script Requests => Topic started by: Parkman202 on January 23, 2015, 11:30:51 pm

Title: Switchy-Thing Script Request
Post by: Parkman202 on January 23, 2015, 11:30:51 pm
Hello, everyone! I'm making a game that requires a system that is a little... different. I'll just list it all out because I would ramble on about what it would need.  :wacko:

I need a script that can

- Skip the title screen
- Save switches so, when their state would change from ON/OFF to OFF/ON anywhere in the game, it would save it even when they would close and re-open the game and start at the skipped title screen.

I don't know if this is possible (as to me, it sounds pretty impossible), but if it is, I'll definitely need some help.

Thank you!
Title: Re: Switchy-Thing Script Request
Post by: KK20 on January 24, 2015, 05:30:20 pm
There should be scripts for the first one already.

I don't quite understand the second one entirely. Give an example of its use.
Title: Re: Switchy-Thing Script Request
Post by: G_G on January 24, 2015, 07:37:51 pm
I think all he basically needs is a script that will skip the titlescreen if a save file exists, then load it. That would solve both things he's asking for.
Title: Re: Switchy-Thing Script Request
Post by: ForeverZer0 on January 24, 2015, 08:13:44 pm
Just would need to have the game save everytime a switch is saved, or at least $game_switches.
Title: Re: Switchy-Thing Script Request
Post by: Parkman202 on January 26, 2015, 06:38:26 pm
Quote from: KK20 on January 24, 2015, 05:30:20 pm
I don't quite understand the second one entirely. Give an example of its use.


Let me try and explain.

I would like it to, when the user would save, it would save the activated switches into some sort of file that the game could read from when opening it.

An example would be as follows:

Say that, by skipping the title screen, it would make the player go to my custom title screen. Then let's say the player does something in the game that turned on a switch. This switch makes something on the custom menu change, be it a button or an art aspect. Now, after the event that calls the switch has ended, the player decides to close the game. This means that the change to the title screen is not present due to the fact that when the player begins the game again, they are started at a point in the game (the title screen map with the player's starting position) where the switch isn't identified as 'ON'. Would there be a way for specific switches to be saved outside of the game when they are triggered and when the game would start, have that list of switches be defined as 'ON'?
Title: Re: Switchy-Thing Script Request
Post by: ForeverZer0 on January 26, 2015, 11:00:32 pm
Sounds like all you have to do is save the switches data when they are changed, not the whole game. This is a simple thing. Does your game allow the player to choose the save file to save, or just automatically pick the file to save to?
Title: Re: Switchy-Thing Script Request
Post by: Parkman202 on January 26, 2015, 11:05:51 pm
Quote from: ForeverZer0 on January 26, 2015, 11:00:32 pm
Sounds like all you have to do is save the switches data when they are changed, not the whole game. This is a simple thing. Does your game allow the player to choose the save file to save, or just automatically pick the file to save to?


It allows the player to save to any file.

I should also mention that I am running on RPG Maker XP.
Title: Re: Switchy-Thing Script Request
Post by: KK20 on January 26, 2015, 11:20:26 pm
Okay so let me ask a few questions.

1) Do the switches' states carry over to the other files or the new game?
2) Are the switch states saved when the player saves their game OR the instance any switch changes?
3) Do you only care about a certain range of switches or ALL switches?

Basically the idea I have is to make another "save file" (rxdata file) created depending on what you say to question 2 and have the game load this file every time the title screen is reached.
Title: Re: Switchy-Thing Script Request
Post by: ForeverZer0 on January 27, 2015, 12:36:45 am
See how this works, I barely tested it, so I will leave that up to you.

Spoiler: ShowHide
#==============================================================================
# ** Game_Temp
#------------------------------------------------------------------------------
#  This class handles temporary data that is not included with save data.
#  Refer to "$game_temp" for the instance of this class.
#==============================================================================

class Game_Temp
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor :save_filename
  attr_accessor :save_data
end

#==============================================================================
# ** Game_Switches
#------------------------------------------------------------------------------
#  This class handles switches. It's a wrapper for the built-in class "Array."
#  Refer to "$game_switches" for the instance of this class.
#==============================================================================

class Game_Switches
  #--------------------------------------------------------------------------
  # * Set Switch (alias method)
  #     switch_id : switch ID
  #     value     : ON (true) / OFF (false)
  #--------------------------------------------------------------------------
  alias zer0_set_switch []=
  def []=(switch_id, value)
    # Original method
    zer0_set_switch(switch_id, value)
    # Return if game has not yet been saved
    return if $game_temp.save_data.nil?
    # Change $game_switches only to last saved data
    $game_temp.save_data[3] = self
    # Save game, with only change being $game_switches
    File.open($game_temp.save_filename, 'wb') {|file|
      $game_temp.save_data.each {|data| Marshal.dump(data, file) }
    }
  end
end

#==============================================================================
# ** Scene_Save
#------------------------------------------------------------------------------
#  This class performs save screen processing.
#==============================================================================

class Scene_Save
 
  #--------------------------------------------------------------------------
  # * Decision Processing (alias method)
  #--------------------------------------------------------------------------
  alias zer0_on_decision on_decision
  def on_decision(filename)
    zer0_on_decision(filename)
    # Store last used save file
    $game_temp.save_filename = filename
  end
  #--------------------------------------------------------------------------
  # * Write Save Data (alias method)
  #     file : write file object (opened)
  #--------------------------------------------------------------------------
  alias zer0_write_save_data write_save_data
  def write_save_data(file)
    # Original method
    zer0_write_save_data(file)
    # Store characters
    characters = []
    for i in 0...$game_party.actors.size
      actor = $game_party.actors[i]
      characters.push([actor.character_name, actor.character_hue])
    end
    # Store current data
    $game_temp.save_data = [characters, Graphics.frame_count, $game_system,
      $game_switches, $game_variables, $game_self_switches, $game_screen,
      $game_actors, $game_party, $game_troop, $game_map, $game_player]
  end
end
Title: Re: Switchy-Thing Script Request
Post by: Parkman202 on January 27, 2015, 01:06:07 pm
I need to try and explain this a little better.

I don't need it to do anything when I save manually. I just need it to save certain switches into a file (.rxdata or .txt; whichever works better) when they are turned to 'ON'. The game should then load the file once Scene_Map is loaded in "New Game", and since the title is skipped, "New Game" is loaded instantly.

I hope I discribed it a little better; I am kind of making a very odd request.  :uhm:
Title: Re: Switchy-Thing Script Request
Post by: ForeverZer0 on January 27, 2015, 02:24:00 pm
That doesn't affect saving the game normally, but neither does it do exactly what you want since all switches are saved and you only need specific ones.
Title: Re: Switchy-Thing Script Request
Post by: Parkman202 on January 27, 2015, 02:40:49 pm
Well, I'm not a good script reader, but if it saves all switches, it can work. I'll test it when I get home. That's for scripting that, though!
Title: Re: Switchy-Thing Script Request
Post by: Parkman202 on January 27, 2015, 07:51:28 pm
Quote from: ForeverZer0 on January 27, 2015, 12:36:45 am
See how this works, I barely tested it, so I will leave that up to you.

Spoiler: ShowHide
#==============================================================================
# ** Game_Temp
#------------------------------------------------------------------------------
#  This class handles temporary data that is not included with save data.
#  Refer to "$game_temp" for the instance of this class.
#==============================================================================

class Game_Temp
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor :save_filename
  attr_accessor :save_data
end

#==============================================================================
# ** Game_Switches
#------------------------------------------------------------------------------
#  This class handles switches. It's a wrapper for the built-in class "Array."
#  Refer to "$game_switches" for the instance of this class.
#==============================================================================

class Game_Switches
  #--------------------------------------------------------------------------
  # * Set Switch (alias method)
  #     switch_id : switch ID
  #     value     : ON (true) / OFF (false)
  #--------------------------------------------------------------------------
  alias zer0_set_switch []=
  def []=(switch_id, value)
    # Original method
    zer0_set_switch(switch_id, value)
    # Return if game has not yet been saved
    return if $game_temp.save_data.nil?
    # Change $game_switches only to last saved data
    $game_temp.save_data[3] = self
    # Save game, with only change being $game_switches
    File.open($game_temp.save_filename, 'wb') {|file|
      $game_temp.save_data.each {|data| Marshal.dump(data, file) }
    }
  end
end

#==============================================================================
# ** Scene_Save
#------------------------------------------------------------------------------
#  This class performs save screen processing.
#==============================================================================

class Scene_Save
 
  #--------------------------------------------------------------------------
  # * Decision Processing (alias method)
  #--------------------------------------------------------------------------
  alias zer0_on_decision on_decision
  def on_decision(filename)
    zer0_on_decision(filename)
    # Store last used save file
    $game_temp.save_filename = filename
  end
  #--------------------------------------------------------------------------
  # * Write Save Data (alias method)
  #     file : write file object (opened)
  #--------------------------------------------------------------------------
  alias zer0_write_save_data write_save_data
  def write_save_data(file)
    # Original method
    zer0_write_save_data(file)
    # Store characters
    characters = []
    for i in 0...$game_party.actors.size
      actor = $game_party.actors[i]
      characters.push([actor.character_name, actor.character_hue])
    end
    # Store current data
    $game_temp.save_data = [characters, Graphics.frame_count, $game_system,
      $game_switches, $game_variables, $game_self_switches, $game_screen,
      $game_actors, $game_party, $game_troop, $game_map, $game_player]
  end
end



It didn't work. I think what I am asking may be very difficult if possible at all. It's hard enough to explain. >.<
Title: Re: Switchy-Thing Script Request
Post by: KK20 on January 29, 2015, 03:34:56 pm
Well I wrote this with my best understanding of the request.

#==============================================================================
# ** C O N F I G U R A T I O N
#==============================================================================
module SwitchSaving
 
  # Choose switch IDs that you want applied upon starting the game. Can choose
  # individual IDs or a range of them (i.e. x..y).
  APPLY_CHANGES = [1, 2, 3, 5..10, 90]
 
  # Switch states will be saved to this file. Extension (.rxdata) not needed.
  FILENAME = "Switch_States"

end
#==============================================================================
# ** Game_Switches
#------------------------------------------------------------------------------
#  This class handles switches. It's a wrapper for the built-in class "Array."
#  Refer to "$game_switches" for the instance of this class.
#==============================================================================
class Game_Switches
  #--------------------------------------------------------------------------
  # * Set Switch
  #     switch_id : switch ID
  #     value     : ON (true) / OFF (false)
  #--------------------------------------------------------------------------
  alias save_switches_upon_set []=
  def []=(switch_id, value)
    save_switches_upon_set(switch_id, value)
    # If setting a switch to TRUE
    if value
      file = File.open(SwitchSaving::FILENAME + ".rxdata","wb")
      Marshal.dump($game_switches, file)
      file.close
    end
   
  end
end
#==============================================================================
# ** Scene_Title
#------------------------------------------------------------------------------
#  This class performs title screen processing.
#==============================================================================
class Scene_Title
  #--------------------------------------------------------------------------
  # * Command: New Game
  #--------------------------------------------------------------------------
  alias load_game_switches_on_new_game command_new_game
  def command_new_game
    load_game_switches_on_new_game
    if FileTest.exist?(SwitchSaving::FILENAME + ".rxdata")
      file = File.open(SwitchSaving::FILENAME + ".rxdata","rb")
      switches = Marshal.load(file)
      SwitchSaving::APPLY_CHANGES.each{|id|
        if id.is_a?(Range)
          id.each{|i| $game_switches[i] = switches[i] }
        else
          $game_switches[id] = switches[id]
        end
      }
    end
   
  end
end

You did say "only save when a switch turns ON", so if you want it to save whenever a switch changes, just comment out:

    #if value
      file = File.open(SwitchSaving::FILENAME + ".rxdata","wb")
      Marshal.dump($game_switches, file)
      file.close
    #end
Title: Re: Switchy-Thing Script Request
Post by: ForeverZer0 on January 29, 2015, 04:16:53 pm
I still think the save file would be a better choice. All someone has to do is delete your switch file to reset everything. If it is contained within the save file, they cannot without deleting their progress, but it really just depends on what the feature is used for.
Title: Re: Switchy-Thing Script Request
Post by: Parkman202 on January 29, 2015, 04:18:53 pm
It does exactly as I need. However, I don't need it to look for 'Scene_Title'. As I had mentioned, I have a script that skips the title, so it needs to call 'Scene_Map' instead. I'll try and play with it a little, but here is my title skipping script.

Spoiler: ShowHide


#==============================================================================
# Xenres' Title Skip
# Version 1.0
# 2010-11-24
#------------------------------------------------------------------------------
#  Loads necessary objects and skips to the start map to use as a title screen
#  or whatever.
#==============================================================================

class Scene_Title
 #--------------------------------------------------------------------------
 # * Main Processing
 #--------------------------------------------------------------------------
 def main
   # If battle test
   if $BTEST
     battle_test
     return
   end
   # Load database
   $data_actors        = load_data("Data/Actors.rxdata")
   $data_classes       = load_data("Data/Classes.rxdata")
   $data_skills        = load_data("Data/Skills.rxdata")
   $data_items         = load_data("Data/Items.rxdata")
   $data_weapons       = load_data("Data/Weapons.rxdata")
   $data_armors        = load_data("Data/Armors.rxdata")
   $data_enemies       = load_data("Data/Enemies.rxdata")
   $data_troops        = load_data("Data/Troops.rxdata")
   $data_states        = load_data("Data/States.rxdata")
   $data_animations    = load_data("Data/Animations.rxdata")
   $data_tilesets      = load_data("Data/Tilesets.rxdata")
   $data_common_events = load_data("Data/CommonEvents.rxdata")
   $data_system        = load_data("Data/System.rxdata")
   # Make system object
   $game_system = Game_System.new    
   # Make each type of game object
   $game_temp          = Game_Temp.new
   $game_system        = Game_System.new
   $game_switches      = Game_Switches.new
   $game_variables     = Game_Variables.new
   $game_self_switches = Game_SelfSwitches.new
   $game_screen        = Game_Screen.new
   $game_actors        = Game_Actors.new
   $game_party         = Game_Party.new
   $game_troop         = Game_Troop.new
   $game_map           = Game_Map.new
   $game_player        = Game_Player.new
   
   # Set up initial party
   $game_party.setup_starting_members
   # Set up initial map position
   $game_map.setup($data_system.start_map_id)
   # Move player to initial position
   $game_player.moveto($data_system.start_x, $data_system.start_y)
   # Refresh player
   $game_player.refresh
   # Run automatic change for BGM and BGS set with map
   $game_map.autoplay
   # Update map (run parallel process event)
   $game_map.update
   # go to REAL title screen
   $scene = Scene_Map.new
 end
end

Title: Re: Switchy-Thing Script Request
Post by: KK20 on January 29, 2015, 06:05:59 pm
In that case, replace Scene_Title in my code to this:

#==============================================================================
# ** Scene_Title
#------------------------------------------------------------------------------
#  This class performs title screen processing.
#==============================================================================
class Scene_Title
  #--------------------------------------------------------------------------
  # * Command: New Game
  #--------------------------------------------------------------------------
  alias load_game_switches_on_new_game main
  def main
    load_game_switches_on_new_game
    if FileTest.exist?(SwitchSaving::FILENAME + ".rxdata")
      file = File.open(SwitchSaving::FILENAME + ".rxdata","rb")
      switches = Marshal.load(file)
      SwitchSaving::APPLY_CHANGES.each{|id|
        if id.is_a?(Range)
          id.each{|i| $game_switches[i] = switches[i] }
        else
          $game_switches[id] = switches[id]
        end
      }
    end
   
  end
end

@F0: But how would the game know which save file to look at? If it was just one save file, I'd agree with you.
Title: Re: Switchy-Thing Script Request
Post by: Parkman202 on January 29, 2015, 06:24:11 pm
IT WORKS! IT WORKS! Thank you so much! I didn't even know this would have been possible!

I'll make sure to credit you in the game!

QuoteAll someone has to do is delete your switch file to reset everything.


Simple! Just rename this:

FILENAME = "Switch_States"


To something official-ish to scare the player away:

FILENAME = "SaveGame"

Title: Re: Switchy-Thing Script Request
Post by: ForeverZer0 on January 30, 2015, 12:29:30 am
Renaming a file does not prevent it from being deleted, not even a little bit. But as I said, it depends if this would even matter or not.
Title: Re: Switchy-Thing Script Request
Post by: Parkman202 on January 30, 2015, 01:06:10 pm
I meant that it could scare the player because they would think it would be the save file interpreter or something.