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().
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!
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?
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).
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.
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!