• Just a reminder that providing specifics on, sharing links to, or naming websites where ROMs can be accessed is against the rules. If your post has any of this information it will be removed.
  • Ever thought it'd be cool to have your art, writing, or challenge runs featured on PokéCommunity? Click here for info - we'd love to spotlight your work!
  • Which Pokémon Masters protagonist do you like most? Let us know by casting a vote in our Masters favorite protagonist poll here!
  • Red, Hilda, Paxton, or Kellyn - which Pokémon protagonist is your favorite? Let us know by voting in our poll!
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

[Scripting Question] Saving new variables

  • 24
    Posts
    6
    Years
    • Seen Jul 24, 2023
    Hello all,
    I've been working for some time on a relatively small and simple skill tree for the player character. Eventually, I'd like to post it here when all of the kinks are worked out. I've gone through Marin's intro to ruby document, Luka's videos, and have been reading scripting questions on here for a while too, but this is my first time really trying to script something myself.

    It works like so:
    - there is a global variable ($skillpoints) that accounts for the amount of skill points the trainer has. There is a method pbAddSkillPoint that simply adds 1 to the $skillpoints variable
    - Each skill (21 in total) is tied to a global variable ($trskill1, $trskill2) that is initially set to false at the beginning of my script. When the player selects a skill on my skill screen scene under the right conditions, the global variable for that skill is then set to true. I have edited various other scripts to work differently if a skill is true. For example, the Capture Specialist skill adds +10% to the catch rate when catching Pokemon if it is on. I used an if statement at the end of def pbThrowPokeball in PokeBattle_Battle to achieve this:
    Code:
    if $tamerskill1 == true
    x=(x*1.1).floor
    end

    The snag that I've run into is that I have no idea how to get these variables to save. Everything works perfectly within the instance of the game that they're chosen in, but once I save and come back, $skillpoints is set back to 0 and all $trskillX are set back to false. I did some research and have tried moving my initial instances of the variables to a class that gets saved (Game_Player), but that didn't do it, and helped me to realize I don't really understand how the saving works. I have also tried adding lines to PScreen_Save ('Marshal.dump($skillpoints,f)' under def pbSave) and Pscreen_Load ('$skillpoints = Marshal.load(f)' under the File.open(savefile){|f| section), but all that does it crash the game when I try to load the save (including if I start over with a new save).

    I hope this all made sense, and if anyone could point me in the right direction about saving and loading variables, I'd really appreciate it. There's still a ton about scripting/coding that I don't understand, so I'm pretty sure I'm making a novice mistake somewhere. Let me know if I need to provide more information or get more specific about anything, and I'll be happy to.

    Edit: I just thought about setting the variables to be game variables/game switches. Would that be a better way to go about this? Would it hinder the eventual plug'n'play-ability of this script?
     
    Last edited:
    In order for your new variables to be saved, you need to Marshal.dump and load them during the save and load procedures respectively. The easier way would be to piggy-back off of something that already gets saved to the save file already. Like you mentioned, $game_variables being one of them. Though you could in theory define them to be a part of the $Trainer or $PokemonGlobal variables by simply adding an attribute accessor, and getting and manipulating the variable something like this:
    Code:
    class PokemonGlobalMetadata
      attr_accessor :custom_variable
    end
    
    # prints value of custom variable
    p $PokemonGlobal.custom_variable
    # $PokemonGlobal is always saved to file
     
    I decided to forgo using Marshal data, and went over the section in Marin's Ruby Basics document about attribute accessors. That and Luka's comment helped a lot, and I ended up using $PokemonGlobal in the way you suggested Luka. Works like a dream, thanks for helping me figure this out.
     
    Back
    Top