Deplexity
|
  |
| Joined: 18 Nov 2011 |
| Total Posts: 1965 |
|
|
| 10 Dec 2014 10:05 PM |
So right now I have this code:
local newTest = {}
function getGameSystems(Instance) for index,Module in pairs(Instance:GetChildren()) do if Module and Module:IsA("ModuleScript") then getfenv()[Module.Name] = require(Module) end end end
getGameSystems(Workspace.gameSystems)
print(newTest.testfunction())
For some reason, this errors, saying: 20:04:20.488 - Workspace.testScript:16: attempt to call field 'testfunction' (a nil value)
But in the print function at the bottom, if I replace it with:
print(getfenv().newTest.testfunction())
It works fine. Is there anyway to make it so I don't have to enter getfenv() at the beginning of every variable? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 10 Dec 2014 10:46 PM |
newText.getGameSystems = function(Instance) ... end; |
|
|
| Report Abuse |
|
|
Deplexity
|
  |
| Joined: 18 Nov 2011 |
| Total Posts: 1965 |
|
|
| 10 Dec 2014 11:00 PM |
That sort of works.
Essentially what the function does is get all the ModuleScripts in a folder and sets the tables in the ModuleScript to their respective table in the code.
Just an easier way instead of having to type the require thingy for every variable, because there's going to be a lot.
One way I thought of doing this is just checking the ModuleScript's name and setting the table with the same name to the ModuleScript, but I don't know how to actually get variables names.
How would I do this? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 10 Dec 2014 11:10 PM |
Just do like:
local global = getfenv(0);
local loadModuleInEnv = function(mod) for key, value in next, require(mod) do global[key] = value; end end;
So you would just do: loadModuleInEnv(workspace.Blah); loadModuleInEnv(workspace.Blah2);
And all the things it returns (table or function or whatever) is added to the environment. But if you want to make a table with the name of the module and with everything in it, just do:
global[mod.Name] = require(mod); |
|
|
| Report Abuse |
|
|
Deplexity
|
  |
| Joined: 18 Nov 2011 |
| Total Posts: 1965 |
|
|
| 10 Dec 2014 11:27 PM |
Thanks so much :)
Here was my finished code:
local math = {}; local table = {}; local string = {};
local loadModuleInEnv = function() repeat wait() until Workspace:findFirstChild("gameSystems") for index, module in next, Workspace.gameSystems:GetChildren() do for key, value in next, require(module) do string[key] = value; end end end;
loadModuleInEnv();
string.testfunction()
> "It works!" |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 10 Dec 2014 11:27 PM |
Why do you have local math = {}, blah at the top? Replacing the math, table, and string libraries :D |
|
|
| Report Abuse |
|
|
Deplexity
|
  |
| Joined: 18 Nov 2011 |
| Total Posts: 1965 |
|
| |
|