How to put middleclass into a module?

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
togFox
Party member
Posts: 801
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

How to put middleclass into a module?

Post by togFox »

I have this:

main.lua

Code: Select all

cls = require "myclasses"

function love.load()

	myperson = nil
	myperson = cls.Person:new()
	Player[1] = myperson	
end
myclasses.lua

Code: Select all

class = require ("Middleclass.middleclass")
-- https://github.com/kikito/middleclass

Person = class('Person')
function Person:initialize()
	self.name = ""
end
Error on main.lua:
line = myperson = cls.Person:new()
error = attempt to index global 'cls' (a boolean value)

It is treating cls as a table. I think. I want it to instantiate the PERSON class in myclasses.lua file. It works if I dump it all into main.lua but I want to keep my classes together in a separate file for editability reasons.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: How to put middleclass into a module?

Post by ReFreezed »

You have to return the value that represents the module from the module file (i.e. return the 'Person' table from myclasses.lua). If you don't do that then the value will be 'true' (i.e. cls will be 'true', and thus you get that error).

There's another issue in the code. One reason to use modules is so that you don't have to create/use globals, but instead only use local variables. The code you posted currently defines 'cls', 'myperson', 'class' and 'Person' as globals. I suggest reading the manual about variables and local declarations.

(Also, none of this has anything to do specifically with the middleclass library.)
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
togFox
Party member
Posts: 801
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: How to put middleclass into a module?

Post by togFox »

Where does the return go? Can I return self?

And yes about the globals. That will be fixed. Thx.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: How to put middleclass into a module?

Post by grump »

You know, this is really general and basic stuff that you could learn in much less time by reading documentation or beginner's tutorials.
User avatar
idbrii
Prole
Posts: 34
Joined: Sat Jun 12, 2021 5:07 am

Re: How to put middleclass into a module?

Post by idbrii »

Check out the lua users tutorial on modules: http://lua-users.org/wiki/ModulesTutorial
User avatar
togFox
Party member
Posts: 801
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: How to put middleclass into a module?

Post by togFox »

Right! I see now. I've always used the old method:

Code: Select all


module("mymodule", package.seeall)
but now I can see how to switch. Thanks idbrii!
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests