Noob question - oop/modules

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Posts: 5
Joined: Sun Aug 16, 2015 12:17 pm

Noob question - oop/modules

Post by Chinol » Sun Sep 17, 2017 1:50 pm

Yesterday i tested few approaches about modules/oop or something like that. I know the best way to achieve oop is write some classes, metatables and bla bla bla. Leave this for a while. I scribed few lines of code, ran it and met a problem...

Someone can tell me what is going on?
BTW. Could u send me some "user friendly" tutorials about oop in Love? :D Thanks in advance.

User avatar
Party member
Posts: 1076
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany

Re: Noob question - oop/modules

Post by s-ol » Sun Sep 17, 2017 2:42 pm

So... you want to do OOP but not classes? Those are basically the same concept. What you are doing is a broken attempt at writing a class, even if you're not using metatables.

Your main problem: you defined building.Build using the ':' operator, which means the function has a hidden first parameter called 'self' that you aren't using (but should be).
When you call it you leave out the colon and so all your arguments shift one spot to the left: self becomes your name, the name becomes the health value and the health value is nil.
You just need to change your main file call to

Code: Select all

Build:Build("Somehjng", 10)
However fixing this you will soon run into another error. You are not using the 'self' value and creating new tables, instead you are setting and reading values from the 'building' table itself. This means that if you call 'building:Build()' multiples times, you will return the same table, and all your buildings will really only be one. Whenever you change one of them, all the others will be the same health and name as well.

Here is a PiL chapter about all this: /blog  - /st8.lua  - /gtglg /curcur

Code: Select all

print( type(love) )
if false then

User avatar
Party member
Posts: 118
Joined: Wed Mar 15, 2017 8:52 am
Location: Germany

Re: Noob question - oop/modules

Post by erasio » Sun Sep 17, 2017 3:23 pm

If you're just starting out. I would highly suggest using one of the libraries available.

My favorites are hump.class and middleclass.

For general structure my favorite tool is lovetoys. In my opinion it enforces an amazing and modular structure even though it's a bit weird to start out with.

Posts: 25
Joined: Wed Aug 16, 2017 2:55 pm

Re: Noob question - oop/modules

Post by hamberge » Fri Sep 29, 2017 2:44 pm

Lua is also a very flexible language that allows you to implement classes in lot of different ways. Perhaps one of the easiest is to just have your Building class have a newBuilding function that creates a new table, specifies its properties and its functions by pointing the table's properties to already defined functions (which can be defined within Building) and returns that table. Then you don't have to worry about metatables or even self or the colon operator. You do lose a lot of functionality of true classes this way (inheritance, type checking) but it nice to start out with.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests