Modules in Löve

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
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Modules in Löve

Post by kikito »

Hello fellow lövers!

I'd like to develop something on LÖVE, but I'd be much more comfortable if I was able to split my lua code on several files.

A folder would actually be nice, too.

"Vanilla" LUA offers "require" and "module" functions for organizing your code - what is the nearest equivalent in LÖVE?

Let's imagine that I want to create a package with the following structure:

Code: Select all

  main.lua
  package/
    module1.lua
    module2.lua
    init.lua  
What should I include on each of the 3 lua files above?

Thanks a lot

PS: Avatar!
When I write def I mean function.
User avatar
F687/s
Prole
Posts: 15
Joined: Wed May 20, 2009 8:29 pm
Location: Blowin' Green, O'Hi-o
Contact:

Re: Modules in Löve

Post by F687/s »

"Vanilla" LUA offers "require" and "module" functions for organizing your code - what is the nearest equivalent in LÖVE?
Well, um... require() , to tell the truth. In version 0.5.0, you can use love.filesystem.require() to load external code, but 0.6 just simplifies it down to require. Just place all the includes in your load() function (or love.load() for 0.6), and experience the magic of code organization!

Hope that answers your question.
User avatar
osgeld
Party member
Posts: 303
Joined: Sun Nov 23, 2008 10:13 pm

Re: Modules in Löve

Post by osgeld »

ok i have a question

by default core lua likes require differently than one would expect
ie

require("bob.lua") looks in folder bob for *.lua
and
require("bob") loads bob.lua

I havent gotten around to luv 6 and was wondering how this new require function works, is is like core lua or like love.filesystem.require()

spanks
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Modules in Löve

Post by bartbes »

Like love.filesystem.require
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Modules in Löve

Post by kikito »

Hey guys,

Thanks for your input. However, you didn't tell me anything I didn't already know.

What I need are the details. For example:
  • What should Iwrite on main.lua? require 'package'? 'package.module1'? or love.filesystem.require something? Should I use
  • Can I use "module" on my module files? If so, do I get it to work? Is there a special syntax in LÖVE?
I've tried the "vanilla LUA way"(section 5.3 on http://www.lua.org/manual/5.1/manual.html) doesn't seem to work on LÖVE.

What am I supposed to write on main.lua, module1.lua and module2.lua?

Thanks and regards!
When I write def I mean function.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Modules in Löve

Post by bartbes »

Yes, you can use module, and require like this:

Code: Select all

require "module1.lua"
User avatar
Tenoch
Citizen
Posts: 76
Joined: Mon Jul 21, 2008 7:49 am

Re: Modules in Löve

Post by Tenoch »

I still don't get why we don't comply totally to the Lua standard and make require() take only the module name, without the .lua
Especially if you use the module() function in it, which iirc uses the name passed to require() to name to module.

(Oh and Lua is capitalized Lua. Not LUA. :halloween: )
"When in doubt, use brute force." Ken Thompson
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Modules in Löve

Post by bartbes »

Ask rude, I have asked him the same question before.
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Modules in Löve

Post by rude »

Tenoch wrote:I still don't get why we don't comply totally to the Lua standard and make require() take only the module name [...]
In 0.6.0, you can choose between require("foo.lua"), and require("foo"). You can also do require("foo.bar"), which will expand to "foo/bar.lua".
User avatar
Tenoch
Citizen
Posts: 76
Joined: Mon Jul 21, 2008 7:49 am

Re: Modules in Löve

Post by Tenoch »

Awesome!

/me happy :megagrin:
"When in doubt, use brute force." Ken Thompson
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest