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.

save custom variables in VX

khmp

Sponsor

Alright so have you looked at Scene_File in VX? Its ok if you haven't. When this scene is started it either has the ability to save information or load information to and from a file respectively speaking. Saving information to a file is really easy and the way RGSS/2 achieves this is through the Marshal module.

To save information you need two things to save to a file. First the piece of data you are trying to save. So for the sake of simplicity and for this example we will save a string, "I used Marshal.dump to save this data." First we need to define a port or the destination where we want the data to be saved to. Afterwards saving is a simple task.

Save Example:
Code:
save_string = 'I used Marshal.dump to save this data.'
save_file = File.new('test.txt', 'wb')
Marshal.dump(save_string, save_file)
save_file.close

In File.new I used a second argument which was a string, 'wb', this tells the file that whatever data is written to it to save it in binary. There are plenty more types of arguments that could be placed there. For more of them with explanations visit here and look up "IO". Anyway now the data has been saved how do we go about retrieving it? Marshal has a method to achieve this as well conveniently called "load". "load" requires only one parameter/argument which is the file we are trying to read from. So first let's make a file that reads in the same mode as the data was written. The mode that we wrote the file in must match the read's equivalent otherwise you might run into odd bugs. Save it in binary, read it in binary. Get it? Also if you are saving multiple items to a file. The order the data is saved in better match the way the data is loaded. Save string1 then string2 then int1, you have to load it in that same order.

Load Example:
Code:
load_file = File.open('test.txt', 'rb')
load_string = Marshal.load(load_file)
load_file.close

print load_string

If you were to place that code directly above main together with the saving you should see that the string was loaded from the file that we just wrote to. And it will print "I used Marshal.dump to save this data.". I hope that helps you out Nuberus. If not just clarify the confusing parts if possible.

Good luck with it Nuberus! :thumb:
 
I finally had a bit of time so I could try this out. It actually worked! Not only that, it was actually really easy to do! Thanks for the awesome explanation, khmp. You're the King of Awesome.
 

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