Jump to content
Zen Blood

Global Array Becomes Null After Exiting Game?

Recommended Posts

Good day!

 

I'm having a wee bit of a problem managing a global array.

 

So, I set up an array: $hereismyarray = [0,0,0,0,0,0,0]

And throughout my game I want to change the value of certain variables in the array, like this: $hereismyarray[2] = 6

 

I can move from map to map and the array will continue to work, but unfortunately, if I close the game and re-open it, the global array becomes null for some reason (even if I save the game file after the array has been set).

 

I would rather use an array instead of 7 different variables. Is there any way I can force my game to save the array so that it still exists after the game is closed and opened again?

 

Thank you for your time!

Share this post


Link to post
Share on other sites

That reason is, that this array is not passed into a save file, so it just gets disposed when you close the game and loading a save file will not 'load' values.

Also, if you modify your array, then go to title and select new game, the array will be the same (modified), as it will not be reset, due to being stored in the memory.

 

What you need to do is to include your array when starting a new game (to initialize it to its starting values) and in the save file to store&load it properly.

 

To do so, open your script editor and head to DataManager.

 

1. First things first, (re)set that variable (array) to base values when starting a new game.

 

Around line #80 you'll see this:

  #--------------------------------------------------------------------------
  # * Create Game Objects
  #--------------------------------------------------------------------------
  def self.create_game_objects
    $game_temp          = Game_Temp.new
    $game_system        = Game_System.new
    $game_timer         = Game_Timer.new
    $game_message       = Game_Message.new
    $game_switches      = Game_Switches.new
    $game_variables     = Game_Variables.new
    $game_self_switches = Game_SelfSwitches.new
    $game_actors        = Game_Actors.new
    $game_party         = Game_Party.new
    $game_troop         = Game_Troop.new
    $game_map           = Game_Map.new
    $game_player        = Game_Player.new
    $your_array         = [0,10,25,55,99] # Add your array here and set its starting values
  end

Add your array there and set its starting values.

 

putting that in the "Set Up New Game" section below it would also do the thing, it doesn't make any difference.

 

If you'll not do this, loading the save, going to title and making a new game will transfer the values from the save file to new game.

 

2. Include your variable (array) into the save file.

 

Around line #210 you'll see this:

  #--------------------------------------------------------------------------
  # * Create Save Contents
  #--------------------------------------------------------------------------
  def self.make_save_contents
    contents = {}
    contents[:system]        = $game_system
    contents[:timer]         = $game_timer
    contents[:message]       = $game_message
    contents[:switches]      = $game_switches
    contents[:variables]     = $game_variables
    contents[:self_switches] = $game_self_switches
    contents[:actors]        = $game_actors
    contents[:party]         = $game_party
    contents[:troop]         = $game_troop
    contents[:map]           = $game_map
    contents[:player]        = $game_player
    contents[:your_array]    = $your_array # < include your array in the save file
    contents
  end

Include your array in the save file there.

Now this array will be saved in the save file.

 

3. Saved it, now it's time to load it.

 

Around line #225; below "Create Save Contents", you'll see this:

  #--------------------------------------------------------------------------
  # * Extract Save Contents
  #--------------------------------------------------------------------------
  def self.extract_save_contents(contents)
    $game_system        = contents[:system]
    $game_timer         = contents[:timer]
    $game_message       = contents[:message]
    $game_switches      = contents[:switches]
    $game_variables     = contents[:variables]
    $game_self_switches = contents[:self_switches]
    $game_actors        = contents[:actors]
    $game_party         = contents[:party]
    $game_troop         = contents[:troop]
    $game_map           = contents[:map]
    $game_player        = contents[:player]
    $your_array         = contents[:your_array] # < include your array to extract it when loading
  end

Include your array to extract it when loading the save file.

 

 

And voila, the array will be initialized, saved and loaded successfully. 100% resistant to game shutdown.

 

 

A more efficient way would be to store your array in the class, even $game_system would do, eventually setup your custom class to keep your values organized, but that's kind of optional I suppose, especially if that would be just one or few values... I'm not oriented well when it comes to optimization (unless something makes a significant, noticeable difference), so I can't really tell.

Share this post


Link to post
Share on other sites

If you'd be aiming to add more custom global variables, you could give classes a try. That would make things more organized.

 

That class would be like a box, where you'd be keeping your variables.

 

It's been a while since I worked with Ruby, but if I remember correctly, you could do something like this:

 

Create a new script -> Your own class:

class Game_Stats
  attr_accessor :score # these make variables from that class accessible 'directly'
  attr_accessor :deaths # otherwise you won't be able to read, nor edit these
  attr_accessor :fame # another way would be setting methods (defs) to read/edit them
  attr_accessor :keys
  
  def initialize # sets starting values
    @score = 0
    @deaths = 0
    @fame = 0
    @keys = Array.new # creates an empty array
    # putting @keys = [] would work too.
  end
end

That's all you need to setup that box with your variables.

 

Then in the steps I mentioned above, replace "your array" things with that new class like this:

 

for 1st step:

$game_stats         = Game_Stats.new

for 2nd step:

contents[:stats]         = $game_stats

