A few questions from a Lua/Love2D newbie.

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
BOT-Brad
Citizen
Posts: 87
Joined: Tue Dec 02, 2014 2:17 pm
Location: England

A few questions from a Lua/Love2D newbie.

Post by BOT-Brad »

Hi there guys!

Firstly, although I would not consider myself a 'professional' coder (more of an occasional 'hobby programmer'), I have worked on and off for nearly 4 years developing applications and programs in both Java and ActionScript 2/3 (Flash). I first discovered Love2D last year, but never really looked into it further. I saw it pop up again while doing some reading online and have decided to have a crack at learning both Love2D and Lua at the same time.

In a few days, I feel I have already gotten to grips with Lua a fair bit, however there are a few general questions regarding Lua and Love2D I would like to ask and hopefully you guys can steer me in the right direction :).

Q1: What is the best approach to OOP with Lua/Love2D?
There are no classes in Lua (as far as I can tell), so everything seems to pretty much be a table (which definitely has it's perks for many things!). How should I handle many instances of the same object? Just generate an table with the same functions each time? If so, how do I inherit other tables methods?

Q2: Saving/Loading data with Love2D?
I'm currently playing around with a small application which attempts to predict long-term market trends based off lots of data (*yawn*).
Parsing in this data from a file was fairly simple, but once the love2d app closes, I have to re-parse the data again when I run it.
How can I save this data and reload it in a Lua form (serialised) without repeating the parsing procedure?
Can I just save the data in raw Lua files and require() them in if they exist next time?

Q3: General love.graphics questions
I feel that calling love.graphics.setColor() and love.graphics.setFont() hundreds of time each love.draw() is a bit crazy. Is it ok to do that? Although I get constant 60FPS doing both functions say 200 times each draw() call, is it acceptable? If not, how can I minimise these calls if I do need to change colours a lot? Is drawing to canvases faster?

Finally, any general tips for a Java/ActionScript programmer leaping head-first into Love2D/Lua?

:megagrin:
Follow me on GitHub! | Send me a friend request on PSN!
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: A few questions from a Lua/Love2D newbie.

Post by Azhukar »

BOT-Brad wrote:How should I handle many instances of the same object? Just generate an table with the same functions each time? If so, how do I inherit other tables methods?
Metatables. There's many class modules floating around, you can use them or make your own.
BOT-Brad wrote:Can I just save the data in raw Lua files and require() them in if they exist next time?
Yes. Look into serializing lua tables.
BOT-Brad wrote:I feel that calling love.graphics.setColor() and love.graphics.setFont() hundreds of time each love.draw() is a bit crazy. Is it ok to do that?
Yes. You can use a spritebatch for drawing lots of images, setFont is not resource intensive.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: A few questions from a Lua/Love2D newbie.

Post by Robin »

BOT-Brad wrote:Can I just save the data in raw Lua files and require() them in if they exist next time?
Only want to store plain Lua values, for things like configuration? Ser is your man.
Do you want to store instances of classes, and objects created by LÖVE for things like savefiles? Lady is your woman.

(Full disclosure: I wrote both of them.)
Help us help you: attach a .love.
User avatar
BOT-Brad
Citizen
Posts: 87
Joined: Tue Dec 02, 2014 2:17 pm
Location: England

Re: A few questions from a Lua/Love2D newbie.

Post by BOT-Brad »

Thank you for both your responses.

Azhukar
Thanks for your response. I quite like the 'middleclass' and also found 'hump' which has a few nifty features including a class implementation.
I can't quite wrap my head around SpriteBatches though. They seem somewhat akin to drawing tiles from a tilesheet for example. Am I on the right lines?

Robin
Ser indeed looks like the table serialization lib I was after, thanks!
Follow me on GitHub! | Send me a friend request on PSN!
natev
Prole
Posts: 14
Joined: Thu Nov 20, 2014 3:55 pm

Re: A few questions from a Lua/Love2D newbie.

Post by natev »

You can do ultra-simple OO with inheritance like this:

Code: Select all

class = {}
class.add = function(self) return self.a+self.b end
class.getClassname = function(self) return self.classname end
class.new = function(self, params)
  local newInst = {}; newInst.a = params.a or 0; newInst.b = params.b or 0; newInst.classname = params.classname or "object"
  newInst.add = params.add or class.add; newInst.getClassname = params.getClassname or class.getClassname
  return newInst
end

subClass = {}
subClass.add = function(self) return self.a*self.b end
subClass.new = function(self, params)
  local newInst = class:new(params)
  newInst.classname = params.classname or "subObject"
  newInst.add = params.add or subClass.add
  return newInst
end
In this case, subClass inherits any variables or methods of class. Hopefully it's obvious how you can likewise inherit from subClass...

It doesn't handle metatables, but if you're asking this question, you are probably not using metatables :) It also doesn't do multiple inheritance.
Post Reply

Who is online

Users browsing this forum: No registered users and 239 guests