Global Variables from Game_Interpreter?

I've been looking for instructions/tutorials to remove the global variables I've used, but when I follow the directions I found, it does not work. I've tried to put in attr_accessor in the game interpreter, and game_actor but the information does not pass through unless I use a global variable. I also tried making a game temp to do this, again no good.
Here is what I have setup to try and pass through variable/switch data from game interpreter.
Code:
class Game_Interpreter
  attr_accessor   :sublevelup
  attr_accessor   :sublevelcnt
  def command_316
    $game_temp.sublevelup = true
    value = operate_value(@params[2], @params[3], @params[4])
    $game_temp.sublevelcnt = value
    iterate_actor_var(@params[0], @params[1]) do |actor|
      actor.change_level(actor.level + value, @params[5])
    end
  end
end

class Game_Temp
  attr_accessor   :sublevelup
  attr_accessor   :sublevelcnt
  alias r2_old_init_92sd initialize
  def initialize
    r2_old_init_92sd
    @sublevelup = false
    @sublevelcnt = 0
  end
 
end

class Game_Actor < Game_Battler
 
  attr_accessor   :sublevelup
  attr_accessor   :sublevelcnt

However what works is if I use global variables and do this
Code:
class Game_Interpreter
  def command_316
    $sublevelup = true
    value = operate_value(@params[2], @params[3], @params[4])
    $sublevelcnt = value.to_i
    iterate_actor_var(@params[0], @params[1]) do |actor|
      actor.change_level(actor.level + value, @params[5])
    end
  end
end

class Game_Actor < Game_Battler

Advice?
Manually add accessor. Don't use attr_accessor
Code:
def sublevelup=(levelup)
  $game_temp.sublevelup = levelup
end

def sublevelup
  $game_temp.sublevelup
end
Game_Interpreter is instanced a lot iirc, and variables made by attr_accessor are instance based. What exists in one instance of GI wont exist in another.

Also, you made attr_accessor for sublevelup on those 3 classes, and each one of those are different. The value of sublevelup for Game_Interpreter is different than that on Game_Temp, than that of Game_Actor. They will not share one value ever.

You should probably study how instance variables work.

Anyway, Theo's suggestion is a good example of how to achieve what you want. You only actually define the sublevelup value in one class (in this case Game_Temp), and then have the other classes reference that same variable (you can also opt to just call the value from.game temp instead of making method calls in each class that will use it). That way you can share the value virtually anywhere.
since you made attr_accessor for sublevel on those 3 classes, each one of those are different. The value of sublevelup for Game_Interpreter is different than that on Game_Temp, than that of Game_Actor
Ok, good to know. I had always thought that it carried from one to another if I used the attr_accessor

Manually add accessor.
Never heard of that, I don't think. Have to look up some more. Not sure where you would put that code. Interpreter, temp, actor?
But thanks, moving down the right road now I guess.
attr_accessor is only a shortcut to making instance variables that are usable with class.variable_name 🙂 Again, read up on instance variables if needed. For one, the map has a different GI instance than the battle scene, so the value of the attr_accessor variable in the map would be different than that on battle even if its the same class.

The basic idea of Theo's suggestion is that the variable will only exist in one class. In this case, in Game_Temp. Then you just reference it from other classes or anywhere else you need that value. You can opt to make a new method for the reference as Theo wrote, or you can also just call directly the variable from Game_Temp.

Example:

Code:
class Game_Temp
  attr_accessor :sublevelup
end

Class Game_Interpreter
  def sublevelup
    return $game_temp.sublevelup
  end
end

Now if you call sublevelup from GI, it will return the value of the variable from Game_Temp instead. So basically, you now have a "shared" variable.

If you dont want to make a sublevelup method for the classes that will use it, you can always just call $game_temp.sublevelup directly. What's important here is you have the attr_accessor :sublevelup line only on Game_Temp.

PS: I think its safer to put it on Game_System instead as GS is saved in the save file so the data will persist as the GS wont be instanced again in that playthru. Not sure about Game_Temp.
Apologies, still doing something wrong
Code:
class Game_Interpreter
  def command_316
    @sublevelup = true
    value = operate_value(@params[2], @params[3], @params[4])
    @sublevelcnt = value
    iterate_actor_var(@params[0], @params[1]) do |actor|
      actor.change_level(actor.level + value, @params[5])
    end
  end
  def sublevelup
    return $game_actor.sublevelup
  end
  def sublevelcnt
    return $game_actor.sublevelcnt
  end
