[RMXP] Alter Save File Data without overwriting the entire file

Whenever the player dies, I want a variable to be increased in the already-existing Save File, without overwriting it whole, so that I can tell how many times they have died, maybe even where they died, and act accordingly with whatever other function I want the death count to have
Use a game variable then. They get saved by the game every... single... time!
Use a game variable then. They get saved by the game every... single... time!
It wouldn't get saved after a game over, I want to change the variable directly from the Save File, so that when a player loads the save again, the change will persist and take effect
What would happen if the player loads another save file? Would that have a separate counter that counts loads?
A global save file would be a better solution...
A global save file would be a better solution...
Do you mean using only a single save file for the whole game, or a file that stores data that loads to all save files?
Nah, a single file separate from all save files. It'd only contain global stuff like times the player lost or whatever you wanna add to that list. The script would automatically load that file's data before or after loading the selected save file. It would be always available and you would have no need to alter already existing save files.
Nah, a single file separate from all save files. It'd only contain global stuff like times the player lost or whatever you wanna add to that list. The script would automatically load that file's data before or after loading the selected save file. It would be always available and you would have no need to alter already existing save files.
Do you have any script at hand that can do that
No worries. It can be made if it doesn't exist, yet.
No worries. It can be made if it doesn't exist, yet.
Oh, alright then
But since you want to get such an extra file, what else would you like to feed it with?
Tentatively, I'd name the main variable stored there "losses" if you don't mind.
But since you want to get such an extra file, what else would you like to feed it with?
Tentatively, I'd name the main variable stored there "losses" if you don't mind.
Well, I suppose a switch just incase would be pretty useful
What would happen if the player loads another save file? Would that have a separate counter that counts loads?
A very relevant question here, that you have completely ignored @NardBruh
A very relevant question here, that you have completely ignored @NardBruh
Oh I'm sorry about that
What would happen if the player loads another save file? Would that have a separate counter that counts loads?
Well, for the game I had this idea for? My main desire is for the deaths to be remembered even if they're in different saves, but I'm perfectly fine with the opposite too
Here's the first version of my code...

You just need to find the best place to call the Global.load_file method.
Global.save_file will be called right after the battle ended but before the map is loaded.

EDITED in order to include a clarification.

Version 0.1.1

Ruby:
# * GlobalValues XP * #
#   Scripter : Kyonides Arkanthes
#   v0.1.1 - 2023-06-02
# * Outdated! * #
Here's the first version of my code...

You just need to find the best place to call the Global.load_file method.
Global.save_file will be called right after the battle ended but before the map is loaded.

EDITED in order to include a clarification.

Version 0.1.1

Ruby:
# * GlobalValues XP * #
#   Scripter : Kyonides Arkanthes
#   v0.1.1 - 2023-06-02

# This script creates a new Global Variable and stores values there for future
# use in all game sessions.

# It will update the battle result variables automatically if you are using
# a Battle System that is compatible enough with the Default BS.

# Wins, Escapes & Losses are already updated by the script!

# * Script Calls * #

# - Create or Load the new Global Variable
#   Global.load_file

# - Save Global Data at Any Moment
#   Global.save_file

# - Access Wins:
#   $global_data.wins
# - Access Escapes:
#   $global_data.escapes
# - Access Losses:
#   $global_data.losses
# - Access or Update the Switch:
#   $global_data.switch
#   $global_data.switch = true
#   $global_data.switch = false

module Global
  # PATH = "" means the Game's Root Directory (RD)
  # PATH = "Saves" means the Saves Directory at the RD
  PATH = ""
  FILENAME = "GlobalValues.rxdata"
  class Values
    def initialize
      @wins = 0
      @escapes = 0
      @losses = 0
      @switch = nil
    end

    def save_result(result)
      case result
      when 0
        @wins += 1
      when 1
        @escapes += 1
      when 2
        @losses += 1
      end
    end
    attr_accessor :wins, :escapes, :losses, :switch
  end

  extend self
  def filename
    if PATH.empty?
      FILENAME
    else
      PATH + "/" + FILENAME
    end
  end

  def save_file
    save_data($global_data, filename)
  end

  def load_file
    filename = path
    if File.exist?(filename)
      $global_data = load_data(filename)
    else
      $global_data = Global::Values.new
    end
  end
