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.

[Resolved] Loading game variables from file1

Hi, what I'm looking for is a way to load all the variables from a save file if that file exist.
The reason why is I'm working on making a local auction house; the player can sell something to the auction house and load another save file and buy it from the auction house for his/her other character.

I did find this script:
Code:
module Switch_Load
  def self.load_switches
    if FileTest.exist?("Save1.rxdata")
      file = File.open("Save1.rxdata", "r")
    else
      return
    end
    # Read character data for drawing save file
    junk_characters = Marshal.load(file)
    # Read frame count for measuring play time
    junk_frame_count = Marshal.load(file)
    # Read each type of game object
    junk_system        = Marshal.load(file)
    $game_switches      = Marshal.load(file)
    file.close
  end
end
 
Then call it with Switch_Load.load_switches
found it from: http://www.rmxp.org/forums/index.php?topic=47186.0

but when I try to use it like so:
Code:
module Switch_Load
  def self.load_switches
    if FileTest.exist?("Saves/Save1.ckd")
      file = File.open("Saves/Save1.ckd", "r")
    else
      return
    end
    # Read character data for drawing save file
    junk_characters = Marshal.load(file)
    # Read frame count for measuring play time
    junk_frame_count = Marshal.load(file)
    # Read each type of game object
    junk_system        = Marshal.load(file)
    $game_variables      = Marshal.load(file)
    file.close
  end
end
inserted over main

Code:
#==============================================================================
# ** Scene_Title
#------------------------------------------------------------------------------
#  This class performs title screen processing.
#==============================================================================

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 title graphic
    @sprite = Sprite.new
    @sprite.bitmap = RPG::Cache.title($data_system.title_name)
    # Make command window
    s1 = "New Game"
    s2 = "Continue"
    s3 = "Shutdown"
    @command_window = Window_Command.new(192, [s1, s2, s3])
    @command_window.back_opacity = 160
    @command_window.x = 320 - @command_window.width / 2
    @command_window.y = 288
    # Continue enabled determinant
    # Check if at least one save file exists
    # If enabled, make @continue_enabled true; if disabled, make it false
    @continue_enabled = false
    for i in 0..3
      if FileTest.exist?("Saves/Save#{i+1}.ckd")
        @continue_enabled = true
      end
    end
    # If continue is enabled, move cursor to "Continue"
    # If disabled, display "Continue" text in gray
    if @continue_enabled
      @command_window.index = 1
    else
      @command_window.disable_item(1)
    end
    # Play title BGM
    $game_system.bgm_play($data_system.title_bgm)
    # Stop playing ME and BGS
    Audio.me_stop
    Audio.bgs_stop
    # Execute transition
    Graphics.transition
    # Main loop
    loop do
      # Update game screen
      Graphics.update
      # Update input information
      Input.update
      # Frame update
      update
      # Abort loop if screen is changed
      if $scene != self
        break
      end
    end
    # Prepare for transition
    Graphics.freeze
    # Dispose of command window
    @command_window.dispose
    # Dispose of title graphic
    @sprite.bitmap.dispose
    @sprite.dispose
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Update command window
    @command_window.update
    # If C button was pressed
    if Input.trigger?(Input::C)
      # Branch by command window cursor position
      case @command_window.index
      when 0  # New game
        command_new_game
      when 1  # Continue
        command_continue
      when 2  # Shutdown
        command_shutdown
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Command: New Game
  #--------------------------------------------------------------------------
  def command_new_game
    # Play decision SE
    $game_system.se_play($data_system.decision_se)
    # Stop BGM
    Audio.bgm_stop
    # Reset frame count for measuring play time
    Graphics.frame_count = 0
    # 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
    Switch_Load.load_switches
    # 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
    # Switch to map screen
    $scene = Scene_Map.new
  end
  #--------------------------------------------------------------------------
  # * Command: Continue
  #--------------------------------------------------------------------------
  def command_continue
    # If continue is disabled
    unless @continue_enabled
      # Play buzzer SE
      $game_system.se_play($data_system.buzzer_se)
      return
    end
    # Play decision SE
    $game_system.se_play($data_system.decision_se)
    # Switch to load screen
    $scene = Scene_Load.new
  end
  #--------------------------------------------------------------------------
  # * Command: Shutdown
  #--------------------------------------------------------------------------
  def command_shutdown
    # Play decision SE
    $game_system.se_play($data_system.decision_se)
    # Fade out BGM, BGS, and ME
    Audio.bgm_fade(800)
    Audio.bgs_fade(800)
    Audio.me_fade(800)
    # Shutdown
    $scene = nil
  end
  #--------------------------------------------------------------------------
  # * Battle Test
  #--------------------------------------------------------------------------
  def battle_test
    # Load database (for battle test)
    $data_actors        = load_data("Data/BT_Actors.rxdata")
    $data_classes       = load_data("Data/BT_Classes.rxdata")
    $data_skills        = load_data("Data/BT_Skills.rxdata")
    $data_items         = load_data("Data/BT_Items.rxdata")
    $data_weapons       = load_data("Data/BT_Weapons.rxdata")
    $data_armors        = load_data("Data/BT_Armors.rxdata")
    $data_enemies       = load_data("Data/BT_Enemies.rxdata")
    $data_troops        = load_data("Data/BT_Troops.rxdata")
    $data_states        = load_data("Data/BT_States.rxdata")
    $data_animations    = load_data("Data/BT_Animations.rxdata")
    $data_tilesets      = load_data("Data/BT_Tilesets.rxdata")
    $data_common_events = load_data("Data/BT_CommonEvents.rxdata")
    $data_system        = load_data("Data/BT_System.rxdata")
    # Reset frame count for measuring play time
    Graphics.frame_count = 0
    # Make each 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 party for battle test
    $game_party.setup_battle_test_members
    # Set troop ID, can escape flag, and battleback
    $game_temp.battle_troop_id = $data_system.test_troop_id
    $game_temp.battle_can_escape = true
    $game_map.battleback_name = $data_system.battleback_name
    # Play battle start SE
    $game_system.se_play($data_system.battle_start_se)
    # Play battle BGM
    $game_system.bgm_play($game_system.battle_bgm)
    # Switch to battle screen
    $scene = Scene_Battle.new
  end