end

class Game_Actor < Game_Battler
 
  attr_accessor   :sublevelup
  attr_accessor   :sublevelcnt
They are instance variables. You need a specific instance of Game_Actor for it to work the way you wrote it. Right now, I dont think there is an instance of Game_Actor called $game_actor. The game only instances Game_Actor when they create a new actor and that is referenced via $game_actors[id] iirc.

This is exactly why I suggested making this in either Game_Temp or Game_System, because the game auto-instances them into $game_temp and $game_system.

If you need it per actor, then you need to reference the actual actor instance.

Also, your current code still has two different sets of variables. Doing @sublevelup on GI means you're defining an instance variable on GI called sublevelup, it doesnt call the method that you made.

What exactly do you need the sublevel and sublevelcnt to do? Your current method only sets values but never uses them (and you set them only on GI but not on Game_Actor). And do you need it per actor, or as a value shared by all?

If its per actor and is meant to denote that he went thru a sublevel and count how many times, this should work.

Code:
class Game_Interpreter
  def command_316
   value = operate_value(@params[2], @params[3], @params[4])
   iterate_actor_var(@params[0], @params[1]) do |actor|
     actor.change_level(actor.level + value, @params[5])
     #This sub-part already defined the specific actor instance as "actor" so we just use it
     #Set sublevelup of the actor to true
     actor.sublevelup = true
     #Increase count by how many levels are done?
     actor.sublevelcnt += value
   end
  end
end

class Game_Actor < Game_Battler
  #Create instance variables
  attr_accessor   :sublevelup
  attr_accessor   :sublevelcnt
end

This would set the sublevelup variable of that specific actor to true, and increase sublevelcnt by the value whenever you use command 316.

If you need to know the value of sublevelcnt for an actor, just do $game_actors[id].sublevelcnt

PS: Again, please read up and study how Instance Variables work and how they are used. It would help a lot 🙂
I'm not exactly sure what are you trying to do and I'm too tired to process the whole thing you want, but I'll try to explain attr_accessor and the code I posted.

Basically when u declare attr_accessor :varname, it is equal to writing this
Code:
class MyClass
  def varname
    return @varname
  end

  def varname=(value)
    @varname = value
  end
end

If you declare attr_writer :varname, it's equal to writing this
Code:
class MyClass
  def varname=(value)
    @varname = value
  end
end

If you declare attr_reader :varname, it's equal to writing this
Code:
class MyClass
  def varname
    return @varname
  end
end

If you code like this
Code:
class MyClass
  def something=(newthing)
    puts newthing
  end
end
And in the following line, if you write this
Code:
obj = MyClass.new
obj.something = "hello world"
It will print "hello world"

In another word, using the character "=" is just a fancy way to pass an argument. But because of its fanciness, it usually used as a setter/writer. However, you're free to process everything after the argumen is passed, just like regular method name.

Example in the base script, Game_BattlerBase > Line 462, we have this
Code:
  def hp=(hp)
    @hp = hp
    refresh
  end
It's to ensure that if you use $game_actors[x].hp = 1000
after assigning the new hp value, it will refresh.
What exactly do you need the sublevel and sublevelcnt to do?
They are values used to control if a subclass gains a level and by how much from the levelup command (command_316)
then the values are reset in game actor when the subclass goes up a level.

Apologies for the confusion. I understood accessor a bit but not about each class made it's own.
I also did not see the |actor| part, like you wrote, just use it as it is referenced.
I had thought that attr_acessor would reference the same value in each class, since it was the same name.

This works
Code:
class Game_Interpreter
  def command_316
   value = operate_value(@params[2], @params[3], @params[4])
   iterate_actor_var(@params[0], @params[1]) do |actor|
     actor.sublevelup = true
     actor.sublevelcnt = value
     actor.change_level(actor.level + value, @params[5])
   end
  end
end

class Game_Actor < Game_Battler
 
  attr_accessor   :sublevelup
  attr_accessor   :sublevelcnt

Thanks
The engine has a lot of variable types (globals, locals, instance, class vars etc), so it can indeed be confusing at first. I suggest reading the help file as they are all documented there. It will help a lot on your journey 🙂

Original thread: https://forums.rpgmakerweb.com/threads/global-variables-from-game_interpreter.114090/