[RESOLVED] about edit a save data

Started by finalholylight, July 09, 2014, 10:53:52 pm

Previous topic - Next topic

finalholylight

July 09, 2014, 10:53:52 pm Last Edit: July 16, 2014, 06:01:18 am by finalholylight
How to edit a save data ?

My problem is: I continue a game with Save1, when I play, many switches was changed, then I shutdown game, but before shutdown, I want save a special switch but not use scene_save ( that means other switches was changed will be not saved ), I will call this is secret save, player don't know this, so I want to edit that save data.

I used script call in game :

file = File.open("Save1.rxdata", "wb")
$game_switches[100] = true
file.close


but when I continue, error pop up : reach end of file.

KK20

file = File.open("Save1.rxdata", "wb")

This line will create a 'Save1.rxdata' file, but it will delete everything inside of it. I believe you want to open it with "ab" instead, which will append saved data to the end of the file and not delete what you have.

You have to use
Marshal.dump($game_switches[100], file)
if you want to actually save something to the file. What you have right now isn't doing anything.

You will also have problems loading your game now since save files are no longer uniform. For example, if you modify Scene_Load to have
$game_switches[100] = Marshal.load(file)

your players might get that 'reached end of file' error if they never go through your script call.

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

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

Join the CP Discord Server!

finalholylight

No error anymore, but also not working, I set switch 100 ON and save it to save data, when I load game and test, switch 100 is still OFF , I already added $game_switches[100] = Marshal.load(file) in scene_load to load that switch and also added Marshal.dump($game_switches[100], file) in scene_save to avoid error when load save, in game, I load save and active event calls script:
file = File.open("Save1.rxdata", "ab")
$game_switches[100] = true
Marshal.dump($game_switches[100], file)
file.close

to make sure, I check switch 100, it's ON now.

then I return to title screen and continue again, check switch 100 and result is still OFF.

KK20

Oh right, by using "ab", you're appending to the end of the file, not changing the last value. So your saved file looks like this after your script call:

    $game_system
    $game_switches
    $game_variables
    $game_self_switches
    $game_screen
    $game_actors
    $game_party
    $game_troop
    $game_map
    $game_player
    $data_weapons
    $game_switches[100]
    $game_switches[100]

It just adds another $game_switches[100] to the end of it. When you go to load your game again, it's getting the first $game_switches[100] and ignores everything else after it.

You're going to have to make a method that is similar to Scene_Save: wipe out the save file and load it with all the values again.

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

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

Join the CP Discord Server!

finalholylight

Hmm, if I do that, then I should call Marshal.dump($game_switches, file) again, right ?, so that the other switches was activated when player plays this time will be saved too.

ForeverZer0

You can open as read/write, but from the sounds of it, you are making something overly complicated, and should be using some other method instead. If all you need to do is make sure a switch is saved in a certain way, there are much easier and more efficient ways of doing it.
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.

finalholylight

'__'- , then, would you mind give me more details ?, I'm noob about scripting  :facepalm:

ForeverZer0

Unless you are using a custom save system, you simply need to alias the save method and change the switch there.
Depending on how your game is designed, such as menu systems, events, etc., and how/where the player can save, it could be done any number of places.

The following example will always save switch 100 as true, but not disturb it in the game, meaning it will only change it for save files.

class Scene_Save
 
  alias original_write_save_data write_save_data
  def write_save_data(file)
    # Copy original value to a local variable
    original_value = $game_switches[100]
    # Set switch to true
    $game_switches[100] = true
    # Call original method, which actually saves the data
    original_write_save_data(file)
    # Change switch back to its original value
    $game_switches[100] = original_value
  end
end
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.

finalholylight

July 10, 2014, 11:23:12 am #8 Last Edit: July 10, 2014, 12:24:46 pm by finalholylight
Hmm, seem like you misunderstand my idea, when playing game, player active many switches and not use save game, so that the change of those switches will be lost when shut down game, but I still want to save change of one of those switches to save_data. But the code you show me will save that special switch ON when player use save game, this is not what I want.

LiTTleDRAgo

ripped and edited from Drago - Core Engine: ShowHide
class Object
  def force_save(index)
    return DataManager.save_game(index) if defined?(DataManager)
    save = defined?(Window_ActorCommand) ? Scene_File.new(0,nil,0) : Scene_Save.new
    begin
      file = File.open(save.make_filename(index), "wb")
      (save.write_save_data(file) && file.close) || true
    rescue
      File.delete(save.make_filename(index)) rescue nil
      #(Sound.play_buzzer) && false
    end
  end
end


how to use :

$game_switches[100] = true
force_save(0) # This will force save into Save1.rxdata
force_save(1) # This will force save into Save2.rxdata
...etc

ForeverZer0

Untested.


module NoSave
 
 def self.update_switches(save_file)

   # You will need to create your own handling for when the file does not
   # exist, etc.
   
   file = File.open(save_file, 'rb')
   characters         = Marshal.load(file)
   frame_count        = Marshal.load(file)
   game_system        = Marshal.load(file)
   game_switches      = Marshal.load(file)
   game_variables     = Marshal.load(file)
   game_self_switches = Marshal.load(file)
   game_screen        = Marshal.load(file)
   game_actors        = Marshal.load(file)
   game_party         = Marshal.load(file)
   game_troop         = Marshal.load(file)
   game_map           = Marshal.load(file)
   game_player        = Marshal.load(file)
   file.close
   
   # Save only changes made to $game_switches, everything else will remain the
   # same as the last save. If you only want to change specific switches, just
   # use the follwoing pattern:
   #         game_switches[ID] = $game_switches[ID]
   game_switches = $game_switches
   
   file = File.open(save_file, 'wb')
   Marshal.dump(characters, file)
   Marshal.dump(frame_count, file)
   Marshal.dump(game_system, file)
   Marshal.dump(game_switches, file)
   Marshal.dump(game_variables, file)
   Marshal.dump(game_self_switches, file)
   Marshal.dump(game_screen, file)
   Marshal.dump(game_actors, file)
   Marshal.dump(game_party, file)
   Marshal.dump(game_troop, file)
   Marshal.dump(game_map, file)
   Marshal.dump(game_player, file)
   file.close
 end
end


Quote from: LiTTleDRAgo on July 10, 2014, 11:53:10 am
ripped and edited from Drago - Core Engine: ShowHide
class Object
  def force_save(index)
    return DataManager.save_game(index) if defined?(DataManager)
    save = defined?(Window_ActorCommand) ? Scene_File.new(0,nil,0) : Scene_Save.new
    begin
      file = File.open(save.make_filename(index), "wb")
      (save.write_save_data(file) && file.close) || true
    rescue
      File.delete(save.make_filename(index)) rescue nil
      #(Sound.play_buzzer) && false
    end
  end
end


how to use :

$game_switches[100] = true
force_save(0) # This will force save into Save1.rxdata
force_save(1) # This will force save into Save2.rxdata
...etc



This will still save the whole game, not just modified switches.
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.

finalholylight

@LiTTleDRAgo : yes, like ForeverZer0 said, it's save the whole game, but thank you for spending time to help me.
@ForeverZer0 : yay, It's work fine ^^ , thank you
@KK20 : thank you ,too, for trying help me solve my problem.
thanks to all you :D, yay, thanks xD