generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: External API Importing

Previous Thread :: Next Thread 
AbsoluteLOL is not online. AbsoluteLOL
Joined: 01 Dec 2012
Total Posts: 3939
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
Sonofmetal is not online. Sonofmetal
Joined: 07 Feb 2012
Total Posts: 1159
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
BJJScrambler is not online. BJJScrambler
Joined: 13 Jan 2013
Total Posts: 898
09 Feb 2013 07:56 PM
You need to use setfenv() to set the environment.
Report Abuse
StygianBlade is not online. StygianBlade
Joined: 15 Sep 2011
Total Posts: 426
09 Feb 2013 08:35 PM
put the api in _G and import the variables via _G.API instead of API
Report Abuse
AbsoluteLOL is not online. AbsoluteLOL
Joined: 01 Dec 2012
Total Posts: 3939
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
AbsoluteLOL is not online. AbsoluteLOL
Joined: 01 Dec 2012
Total Posts: 3939
10 Feb 2013 08:17 AM
Wait, I may have found a solution. Hold on...

[ AbsoluteLOL - TFN Prime General ]
Report Abuse
AbsoluteLOL is not online. AbsoluteLOL
Joined: 01 Dec 2012
Total Posts: 3939
10 Feb 2013 08:22 AM
Nope. Any help?

[ AbsoluteLOL - TFN Prime General ]
Report Abuse
AbsoluteLOL is not online. AbsoluteLOL
Joined: 01 Dec 2012
Total Posts: 3939
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
ArceusInator is not online. ArceusInator
Joined: 10 Oct 2009
Total Posts: 30553
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 is not online. 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
BJJScrambler is not online. BJJScrambler
Joined: 13 Jan 2013
Total Posts: 898
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
AbsoluteLOL is not online. AbsoluteLOL
Joined: 01 Dec 2012
Total Posts: 3939
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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image