Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Inheritance: adding parameters to a child class not in the parent class — Gideros Forum

Inheritance: adding parameters to a child class not in the parent class

MoKaLuxMoKaLux Member
edited December 2021 in General questions
hope you are all doing fine :)
I have a problem :/
I want to inherit from classes but that doesn't work for me, could you please have a look?
I would like to add parameters to a class that inherits another class but that doesn't work for me, could you please have a look?

I have the base class:
Tiled_Shape_Polygon = Core.class(Sprite)
 
function Tiled_Shape_Polygon:init(xworld, xparams, xlevelscale)
	-- params
	local params = xparams or {}
	params.x = xparams.x or 0
	params.y = xparams.y or 0
	...
	self.x = params.x
	self.y = params.y
end
Then I inherit it from this class:
--!NEEDS:../../tiled/tiled_polygon.lua
Collectibles = Core.class(Tiled_Shape_Polygon)
 
function Collectibles:init()
	self.x += 10 -- this works fine
	self.y += 10 -- this works fine
	...
end
So far so good, but I would like to add additional arguments to the Collectibles class, how can I do?

I tried:
function Collectibles:init(xparams2)
	local params2 = xparams2 or {} -- xparams2 will be a table adding extra stuff to collectibles
	...
end
and this: (because child expects same arguments as parent as seen in an old post from ar2rsawseen here https://forum.gideros.rocks/discussion/comment/22775/#Comment_22775)
function Tiled_Shape_Polygon:init(xworld, xparams, xlevelscale, xparams2)
	-- params
	local params = xparams or {}
	local params2 = xparams2 or {}
	...
end
 
function Collectibles:init(xworld, xparams, xlevelscale, xparams2)
	local params2 = xparams2 or {} -- xparams2 will be a table adding extra stuff to collectibles
	...
end
...

But every time I have this error: attempt to index local 'xparams2' (a nil value)

How can I add extra parameters to Collectibles init without adding them to the parent class? Is it possible?

Thank you in advance for your help ;)
my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories

Comments

  • MoKaLuxMoKaLux Member
    edited December 2021
    So there are different workarounds:
    1) check inside particle2D:init which arguments have been passed, so you could determine who is calling constructor and what to do
    2) in particle2D leave the init without argumetns and create antoher method like particle2D:create(texRegion, ID) which you could call from blob:init
    3) Do not inherit from particle2D but make the instance of it inside blob:init and store as a property so you could use it for anything you wanted to use it

    In the first post I believe I tried solution 1) which didn't work for me

    So solution 2) looks good enough B)

    Viva Gideros <3
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • rrraptorrrraptor Member
    edited December 2021 Accepted Answer
    @MoKaLux
    Foo = Core.class(Sprite)
    function Foo:init(baz)
    	self.baz = baz or 1024
    end
     
    Bar = Core.class(Foo, function(baz, delta) 
    	return baz or 2048 
    end)
     
    function Bar:init(baz, delta)
    	self.delta = delta or 0.001
    end
     
    foo = Foo.new()
    print(foo.baz)
    bar = Bar.new()
    print(bar.baz, bar.delta)
    bar = Bar.new(512, 0.1)
    print(bar.baz, bar.delta)
    Outputs:
    1024
    2048 0.001
    512 0.1

    Likes: hgy29, MoKaLux

    +1 -1 (+2 / -0 )Share on Facebook
Sign In or Register to comment.