Modules / Events / Models

I’m looking for suggestions of books/articles/etc to improve my coding approach using Lua. One “gotcha” that I find “odd” is how I can do inheritance for many models. At the moment, it seems to me that putting the reusable code into a module and requiring it within each of the scripts for the models is the approach I’m using. For some reason, I keep feeling it isn’t the best approach.

So… let’s say I have a bunch of models: Car, Semi, Van, Pickup… which are all vehicles. So they all Drive() and have Color(), MaxSpeed(), Acceleration(), etc.

The way I’d do it is have a module for those methods: Drive, Color, MaxSpeed, Acceleration
Then in each of the models, I have their own script that would just call those methods.
So…

Pickup.Drive() simply calls module.Drive() and does its own specific things before/after.

Is this the best approach? I’m not a fan of having to create Pickup.Drive() if that method is pretty much only calling module.Drive().

1 Like

uhhhhhhh mate i have no idea what the heck are you talking about

no one here on devforum knows about that module.Drive function (its just you who knows that function at the moment) so we cant tell you much there

maybe just send source code and tell us whats broken or what we should add to fix it :pray:

1 Like

the __index metamethod can be set not just to a table, but a function as well! this allows you to try and index something from multiple sources which effectively gives you the ability to inherit from other modules

Example:

local Class = {}
Class.__index = function(self, index)
    return ClassToInheritFrom[index] or self[index]
end

note: not sure if indexing self inside of the __index metamethod will cause a cyclic error, to circumvent this, use the rawget global function!

1 Like

whaaaaaaaaat

this wont work because you forgot setmetatable()

and also

return ClassToInheritFrom[index] or self[index]

might cause c-stack overflow after implementing setmetatable

something like this would fix the c-stack overflow issue after implementing setmetatable

return ClassToInheritFrom[index] or rawget(self,index)
1 Like

Hmmm… sounds like I should read more on metamethods then. It’s making me think of Objective-C and it’s indirection tables. Do you have a place I can read more about that?

1 Like

Traditionally, this is how you create a class

local Class = {}
Class.__index = Class

function Class.new()
    local self = setmetatable({}, Class)

    return self
end

return Class

so techincally, no, I dont have to call the setmetatable function

edited my post for the potential cyclic error, thanks!

1 Like

I don’t have actual code. I’m attempting to communicate a concept. However, let me try using the earlier example. I can do something if you wish. The thing that bugs me is that dislike having to create a bunch of scripts for each and every model I’m creating in a game when 90% of the code is the same between the models. Even writing a stub that calls a method in a module feels messy to me.

My coding background is much more strongly based in C++, Objective-C, C#, C-blah-blah (haha).

1 Like

You can find it on the Lua docs, here:

https://www.lua.org/pil/13.html
https://www.lua.org/pil/13.4.1.html

2 Likes

That there is some light reading for tonight! Thanks.

2 Likes

C++ developer??? mate luau is not for you its literally for children :skull:

1 Like

I know. My son and I are writing a game together… hence learning this platform and language. It is always fun to learn a different paradigm too. I just need to understand the approach that the language is taking.

4 Likes

that’s so cool! I hope the both of you have fun :smile:

1 Like

It certainly is cool… and from many perspectives. I love seeing how the technology has changed from when I started code WAY WAY back in the day (assembly language) to the fact that path-finding, animation, lighting, etc. are now all built into the platform.

And it is also so cool to get his perspective on how to design and put this together. He has way less language/platform baggage than I do! As a result, it can be quite refreshing. I have to remember that I don’t know it all! :crazy_face:

2 Likes