No experience scripting, multiple goals, requesting help

Zwataketa

Villager
Member
Joined
Nov 10, 2020
Messages
22
Reaction score
2
First Language
English
Primarily Uses
RMXP
Did that, but now the GlobalVar file isn't being created when it should be, and Global Variables aren't read from the file either ... Loading a save resets the variables to whatever they were on the save file.
Code:
module GlobalVar
  @@global_variable_data = nil
 
  def self.save
    @@global_variable_data = $game_variables.global_variables
    file = File.open('GlobalVar.rxdata', 'wb')
    Marshal.dump(@@global_variable_data, file)
    file.close
  end
 
  def self.load
    return @@global_variable_data unless @@global_variable_data.nil?
   
    file = File.open('GlobalVar.rxdata', 'rb')
    @@global_variable_data = Marshal.load(file)
    file.close
    @@global_variable_data
  end
 
  def self.exist?
    File.exist?('GlobalVar.rxdata')
  end
 
end

No Error messages this time.
 

KK20

Just some XP Scripter
Veteran
Joined
Oct 11, 2018
Messages
347
Reaction score
131
First Language
English
Primarily Uses
RMXP
That's not the entirety of your script right? When I put them all together, it looked fine to me.
 

Zwataketa

Villager
Member
Joined
Nov 10, 2020
Messages
22
Reaction score
2
First Language
English
Primarily Uses
RMXP
That's not the entirety of your script right? When I put them all together, it looked fine to me.
Here's the entire GlobalVar script:
Code:
RANGE_OF_VARS = 50..52

module GlobalVar
  @@global_variable_data = nil
  
  def self.save
    @@global_variable_data = $game_variables.global_variables
    file = File.open('GlobalVar.rxdata', 'wb')
    Marshal.dump(@@global_variable_data, file)
    file.close
  end
  
  def self.load
    return @@global_variable_data unless @@global_variable_data.nil?
    
    file = File.open('GlobalVar.rxdata', 'rb')
    @@global_variable_data = Marshal.load(file)
    file.close
    @@global_variable_data
  end
  
  def self.exist?
    File.exist?('GlobalVar.rxdata')
  end
  
end

#module Graphics
#  class << self
#    alias clear_gs_cache_on_update update
#    def update
#      GlobalVar.clear_cache
#      clear_gs_cache_on_update
#    end
#  end
#end


class Game_Variables
  
  alias get_variable []
  def [](id)
    if id.between?(RANGE_OF_VARS.first, RANGE_OF_VARS.last) && GlobalVar.exist?
      @data.slice!(RANGE_OF_VARS)
      @data.insert(RANGE_OF_VARS.first, *GlobalVar.load)
    end
    get_variable(id)
  end
  
  alias set_variable []=
  def []=(id, val)
    set_variable(id, val)
    if id.between?(RANGE_OF_VARS.first, RANGE_OF_VARS.last)
      GlobalVar.save
    end
  end

  def global_variables
    @data[RANGE_OF_VARS.last] ||= 0
    @data[RANGE_OF_VARS]
  end
end
 

KK20

Just some XP Scripter
Veteran
Joined
Oct 11, 2018
Messages
347
Reaction score
131
First Language
English
Primarily Uses
RMXP
Yeah, that's exactly what I have and it worked fine.
 

Zwataketa

Villager
Member
Joined
Nov 10, 2020
Messages
22
Reaction score
2
First Language
English
Primarily Uses
RMXP
Yeah, that's exactly what I have and it worked fine.
So, if you were to delete GlobalVar and then try to change one of the global variables afterward, it recreates the file as intended?
 

KK20

Just some XP Scripter
Veteran
Joined
Oct 11, 2018
Messages
347
Reaction score
131
First Language
English
Primarily Uses
RMXP

Zwataketa

Villager
Member
Joined
Nov 10, 2020
Messages
22
Reaction score
2
First Language
English
Primarily Uses
RMXP
WAIT I'm silly. The variable I was trying to change was out of the specified range, and I never updated the range. Doh.

Yeah, with that knowledge, it works now. Sorry for the confusion.

And now Yuka can be a reindeer in game without fiddling with the debug menu. Yay. 1611816243643.png
 

Latest Threads

Latest Posts

Latest Profile Posts

Do you like to practice level design? I do hahahaha! It's not from a specific game, it's just some tests...Fase 1.png
I really need a better indicator of where you can jump up and down though I feel... otherwise I like how this turned out for the first part of the first dungeon of the game.
@Shaz needs to make a website for his plugins. (it can be done easily for free with google sites.) I can't find a database of them anywhere!
Dion2.jpg

testing #2

Forum statistics

Threads
109,126
Messages
1,042,371
Members
141,627
Latest member
gd8
Top