for 3rd step:

$game_stats         = contents[:stats]

 

 

Now the variables you'd specify would be global just like $game_variables and you could use them anywhere like this:

$game_stats.score += 50 # adds 50 to score

if $game_stats.deaths >= 10 # checks, if you have died 10 or more times

$game_stats.keys.push(3) # adds key with ID 3 to the array
#=> $game_stats.keys is now [3]

$game_stats.keys.push("Locker Key") # adds a key that is called by name
#=> $game_stats.keys is now [3, "Locker Key"]

# Then you know what to do with arrays. ;)

etc. etc.

 

That way you could keep your variables in the box (class), rather than 'wildly' in the memory, which makes it more optimized.

 

It all would be stored in the save files and stuff (assuming you included these in the save files (2 last steps in these 3 steps mentioned earlier, to be more specific))- storing everything using single lines - just putting the box into save file, rather than specify variables one by one.

 

 

But that's just for your information, perhaps that way will sound more appealing to you in some cases.

 

RELATED:

By an occasion, if you're not aware of useful array methods, take a look at this documentary.

For example, here's one explaining that push method:

Spoiler

qXA9sEo.png

 

 

 


 

Glad could help! You're welcome! :) 

Share this post


Link to post
Share on other sites

@Rikifive

Thank you for the additional information. Am I seeing hashes in some of the examples you gave me? I've actually been looking into those to use as part of a script for a rather complex mechanic I'm not sure if I can pull off. Nevermind, I've got that all figured out!

 

I have a question about editing previously made methods for the RPGMVXA engine. You showed me that I needed to add a line of code to this method:

  def self.create_game_objects
    $game_temp          = Game_Temp.new    #original command
    $game_system        = Game_System.new  #original command
    #ect;
    $your_array         = [0,10,25,55,99]  #my command
  end

If I do not include the original commands in the body of that method, will they be excluded from the method? If I want to keep them, do I put "super"? If I don't want to keep them and not including them doesn't change anything, how would I exclude them?

 

Sorry if this is rather basic knowledge. Some scripts I've seen have both excluded and included original commands, and it has me rather unsure why.

Edited by Zen Blood
Crossed out figured out stuff.

Share this post


Link to post
Share on other sites

It's been a while, but I finally have gotten around to setting up the hash and variables beyond initiating them. I was able to initiate the hash and variables upon the game starting just fine!

However, when I set up how the hash loads, I get this error.

Error.png.16175c124720690077d8cd648e290292.png


Here's what the three script snippets look like right now:

Spoiler

 


  #--------------------------------------------------------------------------
  # * Create Game Objects
  #--------------------------------------------------------------------------  
  def self.create_game_objects
    $game_temp          = Game_Temp.new
    $game_system        = Game_System.new
    $game_timer         = Game_Timer.new
    $game_message       = Game_Message.new
    $game_switches      = Game_Switches.new
    $game_variables     = Game_Variables.new
    $game_self_switches = Game_SelfSwitches.new
    $game_actors        = Game_Actors.new
    $game_party         = Game_Party.new
    $game_troop         = Game_Troop.new
    $game_map           = Game_Map.new
    $game_player        = Game_Player.new
    
    #MY OBJECTS (NO ERROR)
    #==========
    $lureobjects        = { } 
    $lure_food          = "food"
    $event_to_erase     = 0
  end
  
  #--------------------------------------------------------------------------
  # * Create Save Contents
  #--------------------------------------------------------------------------
  def self.make_save_contents
    contents = {}
    contents[:system]        = $game_system
    contents[:timer]         = $game_timer
    contents[:message]       = $game_message
    contents[:switches]      = $game_switches
    contents[:variables]     = $game_variables
    contents[:self_switches] = $game_self_switches
    contents[:actors]        = $game_actors
    contents[:party]         = $game_party
    contents[:troop]         = $game_troop
    contents[:map]           = $game_map
    contents[:player]        = $game_player
    
    #MY OBJECTS (NO ERROR)
    #==========
    contents[:lureobj]       = $lureobjects
    contents[:lurefood]      = $lure_food 
    contents[:event2erase]   = $event_to_erase
    #----------
    contents
  end
  
  #--------------------------------------------------------------------------
  # * Extract Save Contents
  #--------------------------------------------------------------------------
  def self.extract_save_contents(contents)
    $game_system        = contents[:system]
    $game_timer         = contents[:timer]
    $game_message       = contents[:message]
    $game_switches      = contents[:switches]
    $game_variables     = contents[:variables]
    $game_self_switches = contents[:self_switches]
    $game_actors        = contents[:actors]
    $game_party         = contents[:party]
    $game_troop         = contents[:troop]
    $game_map           = contents[:map]
    $game_player        = contents[:player]
    
    #MY OBJECTS
    #==========    
    $lureobjects        = contents[:lureobj] #(CAUSES ERROR)
    $lure_food          = contents[:lurefood] #(NO ERROR)
    $event_to_erase     = contents[:event2erase] #(NO ERROR)
  end

 

 

 

 

Would anyone happen to know what I'm doing wrong?

Edited by Zen Blood
Updating with which code exactly is causing the error.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×