I apologize in advance for how stupid this question is, but as of yet I have not managed to find an adequate answer in any lua manuals, and the google has been mighty unhelpful.
Is 'for _ , k' any different than 'for v , k'? Is the underscore simply being used for the variable, rather than v, or does it do something fundamentally different?
I've seen it in several Gmod LUA scripts.
Sorry about being such a newbie.
Short answer Yes
Long answer
[lua]
abcd = {"a","b","c","d"}
for _,v in pairs(abcd) do
print(_,v)
end
[/lua]
Output
[code]
1 a
2 b
3 c
4 d
[/code]
I've learned that _ means lua shouldn't care about making a local variable about that.
[lua]
concommand.Add("test_tsetse",function(_,_,args)
end)[/lua]
could therefor be used on a clientside concommand.
There is no difference; "_" is a perfectly valid variable name.
And most people use it to indicate that that variable won't be used in the code (but as stated it very well can be)
[QUOTE=ralle105;29760996]And most people use it to indicate that that variable won't be used in the code (but as stated it very well can be)[/QUOTE]
Alright! Awesome.
Thanks very much, internet. :D
[QUOTE=ralle105;29760996]And most people use it to indicate that that variable won't be used in the code (but as stated it very well can be)[/QUOTE]
This should be added to the table tutorial on the wiki if it already isn't, I have seen quite a few now get confused about this exact topic
I normally use _ because I don't need a k in that foreach loop, but mainly use it in a second foreach loop eg
[lua]for _, v in pairs( player.GetAll() ) do
for k, g in pairs( ents.GetAll() ) do
if g == v then print( tostring( v ) ) end
end
end[/lua]
I don't use that above, it's just a stupid example.
[lua]for _, v in pairs( ents.GetAll() ) do
if( v:IsPlayer() ) then
print( tostring( v ) )
end
end[/lua]
:v:
[QUOTE=c-unitV2;29766548][lua]for _, v in pairs( ents.GetAll() ) do
if( v:IsPlayer() ) then
print( tostring( v ) )
end
end[/lua]
:v:[/QUOTE]
I normally use something like that too.
Sorry, you need to Log In to post a reply to this thread.