Page 2 of 2
Re: [SOLVED]Require all files in a folder
Posted: Thu Sep 11, 2014 9:03 am
by kikito
Do you need any help, or are you just reporting your findings
? If you get stuck, give me the exact folder structure and I will try to give you the code that you need.
Re: [SOLVED]Require all files in a folder
Posted: Thu Sep 11, 2014 5:15 pm
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!
Re: [SOLVED]Require all files in a folder
Posted: Thu Sep 11, 2014 11:26 pm
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.
Re: [SOLVED]Require all files in a folder
Posted: Fri Sep 12, 2014 1:56 am
by kargon
Thanks kikito!
Re: [SOLVED]Require all files in a folder
Posted: Sat Sep 26, 2015 11:17 pm
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)
Re: [SOLVED]Require all files in a folder
Posted: Sun Sep 27, 2015 5:39 pm
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.