Page 1 of 1

Multiple Classes in same file?[Solved]

Posted: Fri Nov 18, 2016 2:34 pm
by cere_ender
Hello, I want to have one file with a class and classes whom inherits from the main class. Is that possible? When I return the main class, I can make the main class only, and if I return the various classes, only the main persists.
May I have to do multiple files to do the subclasses?
Here I have an example:
This make the Button class work

Code: Select all

local class = require 'middleclass'


local Button = class 'Button'


SIMPLE  = 0
TEXTURE = 1


function Button:initialize(dx,dy,dw,dh,background,txt,func)
  self.x            = dx or nil
  self.y            = dy or nil 
  self.height       = dh or nil
  self.width        = dw or nil
  self.background   = background or love.graphics.setColor(255,255,255)
  self.text         = txt or nil
  self.job          = func or function() end
  self.type         = SIMPLE
end

function Button:setXY(dx,dy)
  self.x            = dx
  self.y            = dy
end

function Button:setX(dx)
  self.x            = dx
end

function Button:setY(dx,dy)
  self.y            = dy
end




--[[local SimpleButton = class('SimpleButton', Button)
function SimpleButton:initialize(dx,dy,dh,dw,background,txt,func)
  self.x            = dx or 0
  self.y            = dy or 0
  self.height       = dh or 0
  self.width        = dw or 0
  self.background   = background or love.graphics.setColor(255,255,255,0)
  self.text         = txt or nil
  self.job          = func or function() end
  self.type         = SIMPLE
end

local TextureButton = class('TextureButton', Button)
function TextureButton:initialize(dx,dy,dh,dw,background,func)
  self.x            = dx or 0
  self.y            = dy or 0
  self.height       = dh or 0
  self.width        = dw or 0
  self.background   = background or love.graphics.setColor(255,255,255,0)
  self.job          = func or function() end
  self.type         = TEXTURE
end

]]--

function Button:update(dt)
end
function Button:draw()
    love.graphics.rectangle("fill",self.x,self.y,self.width,self.height)
    love.graphics.print( self.text, self.x+self.width/2, self.y+self.height/2 )
 
end


return Button

But if I do this, I can't make a SimpleButton who inherits Button.

Code: Select all

local class = require 'middleclass'


local Button = class 'Button'


SIMPLE  = 0
TEXTURE = 1


function Button:initialize(dx,dy,dw,dh,background,txt,func)
  self.x            = dx or nil
  self.y            = dy or nil 
  self.height       = dh or nil
  self.width        = dw or nil
  self.background   = background or love.graphics.setColor(255,255,255)
  self.text         = txt or nil
  self.job          = func or function() end
  self.type         = SIMPLE
end

function Button:setXY(dx,dy)
  self.x            = dx
  self.y            = dy
end

function Button:setX(dx)
  self.x            = dx
end

function Button:setY(dx,dy)
  self.y            = dy
end




local SimpleButton = class('SimpleButton', Button)
function SimpleButton:initialize(dx,dy,dh,dw,background,txt,func)
  self.x            = dx or 0
  self.y            = dy or 0
  self.height       = dh or 0
  self.width        = dw or 0
  self.background   = background or love.graphics.setColor(255,255,255,0)
  self.text         = txt or nil
  self.job          = func or function() end
  self.type         = SIMPLE
end

local TextureButton = class('TextureButton', Button)
function TextureButton:initialize(dx,dy,dh,dw,background,func)
  self.x            = dx or 0
  self.y            = dy or 0
  self.height       = dh or 0
  self.width        = dw or 0
  self.background   = background or love.graphics.setColor(255,255,255,0)
  self.job          = func or function() end
  self.type         = TEXTURE
end



function Button:update(dt)
end
function Button:draw()
    love.graphics.rectangle("fill",self.x,self.y,self.width,self.height)
    love.graphics.print( self.text, self.x+self.width/2, self.y+self.height/2 )
 
end


return Button

Main in this last case:

Code: Select all

local Button = require 'button'

function love.load()
 aButton = SimpleButton:new(200,200,100,100,love.graphics.setColor(100,100,100),"hello",nil)   
end
 
 function love.update(dt)
 
 end
 
 function love.draw()
  aButton:draw()
 end

Same if I do this:

Code: Select all

