[Ace] How to get RPG Maker to save and load a variable?

Discussion in 'RGSSx Script Support' started by Gleen, Jun 19, 2012.

  1. Gleen

    Gleen Veteran Veteran

    Messages:
    98
    Likes Received:
    21
    First Language:
    portuguese
    Sorry, i was going to elaborate on that but before i needed to test something and then i got sleepy haha. I don't know what omoney means by saving instances variables so i assume he just wants to save the value of @somevariable.

    Instances variables belongs to specific objects and can be referenced from any method of their class or subclass but the value of any unintialized instance variable is nil, so if he saves a instance variable when he loads the game that variable value will be nil. And if he initializes it again, it will recieve the first value he asigned and not the saved one.

    There's another problem, he can't simply put the code below in make_save_contents:



    Code:
    contents[:test] = @test
    
    Since @test won't be found if isn't initialized inside DataManager. The same for extract_save_contents, but as i said above it will get the original value and not the saved one.

    The right way to save an instance variable is through accessors, seting them inside objects that will be saved. So you're not necessarily saving an instance variable but an atribute to that object.

    I'm not sure if i explained it right (my english is not that good so it limits me...) or if i'm missing something but as far as i understand ruby and the rgss api, thats it.
     
    #1
    omoney likes this.
  2. mobychan

    mobychan CodeMaster Veteran

    Messages:
    297
    Likes Received:
    44
    Location:
    Baden-W├╝rrtemberg - Germany
    First Language:
    German
    I'd say you can save instance variables, but only, if they are part of the information in a global variable, or how else would the Player and his comrades keep their Level/HP/MP... after saving?
     
    #2
    omoney likes this.
  3. Gleen

    Gleen Veteran Veteran

    Messages:
    98
    Likes Received:
    21
    First Language:
    portuguese
    Yep, that's right, its what i said about accessors, you can save instances variavles only if they're atributes of a object wich will be saved.
     
    Last edited by a moderator: Jun 19, 2012
    #3
  4. omoney

    omoney Ruby Nooby Veteran

    Messages:
    57
    Likes Received:
    4
    First Language:
    English
    Thanks for all the responses. I am not quite sure how to save and load instance variables or use accessors though. If i define an instance variable @variable inside inside GameInterpreter in a script how would I save it and load it?
     
    #4
  5. mobychan

    mobychan CodeMaster Veteran

    Messages:
    297
    Likes Received:
    44
    Location:
    Baden-W├╝rrtemberg - Germany
    First Language:
    German
    You'd have to save a Global Variable (like described above) which is an instance of Game_Interpreter.

    Every instance variable of that instance will be saved with it.

    Saving a Global Variable is kinda the same as making a "screenshot" of the information inside the instance you save.
     
    #5
  6. omoney

    omoney Ruby Nooby Veteran

    Messages:
    57
    Likes Received:
    4
    First Language:
    English
    Ok. I think I have an idea of what to do now. Thanks.
     
    #6
  7. BigEd781

    BigEd781 undefined method 'stupid_title' found for nil:NilC Veteran

    Messages:
    940
    Likes Received:
    303
    Location:
    Austin, TX
    First Language:
    Dothraki
    Primarily Uses:
    N/A
    Just to clear things up here; yes, you can serialize (save) instance variables. An instance variable is simply a member of a type that is specific to an instance of said type (i.e., each instance gets a distinct copy). There is absolutely nothing that would prevent you from saving them between sessions.
     
    #7
    omoney likes this.
  8. omoney

    omoney Ruby Nooby Veteran

    Messages:
    57
    Likes Received:
    4
    First Language:
    English
    Good to know, Thanks
     
    #8
  9. timk1980

    timk1980 Apprentice Member

    Messages:
    123
    Likes Received:
    42
    Location:
    Boston
    First Language:
    English
    Just to add another option (which may or may not be a "good" option), you can always add instance variables to one of the classes like Game_System or Game_Player, that already gets saved, then you don't have to worry about specially coding to handle the save.
     
    #9
    omoney and BigEd781 like this.
  10. omoney

    omoney Ruby Nooby Veteran

    Messages:
    57
    Likes Received:
    4
    First Language:
    English
    hm thats true. Right now I just made another class and stuck it inside a global variable and save and load that variable. I wonder which one is more efficient or the preferred practice.
     
    #10
  11. Gleen

    Gleen Veteran Veteran

    Messages:
    98
    Likes Received:
    21
    First Language:
    portuguese
    I guess its a matter of semantics when you're adding an atribute that won't work with data.
     
    #11

Share This Page