[SOLVED] Referring To Instance Variables From Another Class

Discussion in 'RGSSx Script Support' started by Rikifive, Sep 17, 2015.

Thread Status:
Not open for further replies.
  1. Rikifive

    Rikifive Bringer of Happiness Veteran

    Messages:
    1,442
    Likes Received:
    676
    Location:
    Poland
    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: Nov 8, 2015
    #1
  2. Sixth

    Sixth Veteran Veteran

    Messages:
    2,130
    Likes Received:
    798
    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.
     
    #2
  3. KockaAdmiralac

    KockaAdmiralac Cube-shaped garbage can Veteran

    Messages:
    570
    Likes Received:
    156
    Location:
    Belgrade
    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: Sep 17, 2015
    #3
  4. Rikifive

    Rikifive Bringer of Happiness Veteran

    Messages:
    1,442
    Likes Received:
    676
    Location:
    Poland
    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.

    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).
     
    #4
  5. Sixth

    Sixth Veteran Veteran

    Messages:
    2,130
    Likes Received:
    798
    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.
     
    #5
  6. Rikifive

    Rikifive Bringer of Happiness Veteran

    Messages:
    1,442
    Likes Received:
    676
    Location:
    Poland
    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
     
    #6
  7. Sixth

    Sixth Veteran Veteran

    Messages:
    2,130
    Likes Received:
    798
    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.
     
    #7
    Rikifive likes this.
  8. Rikifive

    Rikifive Bringer of Happiness Veteran

    Messages:
    1,442
    Likes Received:
    676
    Location:
    Poland
    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: Sep 17, 2015
    #8
  9. Sharm

    Sharm Pixel Tile Artist Veteran

    Messages:
    12,615
    Likes Received:
    10,300
    Location:
    USA
    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.
     
    #9
Thread Status:
Not open for further replies.

Share This Page