#TLDR I have a string and I want to create a variable with the same name as said string.
I am trying to port some gLua code to Luau, its pretty easy, just need to add some globals, add some classes, add some libraries and replace some operators.
The way I'm deciding to create the custom globals and classes is by creating a module that returns a bunch of meta-tables when required. This is all well and good but there is quite a large number of things I'm adding and having to declare each one in each script takes up a lot of space.
#Example
ModuleScript Named AddonService
--[[functions are ordered
1: AddCSLuaFile
2: include
3: AccessorFunc
...
]]
return table.unpack({
setmetatable({}, {__call =
function(_, module)
-- whatever it needs to do
end
}),
setmetatable({}, {__call =
function(_, module)
-- whatever it needs to do
end
})
-- etc.
gLua Script
local AddCSLuaFile, include, AccessorFunc... = require(game.AddonService)
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")
include("schedules.lua")
include("tasks.lua")
-- Variables
local ENT = {}
Enilm_fMaxYawSpeed = 200 -- Max turning speed
ENT.m_iClass = CLASS_CITIZEN_REBEL -- NPC Class
AccessorFunc( ENT, "m_iClass", "NPCClass" )
AccessorFunc( ENT, "m_fMaxYawSpeed", "MaxYawSpeed" )
function ENT:Initialize()
self:SetModel( "models/alyx.mdl" )
-- etc.
Before you suggest a table of functions, that will not work, like I said I'm adding support for an already defined language and as such I'm not writing the code myself.
I could feasibly write some plugin that detects globals, classes and such and turns them into an index for a table but I would really rather not go through with the mess that is String Patterns unless I absolutely have to.
My Solution
I decided that being able to iterate through all my meta-tables and assign them to a variable with a name I have stored somewhere would be my best option. But of coarse I don't know how to do that or whether its even possible.
#Any Ideas or Alternatives?
#ANSWER
After awhile I found getfenv() and setfenv(). Which is basically exactly what I want.

1 comment
try replacing words with script.source? sorry i dont know much about this studff