Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Checking variables in a class from a class

edited June 2013 in General Posts: 105

If i for example create a Player class and then create a Animal class and from the Animal class i want to check a variable in the Player class. The variable in the Player class would be called: "self.animal". So how to i from the Animal class check if the variable is true or not?

Comments

  • dave1707dave1707 Mod
    Posts: 7,602

    Not sure if this is exactly what you're after.


    function setup()     a1=aa(100)    -- a1 instance of aa setting val to 100     a2=aa(200)    -- a2 instance of aa setting val to 200     b1=bb()       -- b1 instance of bb     b2=bb()       -- b2 instance of bb     print(b1:getVal(a1))  -- get value of a1 self.val from b1     print(b2:getVal(a2))  -- get value of a2 self.val from b2 end aa=class() function aa:init(v)     self.val=v end bb=class() function bb:init() end function bb:getVal(xx)     return xx.val end
  • Posts: 105

    Well... i am not so sure about return things and so, but i have found another way to check variables. You set the variables on "function player:setup()" and then in the Animal class you write:

    Animal=class

    function Animal:setup()
    Player:setup()
    if self.animal== true then
    command
    end
    end

    and so on...

    If you do this way you need to change the variables from self.animal to self.player.animal to sort things out. Thanks for answering anyway!

  • IgnatzIgnatz Mod
    Posts: 5,396

    @MMGames - the way you check variables in another class is quite simple

    if you wrote

    p=Player()
    

    and there is a property "self.score" in the Player class, then you can check its value for p with

    p.score
    
  • Posts: 105

    @Ignatz - Thanks, got it now

Sign In or Register to comment.