• Need informations about lua code.
    12 replies, posted
Hi, I learning Lua and some things seem weird to me. In this code is there a ( _, ) what mean this thing. and also if I don't insert arg into the parenthese of cmdGetNPCs wich problem can result ? function cmdGetNPCs( ply, cmd, arg ) for _, v in pairs( ents.GetAll() ) do if v:IsNPC() then print( "NPC found!" ) end end end concommand.Add( "getnpcs", cmdGetNPCs ) Thanks in advance.
"_," doesn't mean anything special. An underscore( _ ) is a perfectly valid variable name. In your case, _ holds the index of v. The three arguments( ply, cmd, arg ) are optional. However, if you don't have them and your code uses them, the code will throw an error saying they don't exist.
[QUOTE=Nevec;19659911]"_," doesn't mean anything special. An underscore( _ ) is a perfectly valid variable name. In your case, _ holds the index of v. The three arguments( ply, cmd, arg ) are optional. However, if you don't have them and your code uses them, the code will throw an error saying they don't exist.[/QUOTE] Thanks do you know where I can find some tutorials can explain more in deep for the variable and the arguments ?
[url]http://wiki.garrysmod.com/?title=Concommand.Add[/url] - the Wiki usually has a good explanation of things. As for your variable in the loop, it's the key associated with that value as Nevec said. If I did this code: [lua] myTable = {} myTable[1] = 5 myTable[2] = 12 myTable[3] = "hello" myTable[4] = "world" for myKey, myValue in pairs(myTable) do print(myKey.." = "..myValue) end [/lua] It would print out: 1 = 5 2 = 12 3 = hello 4 = world
Thanks alot it's helped alot :) But for in an example can you explain this one ? [URL="http://wiki.garrysmod.com/?title=Vehicle._tostring"]__tostring[/URL]
Functions like that are special functions you don't call by doing vehicle.__tostring() or anything like that. In the case of the function you posted, when you have a vehicle and you put it in print (where it needs to be a string), the __tostring function will be used to let the vehicle decide what string data to return. You know how with Vectors you can just do vec1+vec2? That's achieved through the special __add function defined for vectors that lets it decide exactly what should happen when you try to add two vectors just by doing vec1+vec2.
ok thanks again I have other things about scripting When I want to use this code it wont refresh my code in realtime. [code]lua_openscript autorun\client\test.lua[/code]
you need to do [code]lua_reloadents[/code] first.
[QUOTE=Chad Mobile;19676877]you need to do [code]lua_reloadents[/code] first.[/QUOTE] That's for reloading entities, not opening scripts. [editline]01:03AM[/editline] [QUOTE=lotus006;19673478]ok thanks again I have other things about scripting When I want to use this code it wont refresh my code in realtime. [code]lua_openscript autorun\client\test.lua[/code][/QUOTE] You have to open the script every time you edit it. Having it in autorun will have it run automatically when you join a server/start the server.
[QUOTE=CowThing;19677705]That's for reloading entities, not opening scripts. [/QUOTE] He said it isn't updating, so he has to reload it.
lua_reloadents reloads scripted entities, not autorun scripts.
Whatever, I always thought using lua_reloadents will update your scripts if you made any changes. [editline]07:36AM[/editline] Which is what he was asking.
[QUOTE=lotus006;19673478]ok thanks again I have other things about scripting When I want to use this code it wont refresh my code in realtime. [code]lua_openscript autorun\client\test.lua[/code][/QUOTE] am I only one that see's problem with that? He is running clientside code with a command to run serverside code he needed to use lua_openscript_cl autorun\client\test.lua
Sorry, you need to Log In to post a reply to this thread.