Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Checking code dependencies causes class instance to fire twice — Gideros Forum

Checking code dependencies causes class instance to fire twice

JohnJohn Member
edited March 31 in Bugs and issues
I’ve been having problems with code dependency and this setup fires twice. First time it prints nil and the second time it prints "hello"


File: cSpriteTileFloorFlip.lua
cSpriteTileFloorFlip = Core.class(cSpriteTileFloorFlipController)
function cSpriteTileFloorFlip:init()
    self.super.init(self, "hello")
end
File: cSpriteTileFloorFlipController.lua
cSpriteTileFloorFlipController = Core.class(Sprite)
 
function cSpriteTileFloorFlipController:init(message)
print(message)
end
File:main.lua
cSpriteTileFloorFlip.new()


cSpriteTileFloorFlip.lua code dependency has cSpriteTileFloorFlipController checked and it is this that causing nil

When I change

File: cSpriteTileFloorFlip.lua to
cSpriteTileFloorFlip = Core.class(Sprite)
function cSpriteTileFloorFlip:init()
end
It prints nil

When I remove the code dependency reference the nil goes away

Comments

  • hgy29hgy29 Maintainer
    Accepted Answer
    Hi @John,

    When you create a class that inherits from another, Gideros automatically calls the superclass init() function before calling the subclass init(), so basically your own call to self.super.init() is redundant.

    The subclass should be declared as follow:
    cSpriteTileFloorFlip = Core.class(cSpriteTileFloorFlipController, function() return "hello" end)
    function cSpriteTileFloorFlip:init()
    end
    Here, the second parameter of Core.class is a function that takes the arguments given to the new() calls of the subclass and returns the arguments that should be used to create the superclass instance.

    Likes: talis

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