return Button, SimpleButton, TexturedButton
Thanks for the help

Re: Multiple Classes in same file?

Posted: Fri Nov 18, 2016 2:41 pm
by zorg
You need to do

Code: Select all

local Button, SimpleButton, TexturedButton = require 'button'
in your main, after you return all 3 of them like you wrote in your last code block.

Re: Multiple Classes in same file?

Posted: Fri Nov 18, 2016 2:44 pm
by cere_ender
zorg wrote:You need to do

Code: Select all

local Button, SimpleButton, TexturedButton = require 'button'
in your main, after you return all 3 of them like you wrote in your last code block.
I did it, and return when I do

Code: Select all

aButton = SimpleButton:new(200,200,100,100,love.graphics.setColor(100,100,100),"hello",nil)   
Error
main.lua 4: attempt to index upvalue 'SimpleButton' (a nil value)

Re: Multiple Classes in same file?

Posted: Fri Nov 18, 2016 3:40 pm
by bartbes
Unfortunately, due to the way require works, only the first return value of a file is returned (and cached). You could return a table instead.

Re: Multiple Classes in same file?

Posted: Fri Nov 18, 2016 3:46 pm
by cere_ender
bartbes wrote:Unfortunately, due to the way require works, only the first return value of a file is returned (and cached). You could return a table instead.
Something like this?

Code: Select all

local myClasses={}
myClasses.Button = class 'Button'
myClasses.SecondButton = class ('SecondButton',Button')

return myClasses

Code: Select all

local Class = require 'MyClass'

Class.Button:new(blablablabla)
Should organize in different files better?

Re: Multiple Classes in same file?

Posted: Fri Nov 18, 2016 4:03 pm
by raidho36
You can always assign table value to a local.

Re: Multiple Classes in same file?

Posted: Fri Nov 18, 2016 4:25 pm
by cere_ender
Thank you very much to all. Returning a table did the work I want.
If you will know how I did it for future users who had same errors, here it is the Sample Code and a ScreenShoot.

button.lua

Code: Select all

local class = require 'middleclass'

local Button={}

Button.Button = class 'Button'


SIMPLE  = 0
TEXTURE = 1


function Button.Button:initialize(dx,dy,dw,dh)
  self.x            = dx or nil
  self.y            = dy or nil 
  self.height       = dh or nil
  self.width        = dw or nil
  self.background   = background or love.graphics.setColor(255,255,255)
  self.text         = txt or nil
  self.job          = func or function() end
  self.type         = SIMPLE
end

function Button.Button:setXY(dx,dy)
  self.x            = dx
  self.y            = dy
end

function Button.Button:setX(dx)
  self.x            = dx
end

function Button.Button:setY(dx,dy)
  self.y            = dy
end




Button.SimpleButton = class('SimpleButton', Button.Button)
function Button.SimpleButton:initialize(dx,dy,dh,dw,background,txt,func)
  self.x            = dx or 0
  self.y            = dy or 0
  self.height       = dh or 0
  self.width        = dw or 0
  self.background   = background or love.graphics.setColor(255,255,255)
  self.text         = txt or nil
  self.job          = func or function() end
  self.type         = SIMPLE
end

Button.TextureButton = class('TextureButton', Button.Button)
function Button.TextureButton:initialize(dx,dy,dh,dw,background,func)
  self.x            = dx or 0
  self.y            = dy or 0
  self.height       = dh or 0
  self.width        = dw or 0
  self.background   = background or love.graphics.setColor(255,255,255)
  self.job          = func or function() end
  self.type         = TEXTURE
end



function Button.Button:update(dt)
end
function Button.Button:draw()
    love.graphics.rectangle("fill",self.x,self.y,self.width,self.height) 
end


function Button.SimpleButton:update(dt)
end
function Button.SimpleButton:draw()
    love.graphics.rectangle("fill",self.x,self.y,self.width,self.height)
    love.graphics.print( self.text, self.x+self.width/2, self.y+self.height )
 
end


return Button

main.lua

Code: Select all

local Button= require 'button'

function love.load()
 aButton = Button.SimpleButton:new(200,200,100,100,love.graphics.setColor(100,100,100),"hello",nil)   
end
 
 function love.update(dt)
 
 end
 
 function love.draw()
  aButton:draw()
 end
Image