[SOLVED] Referring To Instance Variables From Another Class

Status
Not open for further replies.

Rikifive

Bringer of Happiness
Veteran
Joined
Jun 21, 2015
Messages
1,441
Reaction score
680
First Language
Polish
Primarily Uses
Other
Hello everybody!
 
I would like to ask how to refer to a instance variable from another class.
So, let's say I have something like this:

class Warrior < Sorcerer # stuff here it totally random and that's not important def init_variables @turtle = 0 # so I have that variable here and the stuff is happening with this... end def smell_da_turtle something here IDK, for example if smell_da_turtle @turtle += 1 # here... else lol end endendclass Windows_XP < Window_Base # and we have different class here - #the window where I want to put that variable in. (initialize...) def refresh contents.clear draw_text(0, 0, 100, 24, @turtle) #I want to do something like this end (other stuff...)end
Don't mind the messy examples and such, I just want to know how to refer to that variable in different class.
 
Why am I asking?
Because I'm modifying Galv's Invader Minigame and there's lots of local variables in minigame's part.
What I want is to put these variables in SCORE WINDOW located at the bottom - just to show these values via draw_text etc. and that's all.
How can I do this?

Thank you.
 
Last edited by a moderator:

Sixth

Veteran
Veteran
Joined
Jul 4, 2014
Messages
2,162
Reaction score
822
First Language
Hungarian
Primarily Uses
RMVXA
Using your example class:

You place the following line in the Warrior class:

attr_reader :turtleThis will make the variable to be seen outside of it's class.You should see many 'attr_reader' and 'attr_accessor' lines in Galv's script, btw, so check them out.

Effectively, this equals this piece of code:

def turtle @turtleendNow, search for this in the code:
Code:
@something = Warrior.new*or*$something = Warrior.new
See that '@something' variable? That contains the @turtle variable mentioned above, the thing you want to draw.You can access the @turtle variable outside of it's own class by using this:

@something.turtleSo, first the variable name which stores the class instance and after that comes the variable from the class itself.Now, since we are always on a scene, both the Warrior and the Windows_XP class instances should be available in the scene class itself.

Simply add an argument to your refresh method in Windows_XP, and pass the '@something.turtle' variable for it.

And finally, use the value of the argument itself in the refresh method in your Windows_XP class.

If the variable which stores the Warrior class happens to be a global variable (it's name starting with a $ sign), you can use it in any class by calling '$something.turtle' directly, as long as you define a reader method (att_reader) for the @turtle variable in the Warrior class.

This should be it, in short.
 

KockaAdmiralac

Cube-shaped garbage can
Veteran
Joined
Jun 15, 2015
Messages
569
Reaction score
153
First Language
Serbian
Primarily Uses
N/A
Line 229 and after it some more lines in Galv's script are

def create_stats    @score_window = Window_InvaderScore.new    @score_window  endThe window is already created, you don't need to create your own, you just need to change the Window_InvaderScore
 
Last edited by a moderator:

Rikifive

Bringer of Happiness
Veteran
Joined
Jun 21, 2015
Messages
1,441
Reaction score
680
First Language
Polish
Primarily Uses
Other
Hmm. I think I did something wrong, because it still gives me null. =P

So... trying directly on that minigame:

I've put "attr_reader :guns" directly under "class Scene_Invaders < Scene_Base"

then... I'm not sure how exactly should I setup that part: "@something = Warrior.new" =P

I tried putting @stuff = Scene_Invaders.new under "class Window_InvaderScore < Window_Base" but that was wrong I guess?

And finally putting "@stuff.guns" in draw_text line, but it gave me null.

Sorry for being so noobish.

Line 229 and after it some more lines in Galv's script are

def create_stats    @score_window = Window_InvaderScore.new    @score_window  endThe window is already created, you don't need to create your own, you just need to change the Window_InvaderScore
That's what I'm trying to do - to put local variables from Scene_Invaders class to that window.

But when I'll put for example "guns" then it gives me error, when I'll try putting @guns it gives me null (displays 0 all the time).
 

Sixth

Veteran
Veteran
Joined
Jul 4, 2014
Messages
2,162
Reaction score
822
First Language
Hungarian
Primarily Uses
RMVXA
If the variable is made in the scene itself, you don't need that attr_reader part. Simply pass that variable as an argument for the refresh method of the score window.
 

Rikifive

Bringer of Happiness
Veteran
Joined
Jun 21, 2015
Messages
1,441
Reaction score
680
First Language
Polish
Primarily Uses
Other
You mean simply putting:

draw_text(5, 5, 100, 24, @guns.to_i)

in refresh method in class Window_InvaderScore < Window_Base?

Then it gives me null.

When I'll try guns without "@" it gives me undefinied local variable method error.

What am I doing wrong? =P
 

Sixth

Veteran
Veteran
Joined
Jul 4, 2014
Messages
2,162
Reaction score
822
First Language
Hungarian
Primarily Uses
RMVXA
You got a refresh method in the score window, right? That is where you draw the stuffs.

Make an argument for that method. Meaning that instead of simply defining it as ”def refresh”, you define it as ”def refresh(guns)”, the 'guns' is the argument.

And in the scene itself, search the place where it calls this refresh method, and change it to ”@scorewin.refresh(@guns)”.

This is how you get outside variables to any method, with arguments.
 

Rikifive

Bringer of Happiness
Veteran
Joined
Jun 21, 2015
Messages
1,441
Reaction score
680
First Language
Polish
Primarily Uses
Other
Oh I got this now!

What is more, now I see how the "score" and "hi-score" were imported here.

Sure this is useful to know.

Thank you so much for help! (=

*Now I'm going to completely improve this game by adding many functions, such as stages etc. =>*
 
Last edited by a moderator:

Sharm

Pixel Tile Artist
Veteran
Joined
Nov 15, 2012
Messages
12,760
Reaction score
10,884
First Language
English
Primarily Uses
N/A
This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.
 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,862
Messages
1,017,050
Members
137,571
Latest member
grr
Top