Checking variables in a class from a class

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?

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

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!

@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

@Ignatz - Thanks, got it now