A little script for loading files

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Bunnybacon
Prole
Posts: 20
Joined: Fri Mar 25, 2016 8:42 am

A little script for loading files

Post by Bunnybacon »

Hey guys!

I am relatively new at game development, so I am still trying to find a good workflow for making new games.
First of all, let me just say that this community is amazing. There is always someone who is willing to help on the IRC.

So I made a function that helps you load all your helper scripts:

Code: Select all

function grab(folder)
	--print("Opening " .. folder)
	files = love.filesystem.getDirectoryItems(folder)
	--print(folder .. " has " .. #files .. " files")
	for k, file in ipairs(files) do
		local path = folder.."/" .. string.sub(file, 0, -5)
		require(path)
		print("Loading ".. path)
	end
end
Love doesn't want the '.lua' extension when using the require function, so I make a substring that trims those off.
All you need to do to use it is:

Code: Select all

function love.load()
	grab("folder1")
	grab("folder2")
	grab("folder3")
end
It is a neat way of loading helper scripts. It only works if the folder only has a bunch of lua files. Let me know if anyone has a cleaner solution.

Have fun.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: A little script for loading files

Post by s-ol »

a little improvement would be to use string:match instead of substr, if it doesn't match "^(.*)%.lua$" then don't require it. Also you shouldnt use slashes in require.
Another useful thing to have would be the return modules.
Here's what I would do:

Code: Select all

function grab(folder)
   local modules = {}
   files = love.filesystem.getDirectoryItems(folder)
   for k, file in ipairs(files) do
      file = file:match("^(.*)%.lua$")
      modules[file] = require(folder .. "." .. file)
   end
   return modules
end
Last edited by s-ol on Fri Mar 25, 2016 1:37 pm, edited 1 time in total.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Bunnybacon
Prole
Posts: 20
Joined: Fri Mar 25, 2016 8:42 am

Re: A little script for loading files

Post by Bunnybacon »

So I guess with your edit, the 'path' variable is unnecessary?
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: A little script for loading files

Post by s-ol »

Bunnybacon wrote:So I guess with your edit, the 'path' variable is unnecessary?
whoops, yes, missed that copy&pasting on my phone.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Sirtycolt3
Prole
Posts: 1
Joined: Fri Feb 17, 2023 11:26 pm

Re: A little script for loading files

Post by Sirtycolt3 »

I know this post is a bit old... But I'm just going to point ut a small thing lua does that caused a huge bug and headache for me.

Code: Select all

local A_Script = require("Scripts.World")
local B_Script = require("Scripts/World")
print(tostring(A_Script))  -- table: 0x500a450
print(tostring(B_Script))  -- table: 0x500d910
print(A_Script == B_Script) -- false!
They create two different objects for the same module (Lua should cache the result of require() so it should result in a reference to the same object no matter how many times you require it)

Just incase someone else has this seemingly illogical bug...
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: A little script for loading files

Post by zorg »

Sirtycolt3 wrote: Fri Feb 17, 2023 11:36 pm I know this post is a bit old... But I'm just going to point ut a small thing lua does that caused a huge bug and headache for me.

Code: Select all

local A_Script = require("Scripts.World")
local B_Script = require("Scripts/World")
print(tostring(A_Script))  -- table: 0x500a450
print(tostring(B_Script))  -- table: 0x500d910
print(A_Script == B_Script) -- false!
They create two different objects for the same module (Lua should cache the result of require() so it should result in a reference to the same object no matter how many times you require it)

Just incase someone else has this seemingly illogical bug...
It does cache it... based on the module name you give it, which in that case is not the same; this is also a reason why slashes shouldn't be used with require, and will throw errors in the next löve release.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: No registered users and 58 guests