[SOLVED]Require all files in a folder

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.
User avatar
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

Post by kikito »

Do you need any help, or are you just reporting your findings :nyu: ? If you get stuck, give me the exact folder structure and I will try to give you the code that you need.
When I write def I mean function.
kargon
Prole
Posts: 13
Joined: Tue Sep 09, 2014 2:45 am

Re: [SOLVED]Require all files in a folder

Post by kargon »

Hi kikito!

Well, my gamedir is benGame and i have an /entities off that. Do you need the entire path? c:\games\benGame\entities.

Thanks for the help!
User avatar
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

Post by kikito »

The steps would be:

1. Put `require.lua` inside the benGame directory
2. Write this wherever you need the files (for example, in main.lua)

Code: Select all

require 'require'

local entities = require.tree('entities')
The returned entities table should be a table with 1 entry per file and subfolder inside the `benGame/entities` folder. For example, if you have `benGame/entities/player.lua`, there should be a entities.player entry on that table, containing whatever player.lua returns.
When I write def I mean function.
kargon
Prole
Posts: 13
Joined: Tue Sep 09, 2014 2:45 am

Re: [SOLVED]Require all files in a folder

Post by kargon »

Thanks kikito!
Fang86
Prole
Posts: 15
Joined: Fri Sep 25, 2015 2:03 am

Re: [SOLVED]Require all files in a folder

Post by Fang86 »

kikito wrote:Snip
Hey kikito, I know this is a very old thread, but, I'm having troubles with requiring. My directory is: BeaverBallRevenge/entities/beaver.lua and BeaverBallRevenge/entities/redneck.lua

Code: Select all

local entities = require.tree("entities")
entities.beaver
entities.redneck
It gives me the following error: Compilation error on line 8:
...Aidan\Desktop\Programming\Lua\BeaverBallRevenge\main.lua:8: '=' expected near 'entities'

(entities.redneck is line 8)
User avatar
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

Post by kikito »

The problem is that "entities.beaver" (or "entities.redneck"), by itself, is not a valid Lua instruction.

Code: Select all

local entities = require.tree("entities")
entities.beaver   -- <=== Remove these two lines
entities.redneck  -- <=== They are the ones provoking an error
You can't put the name of an entity in a line in Lua all by itself. That has no meaning in Lua. You must do something with your entities. For example, drawing them. Or creating instances of them. Something.
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Google [Bot] and 7 guests