463 posts
Location
Star Wars
Posted 01 August 2016 - 05:36 PM
I want to achieve this without arguments, is this possible?
Example:
print(APIname) --> nil
API.name('A text')
print(APIname) --> 'A text'
or
print(API) --> nil
using('API')
print(API) --> table: xxxxx
So I could do this:
tEnv.APIname = sText -- tEnv is the Environment of the caller, sText is the argument
or
tEnv[sKey] = tAPIs[sKey] -- tEnv is the Environment of the caller, sKey is the argument, tAPIs are internal APIs
I need it also for identification: 'Who call me?
–> You get this
–> And you get this2'
Edited on 01 August 2016 - 05:23 PM
3057 posts
Location
United States of America
Posted 01 August 2016 - 05:46 PM
You want the API to know it's own name? Or do you want the environment of the caller? The former seems to fit with your example; the latter does not.
463 posts
Location
Star Wars
Posted 01 August 2016 - 07:18 PM
You want the API to know it's own name? Or do you want the environment of the caller? The former seems to fit with your example; the latter does not.
I updated the post to specify my goal.
3057 posts
Location
United States of America
Posted 01 August 2016 - 07:36 PM
You can kind of do this using getfenv( nStackLevel ). For example,
local function something()
print( getfenv( 1 ).i )
end
something() --# nil
i = 1
something() --# 1
As for "who called it", you can't know that. The information literally isn't there.
463 posts
Location
Star Wars
Posted 01 August 2016 - 07:47 PM
You can kind of do this using getfenv( nStackLevel ). For example,
local function something()
print( getfenv( 1 ).i )
end
something() --# nil
i = 1
something() --# 1
As for "who called it", you can't know that. The information literally isn't there.
I think, I have to change the working wise of my program…
1023 posts
Posted 01 August 2016 - 10:54 PM
As for "who called it", you can't know that. The information literally isn't there.
Luckily a lot of the built in functions that run files set a name for the file when it is loadstringed, and that is fairly easy to obtain. If I understand correctly he should be able to get the who called me information by pcalling error with a certain level to error at. He would then have to find the information he wants out of the error message. I would give a example, but I don't have much time right now.
463 posts
Location
Star Wars
Posted 02 August 2016 - 12:06 PM
As for "who called it", you can't know that. The information literally isn't there.
Luckily a lot of the built in functions that run files set a name for the file when it is loadstringed, and that is fairly easy to obtain. If I understand correctly he should be able to get the who called me information by pcalling error with a certain level to error at. He would then have to find the information he wants out of the error message. I would give a example, but I don't have much time right now.
I think that would be bad code, but the solution with the top-level is the best one.