Hey guys, I'm making a scene-based game and each scene will be a file in the 'scripts' folder.
I've already do it in ruby, but I have no idea on how to do that in Lua.
Anyone know how to?
Thanks for the attention.
[SOLVED]Require all files in a folder
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- wesleylucas
- Prole
- Posts: 12
- Joined: Thu May 09, 2013 11:08 pm
- Location: Brazil
[SOLVED]Require all files in a folder
Last edited by wesleylucas on Fri May 10, 2013 12:12 pm, edited 1 time in total.
def or function doesn't matter, you understand what I mean anyway.
Re: Require all files in a folder
So it would be easier if you at least put an example of your code as a reference, but I think I know what you want.
In your main.lua put require "NAME_OF_FOLDER/NAME_OF_FILE"
Don't actually put NAME_OF_FOLDER and whatever. I think you know what I mean.
In your main.lua put require "NAME_OF_FOLDER/NAME_OF_FILE"
Don't actually put NAME_OF_FOLDER and whatever. I think you know what I mean.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
-
- Prole
- Posts: 6
- Joined: Tue May 07, 2013 10:35 am
Re: Require all files in a folder
You could use love.filesystem.enumerate to get the names of all files in the folder as strings, then use a for loop to iterate over the keys and values in the list that returns (see the example in the docs), and then use each string as an argument to require each file, I think.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Require all files in a folder
I did this some time ago:
https://github.com/kikito/fay/blob/mast ... equire.lua
It extends require with some methods. The usage for requiring the files in a folder is:
As a bonus, you also have require.relative:
Note that require('file') without methods works exactly like before.
https://github.com/kikito/fay/blob/mast ... equire.lua
It extends require with some methods. The usage for requiring the files in a folder is:
Code: Select all
require 'require' -- or require 'lib.require', depending on where you put it
local d = require.tree('path.to.directory')
d.file1 -- contents returned by path/to/directory/file1.lua
d.file2 -- contents returned by path/to/directory/file2.lua
d.subdir.file3 -- contents returned by path/to/directory/subdir/file3.lua
Code: Select all
local f = require.relative(..., "hello") -- if the path to the current file is 'foo.bar.baz', this requires 'foo.bar.hello'
When I write def I mean function.
-
- Prole
- Posts: 6
- Joined: Tue May 07, 2013 10:35 am
Re: Require all files in a folder
That's an elegant addition. I assume other code projects can use it with credit?
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Require all files in a folder
Sure, consider this an explicit permission to use it.
All my source code is MIT-licensed. That little snippet was not big enough to make it a full lib.
All my source code is MIT-licensed. That little snippet was not big enough to make it a full lib.
When I write def I mean function.
- wesleylucas
- Prole
- Posts: 12
- Joined: Thu May 09, 2013 11:08 pm
- Location: Brazil
Re: Require all files in a folder
Thank you! That's exactly what I mean, better actually!kikito wrote:I did this some time ago:
https://github.com/kikito/fay/blob/mast ... equire.lua
It extends require with some methods. The usage for requiring the files in a folder is:
As a bonus, you also have require.relative:Code: Select all
require 'require' -- or require 'lib.require', depending on where you put it local d = require.tree('path.to.directory') d.file1 -- contents returned by path/to/directory/file1.lua d.file2 -- contents returned by path/to/directory/file2.lua d.subdir.file3 -- contents returned by path/to/directory/subdir/file3.lua
Note that require('file') without methods works exactly like before.Code: Select all
local f = require.relative(..., "hello") -- if the path to the current file is 'foo.bar.baz', this requires 'foo.bar.hello'
Credits for you in my game!
def or function doesn't matter, you understand what I mean anyway.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: [SOLVED]Require all files in a folder
I'm glad it helped you, good luck with your game!
When I write def I mean function.
Re: [SOLVED]Require all files in a folder
Hi.
Been searching around for this functionality. I downloaded kikito's require.lua and put it in my game base directory. However, I'm having problems trying to get it to work properly.
I currently have a /characters/ (off my base game directory) that has a bunch of files I want to include. What is the proper syntax to include files from another directory? What is the proper syntax to include just 1 file from another directory?
Thanks so much!
Note: I have read the examples above and I'm still having trouble (I'm new to all this!).
Been searching around for this functionality. I downloaded kikito's require.lua and put it in my game base directory. However, I'm having problems trying to get it to work properly.
I currently have a /characters/ (off my base game directory) that has a bunch of files I want to include. What is the proper syntax to include files from another directory? What is the proper syntax to include just 1 file from another directory?
Thanks so much!
Note: I have read the examples above and I'm still having trouble (I'm new to all this!).
Re: [SOLVED]Require all files in a folder
OK!
Forgot to remove .lua, so that part works.
Now I just need to figure out how to get all files from a directory to be auto required.
Thanks in advance!
Forgot to remove .lua, so that part works.
Now I just need to figure out how to get all files from a directory to be auto required.
Thanks in advance!
Who is online
Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot] and 7 guests