Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Override new function — Gideros Forum

Override new function

dddentdddent Member
edited April 2014 in General questions
Hello,

I'm trying to override the new function of a class that inherits the Sprite class, but I always get an error and I'm not sure why. Here is the code and the error:

Code:
Rectangle = Core.class(Sprite)
 
Rectangle.__new = Rectangle.new
 
Rectangle.new = function (x, y, width, height)
	self = Rectangle.__new()
	self:setX(x)
	self:setY(y)
 
	self:beginPath()
	self:setLineStyle(2)
	self:setFillStyle(Shape.SOLID, 0xFF0000)
	self:moveTo(0,0)
	self:lineTo(x,0)
	self:lineTo(x,y)
	self:lineto(0,y)
	self:lineTo(0,0)
	self:endPath()
 
	return self
end
 
rect = Rectangle.new(50,50,100,100)
 
stage:addChild(rect)
Error:
button.lua is uploading.
Uploading finished.
[string "property.lua"]:59: stack overflow
stack traceback:
	[string "property.lua"]:59: in function '__new'
	[string "property.lua"]:59: in function '__new'
	[string "property.lua"]:59: in function '__new'
	[string "property.lua"]:59: in function '__new'
	[string "property.lua"]:59: in function '__new'
	[string "property.lua"]:59: in function '__new'
	[string "property.lua"]:59: in function '__new'
	[string "property.lua"]:59: in function '__new'
	[string "property.lua"]:59: in function '__new'
	[string "property.lua"]:59: in function '__new'
	...
	[string "property.lua"]:59: in function '__new'
	[string "property.lua"]:59: in function '__new'
	[string "property.lua"]:59: in function '__new'
	[string "property.lua"]:59: in function '__new'
	[string "property.lua"]:59: in function '__new'
	[string "property.lua"]:59: in function '__new'
	[string "property.lua"]:59: in function '__new'
	button.lua:8: in function 'new'
	button.lua:25: in main chunk
Thanks for help