end

class Scene_Battle
  alias :kyon_global_values_scn_btl_btl_end :battle_end
  def save_global_data(result)
    $global_data.save_result(result)
    Global.save_file
  end

  def battle_end(result)
    save_global_data(result)
    kyon_global_values_scn_btl_btl_end(result)
  end
end
Well uh, winning a fight, escaping a fight and losing a fight lead to the game crashing, line 84 being stated as the issue by RPG Maker

Global.save_file works, but using load_file crashes the game, the same happens with the switch
No problem, I've come with an improved version of my script.

Version 0.2.1

Ruby:
# * GlobalValues XP * #
#   Scripter : Kyonides Arkanthes
#   v0.2.1 - 2023-06-02
# * Outdated! * #
No problem, I've come with an improved version of my script.

Version 0.2.1

Ruby:
# * GlobalValues XP * #
#   Scripter : Kyonides Arkanthes
#   v0.2.1 - 2023-06-02

# This script creates a new Global Variable and stores values there for future
# use in all game sessions.

# It will update the battle result variables automatically if you are using
# a Battle System that is compatible enough with the Default BS.

# Wins, Escapes & Losses are already updated by the script!

# * Script Calls * #

# - Access Wins:
#   $global_data.wins
# - Access Escapes:
#   $global_data.escapes
# - Access Losses:
#   $global_data.losses
# - Access Total Saves:
#   $global_data.saves
# - Access Total Loads:
#   $global_data.loads
# - Access or Update the Switch:
#   $global_data.switch
#   $global_data.switch = true
#   $global_data.switch = false

# * Optional - No Need for You to use them! * #

# - Create or Load the new Global Variable
#   Global.load_file

# - Save Global Data at Any Moment
#   Global.save_file

module Global
  # PATH = "" means the Game's Root Directory (RD)
  # PATH = "Saves" means the Saves Directory at the RD
  PATH = ""
  FILENAME = "GlobalValues.rxdata"
  class Values
    def initialize
      @wins = 0
      @escapes = 0
      @losses = 0
      @saves = 0
      @loads = 0
      @switch = nil
    end

    def save_result(result)
      case result
      when 0
        @wins += 1
      when 1
        @escapes += 1
      when 2
        @losses += 1
      end
      Global.save_file
    end

    def saved!
      @saves += 1
      Global.save_file
    end

    def loaded!
      @loads += 1
      Global.save_file
    end
    attr_accessor :wins, :escapes, :losses, :saves, :loads, :switch
  end

  extend self
  def filename
    if PATH.empty?
      FILENAME
    else
      PATH + "/" + FILENAME
    end
  end

  def save_file
    save_data($global_data, filename)
  end

  def load_file
    if File.exist?(filename)
      $global_data = load_data(filename)
    else
      $global_data = Global::Values.new
      save_file
    end
  end
  load_file
end

class Scene_Save
  alias :kyon_global_values_scn_sv_wsd :write_save_data
  def write_save_data(file)
    kyon_global_values_scn_sv_wsd(file)
    $global_data.saved!
  end
end

class Scene_Load
  alias :kyon_global_values_scn_ld_rsd :read_save_data
  def read_save_data(file)
    kyon_global_values_scn_ld_rsd(file)
    $global_data.loaded!
  end
end

class Scene_Battle
  alias :kyon_global_values_scn_btl_btl_end :battle_end
  def save_global_data(result)
    $global_data.save_result(result)
  end

  def battle_end(result)
    save_global_data(result)
    kyon_global_values_scn_btl_btl_end(result)
  end
end
Oh sweet, it works now!

