Hit a speedbump as I was learning Love and Lua

General discussion about LÖVE, Lua, game development, puns, and unicorns.
ShadowProtocol
Prole
Posts: 15
Joined: Thu Nov 25, 2010 9:41 pm

Hit a speedbump as I was learning Love and Lua

Post by ShadowProtocol »

So I got the Learning Lua book written by the author, which is absolutely wonderful.
Coming from Python, I was having a really easy time, so the next day I downloaded and started doing Love tutorials.
I was messing around with drawing basic shapes but then I ran into a problem, NO CLASSES!?!??

I stopped working with Lua immediately. Can someone tell me how we program in Lua without OOP? My god

Lua seems so freakin cool but I need someone to teach me how I can work with class prototypes. I looked online but there were no good tutorials.

I'll probably get flamed for this, but I think it would be good on the LOVE community if someone could provide a short tutorial on creating a Shape class, and then Inheritance using a Ball class inheriting from Shape.

<3 LOVE thanks :awesome:
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Hit a speedbump as I was learning Love and Lua

Post by Robin »

Lua has no class system included, but it gives you the tools to write one. Countless have been written. Even when you only look at the ones written by lovers, we have SECS, MiddleClass, and many more. Pick one, put it in your project, include the library, and you're ready to go. :)

(Between the two, SECS is simpler, while MiddleClass is more advanced.)

Hope that helps!
Help us help you: attach a .love.
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Hit a speedbump as I was learning Love and Lua

Post by nevon »

And, of course, I want to give a mention to my favorite class system (among other things), HUMP.
ShadowProtocol
Prole
Posts: 15
Joined: Thu Nov 25, 2010 9:41 pm

Re: Hit a speedbump as I was learning Love and Lua

Post by ShadowProtocol »

Thanks guys. So I need a giant library to use classes?
Does that make Lua/love development really difficult? I hope they aren't so difficult to learn to use because I'd like to continue at the pace ive been learning at.

Also, any projects from the forums you recommend I download and learn source code from? I'd like to make an engine/framework that handles collisions, entity management, etc but I can't without seeing how others were making their games (and making classes)
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Hit a speedbump as I was learning Love and Lua

Post by thelinx »

Not to self-advertise, but Döts is a fairly simple LÖVE game that uses classes extensively.

It used MiddleClass for class capabilities.
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Hit a speedbump as I was learning Love and Lua

Post by nevon »

ShadowProtocol wrote:Thanks guys. So I need a giant library to use classes?
No. But you need to implement classes yourself. If you want to use a huge library to do that, you can. Or you can go with a more barebones approach (the basic version of SECS is like 15 lines).
ShadowProtocol wrote:Does that make Lua/love development really difficult? I hope they aren't so difficult to learn to use because I'd like to continue at the pace ive been learning at.
No, it's quite simple. While the lack of a built-in class system is one of the things I dislike about Lua, it's not really a big deal since you can implement it yourself rather easily.
ShadowProtocol
Prole
Posts: 15
Joined: Thu Nov 25, 2010 9:41 pm

Re: Hit a speedbump as I was learning Love and Lua

Post by ShadowProtocol »

it's not really a big deal since you can implement it yourself rather easily.
How...? thats my point lol. I've looked up tutorials on classes in Lua using Google but it was based on large, complicated examples, instead of a simple Shapes class and Square/Circle class..
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Hit a speedbump as I was learning Love and Lua

Post by nevon »

ShadowProtocol wrote:
it's not really a big deal since you can implement it yourself rather easily.
How...? thats my point lol. I've looked up tutorials on classes in Lua using Google but it was based on large, complicated examples, instead of a simple Shapes class and Square/Circle class..
A class system has nothing to do with creating a square class or a circle class. Creating a class system means creating a way to implement classes in general.

If you check the links we've supplied you with, there are plenty of examples of how to create simple classes. Using SECS, you'd do this:

classes.lua:

Code: Select all

--[[
Copyright (c) 2009 Bart Bes

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
]]

__HAS_SECS_COMPATIBLE_CLASSES__ = true

local class_mt = {}

function class_mt:__index(key)
    return self.__baseclass[key]
end

class = setmetatable({ __baseclass = {} }, class_mt)

function class:new(...)
    local c = {}
    c.__baseclass = self
    setmetatable(c, getmetatable(self))
    if c.init then
        c:init(...)
    end
    return c
end
main.lua:

Code: Select all

require "class" --this assumes you've saved the code above in class.lua

myclass = class:new()
myclass.value = 13
function myclass:setvalue(v)
    self.value = v
end
object = myclass:new()
object:setvalue(128)
--myclass.value is still 13 and object.value is now 128
anotherObject = object:new()
--anotherObject.value is 128
ShadowProtocol
Prole
Posts: 15
Joined: Thu Nov 25, 2010 9:41 pm

Re: Hit a speedbump as I was learning Love and Lua

Post by ShadowProtocol »

You're right, I sounded a bit desperate and I didn't take the time to look at the link first.

So if I save and add that classes.lua to all of my projects, I can implement classes easily just by doing

##defines the class
myclass = class:new()

##sets default value of one of class' member fields
myclass.value = 13

##function definition
function myclass:setvalue(v)
self.value = v
end

##create instance of class
object = myclass:new()

##call function defined above on instance of class
object:setvalue(128)
--myclass.value is still 13 and object.value is now 128


##Can you explain why a newly created object has the value of another instance, and not the default of the class?
anotherObject = object:new()
--anotherObject.value is 128
[/code]

If you can be so kind as to tell me what's wrong with each of my comments, that would greatly speed up the learning process :nyu:
User avatar
trookat
Prole
Posts: 18
Joined: Sun Nov 14, 2010 12:32 pm
Location: Western Australia
Contact:

Re: Hit a speedbump as I was learning Love and Lua

Post by trookat »

ShadowProtocol wrote: ##function definition
function myclass:setvalue(v)
self.value = v
end
Disclaimer: I could be wrong here; going from using lua I use in wow plugins ( i'm a newbie getting the grips of lua classes and love api ).

The use of self was deprieciated in one of the more current lua versions, i had to switch from using self to object.value when WOW updated its lua base.
I assume that love is the same .From what I understand typically 'self' gets passed to the function or is declared in your table as a function that refrences the values via __index. KISS; use object.value for all your varables even inside of functions. or choose a class system that includes a self setup

--edit second look
i didn't really look at the class that was being used. IDK why this is not working :roll: somone point it out please I would like to know before i start breaking code to include class definintions :ehem:

-edit third look
my guess and someone call me out if i'm wrong

Code: Select all

myclass = class:new()
myclass.value = 13
function myclass:setvalue(v)
    self.value = v
end
is the class defintion. myclass.value is the setting the value of the class

Code: Select all

object = myclass:new() 
object:setvalue(128)
--myclass.value is still 13 and object.value is now 128
is copying the class to a new value called object, so now you have 2 classes myvalue and object with two diffrent values

Code: Select all

anotherObject = object:new()
anotherobject is a copy of object which already has 128 as a value.

Did i get that right?
Last edited by trookat on Fri Nov 26, 2010 12:42 am, edited 1 time in total.
Post Reply

Who is online

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