Multiple Classes in same file?[Solved]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
cere_ender
Prole
Posts: 9
Joined: Fri Sep 25, 2015 1:51 pm
Location: Barcelona, Spain

Multiple Classes in same file?[Solved]

Post 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
Last edited by cere_ender on Fri Nov 18, 2016 4:25 pm, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Multiple Classes in same file?

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
cere_ender
Prole
Posts: 9
Joined: Fri Sep 25, 2015 1:51 pm
Location: Barcelona, Spain

Re: Multiple Classes in same file?

Post 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)
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Multiple Classes in same file?

Post 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.
User avatar
cere_ender
Prole
Posts: 9
Joined: Fri Sep 25, 2015 1:51 pm
Location: Barcelona, Spain

Re: Multiple Classes in same file?

Post 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?
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Multiple Classes in same file?

Post by raidho36 »

You can always assign table value to a local.
User avatar
cere_ender
Prole
Posts: 9
Joined: Fri Sep 25, 2015 1:51 pm
Location: Barcelona, Spain

Re: Multiple Classes in same file?

Post 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
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot] and 64 guests