how to make individual objects in lua?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
findux
Prole
Posts: 6
Joined: Wed Jul 29, 2020 6:46 pm

how to make individual objects in lua?

Post by findux »

I am trying to use object oriented skill in lua. But I little bit confused. As I show you in picture, trying to create two mesh object instance. In debug mode, when I watch the objects, objects members multiplying. When I looked to review the beautifully crafted mgl library, it was a little over the top for me. Could you tell me basic knowledge to create individual object ? Or could you advise me guide papers for dummies?
Thanks..
lua oob confuse.png
lua oob confuse.png (169.21 KiB) Viewed 5649 times
applebappu
Prole
Posts: 37
Joined: Thu Jun 24, 2021 5:49 pm

Re: how to make individual objects in lua?

Post by applebappu »

Speaking as someone who's dabbled in Lua OOP and ultimately mostly given it up except where absolutely necessary, I'd strongly advise against trying to arbitrarily apply any particular programming paradigm to Lua, especially OO. This is because Lua is a very simple language, and the words we use to describe what we're doing in OOP tend to be misleading.

For instance, we might say we want to make a constructor method on an object, but in Lua, that's not really what you're doing. You're just making a table, putting a copying function inside of it, and maybe pointing the __index metamethod for that table to itself. It's a subtle distinction, but an important one to keep in mind. There's only tables, and functions, and that's it.

__index is called when you try to make reference to a table element that isn't there, it describes where to look for that information. You can talk about this as if it's class behavior, but it's really not. And where and how you define the metamethods can be crucial.

I would recommend, as a reliable way to get started, look into writing a copying function instead. There's no built-in "copy table" function in Lua, and coding one yourself would be an excellent way to learn more about the little particulars of the language, which could help prime you to think about metamethods, functions and upvalues, all that sort of thing as well.

In your particular code example, I've never used MGL myself, but I can tell you that your colon syntax in that initial function definition looks a bit odd to me. You don't really want to use that when defining the function; rather, it's "syntactic sugar" for when you CALL that function later. The documentation on this in PIL is a bit misleading, actually. It's so concise that it's maybe unintentionally obscure or too dense.

You could also check out this thread, where I struggled with this same issue and got some good solutions: https://love2d.org/forums/viewtopic.php?f=4&t=91635
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: how to make individual objects in lua?

Post by zorg »

From the code, i can tell that you successfully found a neat example of why you don't make constructors (in lua) be part of the "class" table; if it was just a local funcion returned, then you could not do what you did with the "1st instance" without calling new and *actually* creating an instance...

Anyway, both refer to the same points table as you can see, since they are both at memory location 1c4b3e20; tables only copy by reference, not by creating another hard-copy and copying each inner key-value pair, so you would need to do a few things:

- Get rid of the points and lines table from the Mesh table itself; those should not be "class variables", since you want them to be per-instance, which they currently aren't; define them inside the new function, which is the instance constructor.
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
pgimeno
Party member
Posts: 3567
Joined: Sun Oct 18, 2015 2:58 pm

Re: how to make individual objects in lua?

Post by pgimeno »

To elaborate what zorg said, part of the problem is that by doing mesh = require('sizingMesh'), you're setting 'mesh' to the Mesh class, which is confusing, and the later call to mesh:init() initializes the class, which strikes me as wrong, and the label "1. instance" denotes that confusion, because that's not an instance, it's the class. That should actually be like:

Code: Select all

local Mesh = require('sizingMesh')

local mesh = Mesh:new()
mesh:init(...)
local mesh2 = Mesh:new()
mesh2:init(...)
And yeah, the other part of the problem is that the class contains the tables instead of being created per instance within Mesh:new() (or Mesh:init(), which sounds redundant to me).

I'm fine with having constructors inside classes personally, but after this example, it's clear that having them separate might prevent some class vs instance confusion.
findux
Prole
Posts: 6
Joined: Wed Jul 29, 2020 6:46 pm

Re: how to make individual objects in lua?

Post by findux »

thanks friends . I'm new to Lua, but I like it a little more with every new piece of information.
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests