|
| 09 Feb 2013 06:51 PM |
Dear SH,
I have recently been working on a Game Engine for my place, but I'm having a few issues: I want to create an Import() function, but I getfenv() keeps executing for the script where Import() is located. Example: -- The API local API = { --api stuff }
function _G.ImportAbsoluteEngine() for a, b in pairs(API) do getfenv()[a] = b end end
-- Testing Script _G.ImportAbsoluteEngine() someApiFunction() > error saying someApiFunction() doesn't exist
The issue is that _G.ImportAbsoluteEngine is working in the API script, but not the testing one. However, if I just paste the raw getfenv() code at the top of the testing script, it works. It's ugly and I hate it. Is there ANY way I can make a *working* import function that works for external script? Thanks in advance!
[ AbsoluteLOL - TFN Prime General ] |
|
|
| Report Abuse |
|
|
|
| 09 Feb 2013 07:41 PM |
try this:
-- The API local importService=_G local API = { --api stuff }
importService.ImportAbsoluteEngine=function() for a, b in pairs(API) do getfenv()[a] = b end end
-- Testing Script local importService=_G importService.ImportAbsoluteEngine()
I think that would fix your problem! |
|
|
| Report Abuse |
|
|
|
| 09 Feb 2013 07:56 PM |
| You need to use setfenv() to set the environment. |
|
|
| Report Abuse |
|
|
|
| 09 Feb 2013 08:35 PM |
| put the api in _G and import the variables via _G.API instead of API |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2013 08:16 AM |
I see all of you did not understand my problem. You can't set the function environment on an external script. Example: -- API api = { function a() print("hi") end } _G.gapi = api
_G.import() for a, b in pairs(api) do getfenv()[a] = b end end
-- Tester 1 (raw code) for a, b in pairs(_G.gapi) do getfenv()[a] = b end a() > hi
-- Tester 2 (global import function) _G.import() a() > error saying function a() is nil
Do you see my problem now?
[ AbsoluteLOL - TFN Prime General ] |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2013 08:17 AM |
Wait, I may have found a solution. Hold on...
[ AbsoluteLOL - TFN Prime General ] |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2013 08:22 AM |
Nope. Any help?
[ AbsoluteLOL - TFN Prime General ] |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2013 08:26 AM |
I tried this but to no avail:
function _G.ImportEngine() for a, b in pairs(engine) do setfenv(1, {b = b}) end end
Please help. I suck with function environments ._.
[ AbsoluteLOL - TFN Prime General ] |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2013 08:56 AM |
| If I'm reading this correctly, you're setting the environment of the function, not the script. Try just putting the function's code at the start of all your scripts. |
|
|
| Report Abuse |
|
|
8SunTzu8
|
  |
| Joined: 30 Sep 2011 |
| Total Posts: 8199 |
|
|
| 10 Feb 2013 09:25 AM |
It seems like his reason for doing this is so he doesn't have to do that.
"If you want to become a Developer or Innovator for CSA, contact me." |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2013 09:42 AM |
function _G.ImportEngine() local temp=getfenv() for a, b in pairs(engine) do temp[a]=b end setfenv(1,temp) end
|
|
|
| Report Abuse |
|
|
|
| 10 Feb 2013 12:18 PM |
Oysi told me the solution. It was really simple: function _G.importEngine() for a, b in pairs(_engine) do getfenv(2)[a] = b -- Must use the 2nd environment end end
[ AbsoluteLOL - TFN Prime General ] |
|
|
| Report Abuse |
|
|