Page 1 of 1

A little script for loading files

Posted: Fri Mar 25, 2016 11:39 am
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.

Re: A little script for loading files

Posted: Fri Mar 25, 2016 12:09 pm
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

Re: A little script for loading files

Posted: Fri Mar 25, 2016 12:50 pm
by Bunnybacon
So I guess with your edit, the 'path' variable is unnecessary?

Re: A little script for loading files

Posted: Fri Mar 25, 2016 1:37 pm
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.

Re: A little script for loading files

Posted: Fri Feb 17, 2023 11:36 pm
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...

Re: A little script for loading files

Posted: Sat Feb 18, 2023 7:52 am
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.