Tho I feel like there should be another counter added for the amount of game overs you get, since losses only registers battle losses where you have "Continue Even When Loser" on, it doesn't count getting game overs as losses, I think that would make this script absolutely perfect
=_= Why am I feeling like I am getting abused by someone here?
Yeah, I wonder why. 😛

By the way, you gotta call $global_data.gameover! if the player ever gets a Game Over screen...

Version 0.3.0

Ruby:
# * GlobalValues XP * #
#   Scripter : Kyonides Arkanthes
#   v0.3.0 - 2023-06-03
# * Go straight to its Dedicated Thread * #
=_= Why am I feeling like I am getting abused by someone here?
Yeah, I wonder why. 😛

By the way, you gotta call $global_data.gameover! if the player ever gets a Game Over screen...

Version 0.3.0

Ruby:
# * GlobalValues XP * #
#   Scripter : Kyonides Arkanthes
#   v0.3.0 - 2023-06-03

# This script creates a new Global Variable and stores values there for future
# use in all game sessions.

# It will update the battle result variables automatically if you are using
# a Battle System that is compatible enough with the Default BS.

# Stats like Wins, Escapes, Losses, Saves and Loads are already
# updated by the script automatically!

# * Script Calls * #

# - Access Wins:
#   $global_data.wins
# - Access Escapes:
#   $global_data.escapes
# - Access Losses:
#   $global_data.losses
# - Access Total Saves:
#   $global_data.saves
# - Access Total Loads:
#   $global_data.loads
# - Access or Update Total Gameovers:
#   $global_data.gameovers
#   $global_data.gameover!
# - Access or Update the Switch:
#   $global_data.switch
#   $global_data.switch = true
#   $global_data.switch = false

# * Optional - No Need for You to use them! * #

# - Create or Load the new Global Variable
#   Global.load_file

# - Save Global Data at Any Moment
#   Global.save_file

module Global
  # PATH = "" means the Game's Root Directory (RD)
  # PATH = "Saves" means the Saves Directory at the RD
  PATH = ""
  FILENAME = "GlobalValues.rxdata"
  class Values
    def initialize
      @wins = 0
      @escapes = 0
      @losses = 0
      @saves = 0
      @loads = 0
      @gameovers = 0
      @switch = nil
    end

    def save_result(result)
      case result
      when 0
        @wins += 1
      when 1
        @escapes += 1
      when 2
        @losses += 1
      end
      Global.save_file
    end

    def saved!
      @saves += 1
      Global.save_file
    end

    def loaded!
      @loads += 1
      Global.save_file
    end

    def gameover!
      @gameovers += 1
      Global.save_file
    end
    attr_accessor :wins, :escapes, :losses
    attr_accessor :saves, :loads, :gameovers, :switch
  end

  extend self
  def filename
    if PATH.empty?
      FILENAME
    else
      PATH + "/" + FILENAME
    end
  end

  def save_file
    save_data($global_data, filename)
  end

  def load_file
    if File.exist?(filename)
      $global_data = load_data(filename)
    else
      $global_data = Global::Values.new
      save_file
    end
  end
  load_file
end

class Scene_Save
  alias :kyon_global_values_scn_sv_wsd :write_save_data
  def write_save_data(file)
    kyon_global_values_scn_sv_wsd(file)
    $global_data.saved!
  end
end

class Scene_Load
  alias :kyon_global_values_scn_ld_rsd :read_save_data
  def read_save_data(file)
    kyon_global_values_scn_ld_rsd(file)
    $global_data.loaded!
  end
end

class Scene_Battle
  alias :kyon_global_values_scn_btl_btl_end :battle_end
  def save_global_data(result)
    $global_data.save_result(result)
  end

  def battle_end(result)
    save_global_data(result)
    kyon_global_values_scn_btl_btl_end(result)
  end
end
I'm sorry for making you feel like that

But now this script is perfect! Thank you so so much!

Original thread: https://forums.rpgmakerweb.com/threads/rmxp-alter-save-file-data-without-overwriting-the-entire-file.157967/