Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Inherit attributes — Gideros Forum

Inherit attributes

somezombiegmsomezombiegm Member
edited March 2014 in General questions
Hello, I want my "plane" class to inherit from my "vehicle" class, but I don't know how to add my vehicle attributes to the plane attributes.

I'm thinking the solution could be calling from "plane:init" the vehicle.init, but I don't know how to do it. In java you could call the superior constructor by using "this".

Please help. Here is my test code:
vehicle = gideros.class(Sprite)
 
function vehicle:init(name, speed, passengers)
	self.name = name
	self.speed = speed
	self.passengers = passengers
end
------------------------------------
plane = gideros.class(vehicle)
 
function plane:init(name, speed, passengers, height)
	self.height = height
        -- in here should I call the vehicle's init??
end
------------------------------------
vehicleTest = gideros.class(Sprite)
 
function vehicleTest:init()
	obj = plane.new("name", 100, 30, 3000)
	print(obj.height)
	print(obj.passengers)
end

Comments

  • ar2rsawseenar2rsawseen Maintainer
    the init of the parent class is called automatically, even before your classes init is called

    meaning when function plane:init is called, all the name, speed, etc properties should already be set, because vehicle:init already did that
  • ar2rsawseenar2rsawseen Maintainer
    This way for example, you can extend native classes, like creating CenteredBitmap
     
    CenteredBitmap = Core.class(Bitmap)
     
    function CenteredBitmap:init(texture)
        self:setAnchorPoint(0.5, 0.5)
    end
     
    stage:addChild(CenteredBitmap.new(Texture.new("myimage.png", true)))
  • somezombiegmsomezombiegm Member
    edited March 2014
    Ok, but how can I retrieve the passengers attribute? when I print
    print(obj.passengers)
    it returns me a nil value
  • ar2rsawseenar2rsawseen Maintainer
    Really?
    I just tried your example and it works as expected, printing all the attributes

    Which Gideros Version are you using?
  • Yeah, but it doesn't print it right either, it prints 30 rather than 3000.

    I'm using Gideros v2014.01 for windows
  • ar2rsawseenar2rsawseen Maintainer
    edited March 2014
    So does it print nil or values? :D

    Here is my test result, that works correctly for me
    vehicle = gideros.class(Sprite)
     
    function vehicle:init(name, speed, passengers)
     self.name = name
     self.speed = speed
     self.passengers = passengers
    end
    ------------------------------------
    plane = gideros.class(vehicle)
     
    function plane:init(name, speed, passengers, height)
     self.height = height
    end
    ------------------------------------
    vehicleTest = gideros.class(Sprite)
     
    function vehicleTest:init()
    	obj = plane.new("name", 100, 30, 3000)
    	print(obj.name, obj.speed, obj.passengers, obj.height)
    	--prints name 100 30 3000
    end
     
    vehicleTest.new()
  • somezombiegmsomezombiegm Member
    edited March 2014
    I edited the init on plane cause it needed and extra argument for the object I think. So I changed this:
    function plane:init(name, speed, passengers, height)
    to this:
    function plane:init(newObj, name, speed, passengers, height)
    However I can't retrieve the vehicle values, they print nil values. I don't even think they're being initialized correctly.

    Let me test your last comment...
  • ar2rsawseenar2rsawseen Maintainer
    if you pass newObj as first argument and it is meant only for plane, then you have a problem, because first three arguments will be automatically used by vehicle class as name, speed and passengers
  • The newObj change I made was because I tried to create the object using ":" but I change it back again now.

    So I tried your exact code and it prints

    nil nil nil 3000 :(
  • Wait, it works! But I added all the code to main.lua. I had each file separated on vehicle.lua, plane.lua and vehicleTest.lua. Sorry for not specifying.

    But I would preffer doing it my way, cause my actual code is pretty large, that's just a test code. So, any thoughts why it doesn't work separating the files?
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    @somezombiegm ah I see
    it makes sense now
    click on the plain.lua in your project manager and select Code Dependencies
    and there select vehicle.lua
    So it would always load vehicle class before plain class

    ;)

    Likes: somezombiegm

    +1 -1 (+1 / -0 )Share on Facebook
  • Yes! that fixed it! But I still don't understand why it works. Specially this part:
    function vehicle:init(name, speed, passengers)
    	self.name = name
    	self.speed = speed
    	self.passengers = passengers
    end
    I never call vehicle.new and send those 3 parameters. Does it know I am calling the plane class and gets the parameters in order of appearance automatically?

    One more thing. I change the plane class parameters leaving only height and it still works:
    function plane:init(height)
     self.height = height
    end
    Man I'm so confused :(
  • ar2rsawseenar2rsawseen Maintainer
    edited March 2014
    Basically it automatically passes to the parent class all the parameters you pass to the current class constructor (but only constructor, not other methods)

    And it should not have worked with simply height
    if you only accept height in plane, then height should have value "name"

    Likes: somezombiegm

    +1 -1 (+1 / -0 )Share on Facebook
  • It prints all the values when I put only height on "plane" though :/
  • ar2rsawseenar2rsawseen Maintainer
    @somezombiegm and correct values?
    in you example self.height should be name.

    Can you post a complete example for me to try :)
  • I'm sorry, I triple checked it and it does't work with only height on plane. The working code is:

    vehicle.lua
    vehicle = gideros.class(Sprite)
     
    function vehicle:init(name, speed, passengers)
     self.name = name
     self.speed = speed
     self.passengers = passengers
    end
    plane.lua
    plane = gideros.class(vehicle)
     
    function plane:init(name, speed, passengers, height)
     self.height = height
    end
    vehicleTest.lua
    vehicleTest = gideros.class(Sprite)
     
    function vehicleTest:init()
    	obj = plane.new("name", 100, 30, 3000)
    	print(obj.name, obj.speed, obj.passengers, obj.height)
    end
     
    vehicleTest.new()
    and it prints: name 100 30 3000

    Thanks a lot for your help!
  • ar2rsawseenar2rsawseen Maintainer
    yes but here you have
    function plane:init(name, speed, passengers, height)
  • somezombiegmsomezombiegm Member
    edited March 2014
    Yeah, what I meant is that with
    function plane:init(height)
    doesn't work. I thought it did but I wasn't correctly checking the print. It printed name 100 30 name :\">

    @ar2rsawseen Thanks
Sign In or Register to comment.