Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Using instance variable from "Scene_A" in "Scene_B"

PK8

Member

Basically, I need help figuring out how to use an instance variable from another scene (let's call it "Scene_A") in... let's call it Scene_B without relying on global variables to use it. I can be pretty rgss-stupid at times. So I was wondering if someone could kinda guide me on how to go about doing this step by step.
 
You could make it an object in Game_Temp (although I try to avoid using Game_Temp too, but sometimes you have to). Then just use $game_temp.punks_variable = ???

Code:
class Game_Temp
  attr_accessor :punks_variable
  alias_method :punk_gmtemp_init, :initialize
  def initialize
    @punks_variable = false
  end
end

Do the two scenes activate each other? At the end of the main loop or whenever Scene_A activates Scene_B (visa versa) you could do this in both classes...

Code:
class Scene_A
  def initialize(var)
    @var = var
    # ...Blah blah blah
  end
end

class Scene_B
  # Remember to do the same thing with the Initialize method here too...
  def update_command_cancel
    $game_system.se_play($data_system.cancel_se)
    $scene = Scene_A.new(@var)
  end
end

If Scene_A and Scene_B can be activated through other scenes, then I'd use the Game_Temp suggestion, but if not I'd just use the argument added to initialize in both scenes.
 
Err... the basic idea behind class variables is that they CANNOT be accessed from outside the class or its descendant (you are sure you are speaking of a class variable, one beginning with @@ ?).
So, the only way I see to usre a Class_A class variable from Class_B is to make Class_B a descendant of Class_B. Or to make Class_A and Class_B descendant of the same Class_Parent.

--------------------------------------------------------------------------------------

1st solution if Class_A and Class_B share more the class variables

Code:
class Class_A
  def initialize
    @@z = 0
  end

  def z=( val)
    @@z = val
  end
end

class Class_B < Classe_A
end

--------------------------------------------------------------------------------------

2nd solution if Class_A and Class_B have just have the class variable to share

Code:
class Class_Parent
  def initialize
    @@z = 0
  end

  def z=( val)
    @@z = val
  end
end

class Class_A < Classe_Parent
end

class Class_B < Classe_Parent
end
--------------------------------------------------------------------------------------

and a 3td solution that came up while I was writing the previous ones

Just create an instance of a sharing 3rd class in both Class_A and Class_B (more practical if Class_A and Class_B have different parent classes)

Code:
class Class_Shared
  def initialize
    @@z = 0
  end

  def z=(val)
    @@z = val
  end
end

class Class_A
  attr_accessor :shared

  def initialize
    @shared = Class_Shared.new
    ...
  end
end

class Class_B
  attr_accessor :shared

  def initialize
    @shared = Class_Shared.new
    ...
  end
end

you can even have

Code:
class Class_A < Class_A_Parent
class Class_B < Class_B_Parent

when instanciating Class_A and Class_B like this

Code:
a = Class_A.new
b = Class_B.new

you get the shared class variable via

Code:
a.shared.z
b.shared.z


Hope I made no mistake....
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top