• Dynamic variable names?
    27 replies, posted
Can i dynamically assign a variable a name, along with creating one? like: [code] function createvar(varname,val) varname = val end [/code] but that wouldnt work. i need something that can actually create a variable, as in Money=2000. Is it possible?
For a global - [code]varname = val[/code] For a local - [code]local varname = val [/code] Is that what you needed?
[lua] function CreateVar(varName, val) _G[varName] = val; end [/lua] Although I can't possibly see what you would need this for.
No lol. I know how to create a variable. I want to DYNAMICALY create one. as in [code] for i=1,10 do createvar("Variable"..i,i) end [/code] would create 10 variables, each named Variable1 and Variable2 etc. with a value of i. so Variable1 would = 1 etc. [editline]09:50PM[/editline] [QUOTE=Nori;19064406][lua] function CreateVar(varName, val) _G[varName] = val; end [/lua] Although I can't possibly see what you would need this for.[/QUOTE] if that works, i love you.
Yes, it will CreateVar("MyVar1" , 2) And so on.
Can you explain why you'd want to do this? As it sounds like there's probably a much cleaner more efficient way of doing it.
Actually, i dont have a reason for wanting to do this. I think i might have a long time ago, but i dont remember it. I just think it would be useful knoledge in the future for me to know. Yes, i spelled it wrong, i know. Could you give an example of a cleaner and more effiecent way of doing it?
A more efficient way would be using a table.
I, personally, would use a table for such a thing. [lua] for i = 1,10 do Variables[i] = i end [/lua]
Don't forget to define the table first.
But how is it more effective? if i declare a variable, i can use it just by using its name, instead of having to do table[varname]. Is it quicker with a table, becuase the global table has so many values already or something?
Yes, but if you're using dynamic variables you won't know all of their names. If you want to loop through them, you'd want to have your own table instead of relying on the global table so that you don't get the massive number of already defined variables and functions into whatever.
If I'm not mistaken, Lua can manipulate a 500-entry table faster than it could 500 different variables. Also, using a table is extra-helpful if all these variables are related - "for" loops become especially elegant. Say, for instance, you wanted to multiply all your variables by 2. Using the table method, all you would need to say is: [lua] for k, v in pairs( Variables ) do Variables[k] = v * 2 end [/lua]
Modifying all the variables does not become harder, unlike you say, but it's horribly inefficient. [lua]for i = 1, 10 do _G[ "var" .. i ] = _G[ "var" .. i ] * 2 end[/lua]
Ok. One more thing, is it possible to dynamically create a function? Again, i have no use for this, just thought it would be cool :D
Yea, same as with variables.
Have a look at the AccessorFunc() source code: [url=http://luabin.foszor.com/code/lua/includes/util.lua#118][Link][/url] It creates multiple functions.
[QUOTE=NullPoint;19079270]Yea, same as with variables.[/QUOTE] Wait... so what? A variable is just a variable with a value... would i do something like [lua] _G[function sayhi(txt) Msg(txt) end] [/lua] That doesnt seem right to me.. but if it is... cool
[QUOTE=bobthe2lol;19214060]Wait... so what? A variable is just a variable with a value... would i do something like [lua] _G[function sayhi(txt) Msg(txt) end] [/lua] That doesnt seem right to me.. but if it is... cool[/QUOTE] You want this: [lua]_G["sayhi"] = function(txt) Msg(txt) end[/lua]
[QUOTE=bobthe2lol;19214060]Wait... so what? A variable is just a variable with a value... would i do something like [lua] _G[function sayhi(txt) Msg(txt) end] [/lua] That doesnt seem right to me.. but if it is... cool[/QUOTE] It is (nearly) valid code, since lua takes every object as reference. [lua] _G[function sayhi(txt) Msg(txt) end] = somevalue; [/lua] But that's not what have been asked. For dynamic functions, this here is valid: [lua]_G["FunctioNname"] = function() end;[/lua]
never mind, aVoN edited.
Thanks. I take it it would be horendus if i overwrote _G? :D
Read below.
You can't. [code]] lua_run_cl _G = nil ] lua_run_cl print( "hurr" ) hurr[/code]
what about [lua] for k, v in pairs(_G) do _G[k]=nil end [/lua]
You can do that.
that, in lua_run completely fucks up everything in gmod lol. [editline]04:09PM[/editline] Wait... can _G be accessed by both server and client?
[QUOTE=bobthe2lol;19215151]that, in lua_run completely fucks up everything in gmod lol. [editline]04:09PM[/editline] Wait... can _G be accessed by both server and client?[/QUOTE] Depends where you create it. If it's in a shared file then yes.
Sorry, you need to Log In to post a reply to this thread.