cheers
David

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited April 2014
    @dddent due to complections of postInit and some other Class system stuff, you can't override new function, but you can override __new function to achieve same effect:
    Rectangle = Core.class(Sprite)
     
    Rectangle._new = Rectangle.__new
     
    Rectangle.__new = function (x, y, width, height)
    	self = Rectangle._new()
    	self:setX(x)
    	self:setY(y)
     
    	self:beginPath()
    	self:setLineStyle(2)
    	self:setFillStyle(Shape.SOLID, 0xFF0000)
    	self:moveTo(0,0)
    	self:lineTo(x,0)
    	self:lineTo(x,y)
    	self:lineto(0,y)
    	self:lineTo(0,0)
    	self:endPath()
     
    	return self
    end
     
    rect = Rectangle.new(50,50,100,100)
     
    stage:addChild(rect)

    Likes: dddent

    +1 -1 (+1 / -0 )Share on Facebook
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    But in your case, you could have done simply:
    Rectangle = Core.class(Sprite)
     
    function Rectangle:init(x, y, width, height)
    	self:setX(x)
    	self:setY(y)
     
    	self:beginPath()
    	self:setLineStyle(2)
    	self:setFillStyle(Shape.SOLID, 0xFF0000)
    	self:moveTo(0,0)
    	self:lineTo(x,0)
    	self:lineTo(x,y)
    	self:lineto(0,y)
    	self:lineTo(0,0)
    	self:endPath()
    end
     
    rect = Rectangle.new(50,50,100,100)
     
    stage:addChild(rect)

    Likes: dddent

    +1 -1 (+1 / -0 )Share on Facebook
  • dddentdddent Member
    edited April 2014
    @dddent due to complections of postInit and some other Class system stuff, you can't override new function, but you can override __new function to achieve same effect:
    Rectangle = Core.class(Sprite)
     
    Rectangle._new = Rectangle.__new
     
    Rectangle.__new = function (x, y, width, height)
    	self = Rectangle._new()
    	self:setX(x)
    	self:setY(y)
     
    	self:beginPath()
    	self:setLineStyle(2)
    	self:setFillStyle(Shape.SOLID, 0xFF0000)
    	self:moveTo(0,0)
    	self:lineTo(x,0)
    	self:lineTo(x,y)
    	self:lineto(0,y)
    	self:lineTo(0,0)
    	self:endPath()
     
    	return self
    end
     
    rect = Rectangle.new(50,50,100,100)
     
    stage:addChild(rect)
    Hmm, I rewrote everything, and then overriding new as in my code worked fine, but I have no idea why it does work now and you saying, that it shouldn't work in the first place makes it even weirder...

    I'll post the code, but due to some changes I made, its not working again, I'll post the error below, maybe s.o. can also help me with that:
    Rectangle = Core.class(Shape)
     
    Rectangle.b = Rectangle.new
     
    function Rectangle.new(x,y,width,height, bwidth, bcolor, fcolor)
    	self = Rectangle.b()
     
    	self:setX(x)
    	self:setY(y)
     
    	self.width = width
    	self.height = height
     
    	self.drawbg = function (_bwidth, _bcolor, _fcolor)
    		print(type(_bwidth))
    		self:beginPath()
    		self:setLineStyle(_bwidth or 1, _bcolor or 0x000000)
    		self:setFillStyle(self.SOLID, _fcolor or 0x505050)
    		self:moveTo(0,0)
    		self:lineTo(self.width,0)
    		self:lineTo(self.width, self.height)
    		self:lineTo(0,self.height)
    		self:lineTo(0,0)
    		self:endPath()
    	end
     
    	self.reset = function ()
    		self:beginPath()
    		self:setLineStyle(bwidth or 1, bcolor or 0x000000)
    		self:setFillStyle(self.SOLID, fcolor or 0x505050)
    		self:moveTo(0,0)
    		self:lineTo(self.width,0)
    		self:lineTo(self.width, self.height)
    		self:lineTo(0,self.height)
    		self:lineTo(0,0)
    		self:endPath()
    	end
     
    	self:drawbg()
     
    	return self
    end
    Error:
    button.lua is uploading.
    Uploading finished.
    table
    button.lua:17: bad argument #1 to 'setLineStyle' (number expected, got table)
    stack traceback:
    	button.lua:17: in function 'drawbg'
    	button.lua:39: in function 'new'
    	button.lua:50: in function 'new'
    	menu.lua:1: in main chunk
    EDIT: I intentionally didn't pass any arguments to drawbg() to underline, that the error even occurs when nil is passed as an argument for _bwidth, it always is a table...
  • ar2rsawseenar2rsawseen Maintainer
    @dddent sorry for confusion, I meant that there was an internal __new function, that you have overriden, thus it did not work

    about your last error then problem is that you define function with . and use it with :

    So

    self:drawbg() translates to self.drawbg(self) thus first parameter is always a table

    All in all, this should work:
    Rectangle = Core.class(Sprite)
     
    function Rectangle:init(x, y, width, height)
    	self:setX(x)
    	self:setY(y)
     
    	self:beginPath()
    	self:setLineStyle(2)
    	self:setFillStyle(Shape.SOLID, 0xFF0000)
    	self:moveTo(0,0)
    	self:lineTo(x,0)
    	self:lineTo(x,y)
    	self:lineto(0,y)
    	self:lineTo(0,0)
    	self:endPath()
    end
     
    function Rectangle:drawbg(_bwidth, _bcolor, _fcolor)
    	print(type(_bwidth))
    	self:beginPath()
    	self:setLineStyle(_bwidth or 1, _bcolor or 0x000000)
    	self:setFillStyle(self.SOLID, _fcolor or 0x505050)
    	self:moveTo(0,0)
    	self:lineTo(self.width,0)
    	self:lineTo(self.width, self.height)
    	self:lineTo(0,self.height)
    	self:lineTo(0,0)
    	self:endPath()
    end
     
    function Rectangle:rest ()
    	self:beginPath()
    	self:setLineStyle(bwidth or 1, bcolor or 0x000000)
    	self:setFillStyle(self.SOLID, fcolor or 0x505050)
    	self:moveTo(0,0)
    	self:lineTo(self.width,0)
    	self:lineTo(self.width, self.height)
    	self:lineTo(0,self.height)
    	self:lineTo(0,0)
    	self:endPath()
    end
     
    rect = Rectangle.new(50,50,100,100)
    rect:drawbg()
     
    stage:addChild(rect)

    Likes: dddent

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