end

I get errors when i try to increase a variables value; the variable will return false and i get this popup: script 'Interpreter 4' line 146: NoMethodError occurred.
Undefined method '+' for false:FalseClass

Is there any way to do what I'm looking for?

thanks in advance:
catkitten
 

khmp

Sponsor

I believe your problem lies within here:
Code:
module Switch_Load
  def self.load_switches
    if FileTest.exist?("Saves/Save1.ckd")
      file = File.open("Saves/Save1.ckd", "r")
    else
      return
    end
    # Read character data for drawing save file
    junk_characters = Marshal.load(file)
    # Read frame count for measuring play time
    junk_frame_count = Marshal.load(file)
    # Read each type of game object
    junk_system        = Marshal.load(file)
    $game_variables      = Marshal.load(file)
    file.close
  end
end
When you go to load a file you better read it in the same order that you wrote the file in. To get to variables you must first read the switches. Even if you aren't going to do anything with it. So here is the fix for you:
Code:
module Switch_Load
  def self.load_switches
    if FileTest.exist?("Saves/Save1.ckd")
      file = File.open("Saves/Save1.ckd", "r")
    else
      return
    end
    # Read character data for drawing save file
    junk_characters = Marshal.load(file)
    # Read frame count for measuring play time
    junk_frame_count = Marshal.load(file)
    # Read each type of game object
    junk_system        = Marshal.load(file)
    junk_switches      = Marshal.load(file)
    $game_variables     = Marshal.load(file)
    file.close
  end
end
And you should consider renaming the module and the method to an appropriate name. Variable_Load, load_variables, just to avoid confusion
 
Thank you so much for the help and moving my topic to the correct location, I wasn't sure if it belonged here or in script request.

I have renamed the module and the method, thanks for the tip.

When you go to load a file you better read it in the same order that you wrote the file in. To get to variables you must first read the switches
I didn't know that. Thanks for the info.

Edit: Is there any way to save a specific $game_variable, I've tried this:
Edit2: I fixed it by putting these over main:

Code:
module Variable_Save
  def self.dumper
      var1 = $game_variables[75]
      var2 = $game_variables[76]
      var3 = $game_variables[77]
      var4 = $game_variables[78]
      var5 = $game_variables[79]
      var6 = $game_variables[80]
    file = File.open('Saves/vars.ckd', 'wb')
    #Marshal.dump($game_variables, file)
    Marshal.dump(var1, file)
    Marshal.dump(var2, file)
    Marshal.dump(var3, file)
    Marshal.dump(var4, file)
    Marshal.dump(var5, file)
    Marshal.dump(var6, file)
    file.close   
    end
end
Code:
module Variable_Load
  def self.load_variables
    if FileTest.exist?("Saves/vars.ckd")
      file = File.open("Saves/vars.ckd", "r")
    else
      return
    end
    #$game_variables     = Marshal.load(file)
    var1    = Marshal.load(file)
    var2    = Marshal.load(file)
    var3    = Marshal.load(file)
    var4    = Marshal.load(file)
    var5    = Marshal.load(file)
    var6    = Marshal.load(file)
    file.close
    $game_variables[75] = var1
    $game_variables[76] = var2
    $game_variables[77] = var3
    $game_variables[78] = var4
    $game_variables[79] = var5
    $game_variables[80] = var6
  end
end
And calling them with: Variable_Load.load_variables, and Variable_Save.dumper
 

khmp

Sponsor

If you are going to mess with Save files don't use more than one method to do it. Corrupting a save file is very easy when you play with it's innards at multiple locations. All the variables are saved why would you want only a few saved. Why not use the method to load all the variables. But only replace the ones you want like such:

Code:
module Variable_Load
  VARS_WANTED = (75..80).to_a
  def self.load_switches
    if FileTest.exist?("Saves/Save1.ckd")
      file = File.open("Saves/Save1.ckd", "r")
    else
      return
    end
    # Read character data for drawing save file
    junk_characters  = Marshal.load(file)
    # Read frame count for measuring play time
    junk_frame_count = Marshal.load(file)
    # Read each type of game object
    junk_system      = Marshal.load(file)
    junk_switches    = Marshal.load(file)
    vars             = Marshal.load(file)
    file.close
    VARS_WANTED.each { |index| $game_variables[index] = vars[index] }
  end
end

And this way you don't need a messy save module.
 
Ya, that's a good idea but, I'm making a local Auction house, so I'm saving selected variables to a new save file, "vars.ckd". This way all the game saves(save1.ckd, save2.ckd,....) can have access to save and load the selected variables from the file vars.ckd. But I did take your advice and rewrote my save and load modules:

Code:
module Variable_Load
  VARS_WANTED = (75..80).to_a
  def self.load_variables
    if FileTest.exist?("Saves/vars.ckd")
      file = File.open("Saves/vars.ckd", "r")
    else
      return
    end
    vars             = Marshal.load(file)
    file.close
    VARS_WANTED.each { |index| $game_variables[index] = vars[index] }
  end
end
Code:
module Variable_Save
  def self.dumper
    file = File.open('Saves/vars.ckd', 'wb')
    Marshal.dump($game_variables, file)
    file.close   
    end
end
 

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