Converting a table to normal variables?

How could I convert a table with unique keys to normal variables like


table = {[Cha] = 4, [Cha2] = 1, [Cha8] = 6}
to
Cha = 4 Cha2 = 1 Cha8 = 6

Are you making them global?
If so try

[lua]
function GlobalVarsFromTable(tab)
for k, v in pairs(tab) do
_G[k] = v
end
end
[/lua]

Tried it before but forgot to destring it so didn’t work for me :confused: thanks and what if I want to set them local?

I guess you’ll need to hack around with the debug library, ralle posted a script to create an _L table to contain all the local variables in the scope but I lost it.

Thanks ok, I’ll just give keys in the table a bit more unique names, thanks.

You can’t do this using a loop because the variable name would be invariable, meaning instead of making more than 1 variable you’d redefine it. You can convert a bunch of variables into an array with a loop but not the other way around.

Do you mean to make it local? If not then I have no idea what you are saying, because Loures’s way works perfectly.

[editline]26th December 2010[/editline]

The variable names consist of unique names, I’ve mentioned it earlier, so no problem if wanting to make global variables.

RunString?

Do you mean

[lua]
function GlobalVarsFromTable(tab)
for k, v in pairs(tab) do
RunString("local " … k … " = " … v)
end
end
[/lua]
?

Thats cool, would that actually work? And how recourse consuming is it?

RunString is declared in Lua by default, and it depends on how much the table is big, and yes that code would work.

what is this for?

you can make them globals before you call the function and right after the function is called you nil them.

I’ve basically got a table of convars, since getconvarnumber() is really expensive i’m assigning convars onto variables:[LUA]local ConVarT = {[“Con_Var3”] = “4”, [“Con_Var3”] = “7”, [“Con_Var3”] = “5”}
for CVk,CVv in pairs(ConVarT) do if !ConVarExists(CVk) then CreateConVar(CVk, CVv, {FCVAR_NOTIFY, FCVAR_ARCHIVE}) end end

hook.Add(“Think”, “Thinky”, function() if !ConVTm or CurTime() >= ConVTm then for Con,_ in pairs(ConVarT) do _G[Con] = GetConVarNumber(Con) end ConVTm = CurTime()+ 0.1 end end[/LUA]The table currently consists of over 50 Convars about 10 of them need to be ran to check if a part of the code is enabled, so this gives about 200% performance boost to my script.

And Global variables actually work better for me this time, just curious if there is a way :confused:

Since you said GetConVarNumber() is expensive, try to create a function adding the tables and saving their values.
[lua]ConsoleVars = {};

function AddNew_ConVar( ConStr, ConValue, ConEnum )
ConStr, ConValue = tostring( ConStr ), tostring( ConValue );
if ( ConVarExists( ConStr ) || ConsoleVars[ ConStr ] ) then return; end

CreateConVar( ConStr, ConValue, ConEnum );
ConsoleVars[ ConStr ] = { Value = ConValue, FCVAR = ConEnum };

end

function ReturnNew_ConVar( ConStr )
if( ConsoleVars[ ConStr ] ) then
if( ConsoleVars[ ConStr ].FCVAR == FCVAR_ARCHIVE || table.HasValue( ConsoleVars[ ConStr ].FCVAR, FCVAR_ARCHIVE ) ) then
return GetConVarNumber( ConsoleVars[ ConStr ] );
end
end
return ConsoleVars[ ConStr ].Value or NULL;
end

AddNew_ConVar( “mp_assholemeat”, “5”, { FCVAR_REPLICATED, FCVAR_ARCHIVE } ); //I do not suggest archive. At all D:
[/lua]Untested theory?

The code I wrote earlier does exactly the same thing, just less messy.