splitting my code into multiple lua files using filesystem.load

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
bkwrm
Prole
Posts: 6
Joined: Mon Mar 07, 2016 10:51 pm

splitting my code into multiple lua files using filesystem.load

Post by bkwrm »

So my main lua file is used as a main menu, graphics settings and so on. What I would like to be able to do is to have each level as it's own main lua file that I than transfer control to. I do use require for various functions, but for my own sanity I would like to split up the main game as much as possible.

Code: Select all

chunk = love.filesystem.load("testLevel.lua") -- load the chunk
local result = chunk() -- execute the chunk
This ...kind of works. It only runs code inside the love.update and love.draw blocks and I haven't yet figured out a way to return back to where I was in the main calling lua file. Some of this can be done with doneOnce type variables or loading anything needed in the main file right before transfering to the new lua file, but scripting errors in the called lua file produce some wacky feedback on the crash screen making debugging a pain in the backside.

Basically is there anything I'm missing that would make this easier? Should probably add that dofile tries to run things in my /love/ install directory by default (and not the game one) so that isn't really an option.
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: splitting my code into multiple lua files using filesystem.load

Post by Beelz »

What you're looking for is require:

Code: Select all

local file = require 'filename' -- no .lua
--Then, if the file returns a table you can use the values and methods from there.
print('Map name: ' .. file.title)
print('Map size: ' .. file.size.w .. ', ' .. file.size.h)
In 'filename.lua':

Code: Select all

 -- create the table we will return
local map = {}

-- add whatever
map.title = 'Map 1'
map.size = {w=5000}, h=5000}

-- return the table
return map
Or you can simplify that:

Code: Select all

local map = {
	title = 'Map1',
	size = {w=5000, h=5000}
}
return map

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
bkwrm
Prole
Posts: 6
Joined: Mon Mar 07, 2016 10:51 pm

Re: splitting my code into multiple lua files using filesystem.load

Post by bkwrm »

Thanks for the reply, I already am using require a lot to organize my functions (collisions, menu options, and so on). I was under the impression that require was mostly used to support the main file though?

Like your example is loading a table with the map settings, which is a perfect use for require. I would like to take it a step farther and separate all my loading, updating, and drawing for that part of the game organized and ran from a separate file. Just makes it easier for me to keep things from influencing or tripping up each other as I get farther into development. This i really just for me to mentally keep things organized easier

I realize I'm overthinking it, I'm just surprised that lua has no functionality to use more than one main.lua easily :/ I'd use love.filesystem.load, but that doesn't run love.load as well as no debugging errors.

Thanks for the help though, I guess I was just hoping that I'd missed something.
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: splitting my code into multiple lua files using filesystem.load

Post by Beelz »

Same thing, just include the functions...

Code: Select all

 -- create the table we will return
local map = {
   title = 'Map1',
   size = {w=5000, h=5000}
}

function map.update(dt)
	-- update
end

function map.draw()
	-- draw
end

return map
Then with multiple levels:

Code: Select all

-- Make a table of map tables
local levels = {
	map1 = require 'mapfile1',
	map2 = require 'mapfile2',
	map3 = require 'mapfile3'
}

-- Keep Track
local curLvl = 'map1'

function love.update(dt)
	-- update only current
	levels[curLvl].update(dt)
end

function love.draw()
	-- draw only current
	levels[curLvl].draw()
end

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
bkwrm
Prole
Posts: 6
Joined: Mon Mar 07, 2016 10:51 pm

Re: splitting my code into multiple lua files using filesystem.load

Post by bkwrm »

I will have to fiddle with that when I get home from work, I thought everything in a module though had to be inside a function, and than you called those functions as you needed them? You never ran the module from top to bottom, you only ran the function as you called them from the main.lua.

Does it run through the module as soon as you require it? I was sticking them in the very beginning of my main.lua to make sure all the functions were available as needed.

Thanks, you gave me a few ideas.
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: splitting my code into multiple lua files using filesystem.load

Post by Beelz »

Anything not inside a function will be run as soon as you require the file... If they are in functions, you must call the functions for anything to happen. So say you had a config file that sets globals for your game:

gameConfig.lua

Code: Select all

ScreenW = 800
ScreenH = 600
bgColor = {50,200,180}
-- Note there is no return at the end, and nothing is local...
-- This will insert these varibles into the global spectrum for your game to use.
Then just require it without declaring a variable(because it doesn't return a table):

Code: Select all

require 'gameConfig'

function love.load()
	-- Using the globals we set
	love.window.setMode(ScreenW, ScreenH)
	love.graphics.setBackgroundColor(bgColor)
end
However, for the sake of organization, I prefer to use tables:

Code: Select all

local cfg = {
	ScreenW = 800,
	ScreenH = 600,
	bgColor = {50,200,180}
}
return cfg
Then call upon the table for the values:

Code: Select all

CONFIG = require 'gameConfig'

function love.load()
	love.window.setMode(CONFIG.ScreenW, CONFIG.ScreenH)
	love.graphics.setBackgroundColor(CONFIG.bgColor)
end

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
bkwrm
Prole
Posts: 6
Joined: Mon Mar 07, 2016 10:51 pm

Re: splitting my code into multiple lua files using filesystem.load

Post by bkwrm »

Thank you! Every example I've seen showed using modules like a function library basically. A lot of what you are showing me I already knew, I just had no idea you could run a module from top to bottom, this changes things a lot :)

EDIT: Just tested this, it does run the update and draw functions on it's own. Does not run the load function but that's not really needed since I can just put my loading stuff before the update and draw functions. Also it error codes correctly. This is exactly what I was looking for, thanks again!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], gabe_stein and 4 guests