Selecting what variables to serialize on save

Currently, when you save your game, RM simply marshals a bunch of objects and that's it.


However, what happens when I want to make it so that I don't want a certain instance variable to be saved?


Does this suggest that my design is bad and should find a way to avoid storing it in the object itself?
I'd store it in an object that doesn't get saved. Let's say it's a setting; then I'd have SavedSettings and TempSettings; only the SavedSettings object is stored when saving, but if you want some similar functionality shared between these classes, have them inherit methods one way or the other.
I almost wish that I could jack an extension onto RM to allow for the usage of something like Neo4j Graph Databases or MongoDB. Definitely more advanced, but the payoffs for flexibility and raw power are worth it to me at least.

In this case though, I'd agree and probably go with a temporary object of some sort that doesn't save.
Whenever you call Marshal.dump(Object), Ruby calls the method Object.marshal_dump (or Object._dump) before serializing it. Thus if you want to customize your objects serialization, you just need to write your own version of that method. Realize you'll also need to write your own version of marshal_load too in order to reconstitute your object the way you want to. I've done this before using something like this:
 

Spoiler
class My_Objectdef initialize @saved_variable_1 = "Hello" @saved_variable_2 = "World" @unsaved_variable = "!!!"enddef marshal_dump return [ @saved_variable_1, @saved_variable_2 ]enddef marshal_load(dumpped_object) @saved_variable_1 = dumpped_object[0] @saved_variable_2 = dumpped_object[1]endend
Reference: http://www.linuxtopia.org/online_books/programming_books/ruby_tutorial/Reflection_ObjectSpace_and_Distributed__Ruby_Custom_Serialization_Strategy.html
Assuming RM has JSON support, just serialize objects to JSON from a hash.
Whenever you call Marshal.dump(Object), Ruby calls the method Object.marshal_dump (or Object._dump) before serializing it. Thus if you want to customize your objects serialization, you just need to write your own version of that method. Realize you'll also need to write your own version of marshal_load too in order to reconstitute your object the way you want to. I've done this before using something like this:
How can it be done so that I can have a dozen different scripts all adding custom instance variables to an object, but making sure that only the variables I want to exclude will be excluded?


Also, if someone else decides that they want to exclude their own variables, we would likely end up with two incompatible scripts because they're both overwriting the same method under the assumption that no one else would be doing with.
#==============================================================================


# ** Game_Temp


#------------------------------------------------------------------------------


# This class handles temporary data that is not included with save data.


# The instance of this class is referenced by $game_temp.


#==============================================================================
Assuming RM has JSON support, just serialize objects to JSON from a hash.
It doesn't sadly. But that's just a different format, it's not relevant to the question at hand 🙂

Original thread: https://forums.rpgmakerweb.com/threads/selecting-what-variables-to-serialize-on-save.34893/