Classes in Lua

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
ARCAD0S
Prole
Posts: 2
Joined: Wed Nov 28, 2012 8:33 pm

Classes in Lua

Post by ARCAD0S »

(Banter Starts Here)
Ok, so I have very minimal programming experience and have literally just moved onto LOVE from Game Maker. I've been using the Wiki in addition to some of the simplest examples I can find on the Project/Demo board to get the most out of this however there is one thing that I can't figure out: Classes. Or, at least from what I've read, Lua's equivalent of them.

After I had looked over the Wiki pages I went straight into experimenting with code and produced a fake cube which I would've intended to use for something with graphics reminiscient of Disgaea (isometric in 3d with sprites for characters and enemies) only to realise I had problems with depth and no simple way of setting up several of these tiles simply (see the fakeCube.love attachment).
(Banter Ends Here)

(Main Point)
I went on to try something more within my skill range; a scrolling shooter, where it occurred to me that I had read nothing in the Wiki that followed the same scheme as in Game Maker where you create an object (which is just a class) and then create and can spawn instances of that class which is the generally used way for going about having the ability to shoot. I looked into the use of tables which were suggested to other people with the problem and looked at other people's code but can't wrap my head around how to use them.

My question is this: How can I create and use something that follows the same sort of rules as a Game Maker object (or a 'Class' in main languages) to implement shooting or place several of those cuboids in the attachment without having many times the code?
(Main Point Ends Here)

Also, some information on the fakeCube.love files variables and layout for those interested (in code to save space):

Code: Select all

The white square is what I would call the 'top' of the cuboid. cubeX and cubeY are the coordinates for the middle of the 'top' square.

cubeSize is the scale factor, where the maximum size were the scale factor 1 is 1 pixel along each axis (so the 'top' would be a 2x2 square at minimum) so 50 makes the 'top' square's maximum size 100x100.

cubeRotSpeed is the speed of rotation for when you use the arrow keys to rotate the cuboid. I don't really know much about radians, just few of their uses and how to convert them so this value is just down to what I thought looked right when I tried the game.

cubeD is the maximum cuboid depth in pixels. Basically, the middle section's maximum length no matter what angle you view the cuboid at.

cubeDepth is how deep the cuboid should be rendered at the current angle.

drawShade was just a quick way of setting the 3 shades of grey for the different parts of the cuboid.

extraRot is the z-rotation I guess. It's basically the other direction of rotation but I've never thought about it in terms of being rotation on a z-axis so I don't really know if it is (just assume that it is until you see it used for something that makes less sense than the rest of the code)

pointAX and pointAY to pointDX and pointDY are the coordinates for the 4 vertices of the 'top' square.
pointAAX and pointAAY to pointDDX and pointDDY are the coordinates for the 4 vertices of the 'bottom' square.
Those points are what I would want to make specific to each instance of a class.

All the equations were pretty much just what thought might look good enough and if they did they were kept, so there's bound to be countless inaccuracies from what a real 3D render would look like.
Also feel free to point out any other problems with my code or make any suggestions you find relevant ^^
Attachments
fakeCube.love
Used the arrow keys to rotate the cuboid.
(808 Bytes) Downloaded 168 times
I'm new here, heck, I'm new to programming, but that's what I'm here to change ^^
User avatar
paritybit
Citizen
Posts: 53
Joined: Thu Sep 06, 2012 3:52 am

Re: Classes in Lua

Post by paritybit »

The concept of "classes" and "instances" isn't baked into Lua like it is many other languages. You can either embrace a new model, use one of the many fine examples of an object-oriented class system, or come up with your own. My favorite is Kikito's middleclass, but look around and you may find one you like better.

Whatever class system you use, it's going to come down to Lua tables which can contain functions -- and those functions can be addressed using <table name>.<function name>; often one of those functions is an initializer or constructor which will give you the structure you desire in a language that is a little less structured by default.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Classes in Lua

Post by Inny »

For new users to Lua, it's always highly recommended that you read this free ebook: Programming In Lua, written by one of the language's authors. It's very good at explaining most of Lua's concepts. As for the idea of Classes, what's been said by paritybit is correct, Lua doesn't have classes. It has "Metatables" which serve in the same capacity as classes, in terms of mechanism. You create a class by loading up a table with useful functions, set its __index key to itself, and then set it as the metatable for your objects. Lua lacks a perfect inheritance scheme, so there it's really recommended to use existing implementations. Kikito's Middleclass is very advanced and usable. There's also several others that are really good on Love's libraries page. 30log and SECS are very nice also. At the very least, you should read their code to learn how they work.
User avatar
ARCAD0S
Prole
Posts: 2
Joined: Wed Nov 28, 2012 8:33 pm

Re: Classes in Lua

Post by ARCAD0S »

Thank you for the information and links to other sources, I've started eating my way through the "Programming in Lua" ebook and once I finish I'll take a look at the additional libraries so I can understand the code they use better.
I'm new here, heck, I'm new to programming, but that's what I'm here to change ^^
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 30 guests