Alright, I’ve edited the OP to make things more clear.
You could probably do something with getFenv(), loop through all indexes to find what matches the signature of the function. Very hack though and probably not worth it.
function getName(funct)
for i,v in pairs(getfenv()) do
if v == funct then
return i
end
end
end
That works perfectly!
I don’t think this will effect performance all that much either.
Is there anything truly wrong with doing something like this?
I spoke too soon.
It looks like you can’t really pass envs from a script to a module ;c
sad
I’ve been trying to find a workaround to no avail.
The only issue might be the expanding amount of code you add up into the script, causing the for loop to run through a lot of things.
Try using getfenv(0)
, however it’s not that epic if there are more variables and functions in the global scope.
is getfenv(0) different from just getfenv()?
OH EPIC!
this works
Ok so I have done some testing.
I wrote this piece of code to write code for me:
for i=0,1000 do
print([[function a]]..i..[[()
print('hi]]..i..[[')
end]])
end
Then I pasted that code inside the script so that there are 1,000 unique global functions.
Roblox’s server didn’t even flinch (even when I had it print the function names).
This works perfectly.
for all intents and purposes, I hereby pronounce this
still epic
Don’t do this. Use of getfenv/setfenv adversely affects script performance with Luau and is incompatible with the upcoming Typed Lua feature. From a Roblox engineer:
Using getfenv/setfenv will penalize access to builtin globals (math, game, etc.) in any threads that were ran from the same script, and (with a subsequent set of changes that hasn’t happened yet) will disable optimizations on some built-in functions like math.max etc.
Also worth noting is that getfenv/setfenv will not be compatible with the Typed Lua - in typed scripts, using getfenv/setfenv will violate type invariants so code that seems type-safe won’t be. And I guess script analyzer has been warning about this for a while now. The current preview doesn’t have type support yet so that’s coming later this year, but worth keeping in mind - getfenv/setfenv is just not a sound mechanism going forward.
Store the function in a regular dictionary instead, with keys as names and values as the function.
fRiCk!
>;C
i revoke my statement about this still being epic and stuff
Is there any way to get this same functionality?
I’ve built in a thing in my module just incase this became too resource-intensive anyway so it’s not a huge deal but I’d really like to keep it how it is
Store functions in a regular dictionary with keys as function names and values as functions.
This is as previously stated in an earlier reply in this thread. I have always done this method instead of using getfenv(0)
.
I guess I’m just incapable of reading.
I’m staying up really late so lack of sleep might have something to do with that.
This is how I think I’ll format my module for v4:
GiveClientAccess = require(game.ServerStorage.ServerSide)
GiveClientAccess({
test = function(Player,Brick_Color)
Brick_Color = Brick_Color or BrickColor.Black()
print(Player.Name..' wants to spam '..tostring(Brick_Color)..' parts!')
spawn(function()
while wait(.25) do
Instance.new('Part',workspace).BrickColor = Brick_Color
end
end)
return true
end
})
Thanks for your help!
https://developer.roblox.com/en-us/resources/release-note/Release-Notes-for-407
This is confusing so is it fixed or I am missing something important?
Not only that but how much impact does it have on performance overall?
I’m 99% sure that that is in relation to Setting properties using the environment table from getfenv(0) does not work until certain expressions are used (in Luau). Not what Dandystan is talking abuot.
I see thanks for the clarification, but more importantly how much impact does it have on performance because if it’s negligible I don’t think it’s worth being a concern.
Also worth noting is that getfenv/setfenv will not be compatible with the Typed Lua
not sure what that is either but I probably won’t be using it so even if I use getfenv/setfenv I’m not affected.
Typed Lua is a planned feature that will allow you to essentially lock the parameter of a function to a specific type. Errors will be thrown for passing a value of an unaccepted type. It’s equivalent to writing out a list of asserts at the top of a function.
local BAD_ARGUMENT = "bad argument #%d (%s expected, got %s)"
local function expectAString(parameter)
assert(parameter, BAD_ARGUMENT:format(1, "string", "no value"))
assert(typeof(parameter) == "string", BAD_ARGUMENT:format(1, "string", typeof(parameter)))
print(parameter)
end
The solution for anyone who comes across this topic
local tab = {
someFunc = function()
local fName = debug.info(1, "n")
print("Function's name: "..fName)
end,
}
tab.someFunc()
Yea there’s no real good solution beyond the obvious. The closest real solution we have is with using getfenv()
but that disables optimizations and apparently has issues between modulescripts
i know this is a late reply, but you can just do the following:
local function My_Function()
return "Example"
end
local func_name = debug.info(My_Function, "n")
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.