• Problem with unpacking args (concommand.Add)
    4 replies, posted
The problem is with unpacking args on a concommand, My code wrote fine just i think there's something causing this, I tried both unpack(args) and unpack(args, 1) but both didn't work, Is the lib changed on GMod 13? [lua] function test(ply, cmd, args) local msg = unpack(args) print(msg) end concommand.Add("test", test) [/lua] Whenever i run the command it just can detect args[1] nothing else, also if i run the command with more args like having unpack from args[3] it does the same and just detects args[3].
The reason you're having the "issue" is because unpack returns the same was as a function returns like this: [lua]--[[--------------------------------------------------------- AddSubMenu FROM garrysmod/lua/vgui/dmenu.lua -----------------------------------------------------------]] function PANEL:AddSubMenu( strText, funcFunction ) local pnl = vgui.Create( "DMenuOption", self ) local SubMenu = pnl:AddSubMenu( strText, funcFunction ) pnl:SetText( strText ) if ( funcFunction ) then pnl.DoClick = funcFunction end self:AddPanel( pnl ) return SubMenu, pnl end function unpackerTest( ) return 1, 2, 3, 4, 5; end concommand.Add( "unpacker", function( ply, cmd, args ) print( unpack( args ) ) end )[/lua] In unpackerTest, if you want to access all the return values you need to call it like so: [lua]local _var1, _var2, _var3, _var4, _var5 = unpackerTest( )[/lua] The same is true with the dmenu example. If you want to add an image to a sub menu, when you call PANEL:AddSubMenu( ) you need to assign two variables because _var1 is the SubMenu and not the actual panel so SetImage won't work: [lua]local _var1, _var2 = PANEL:AddSubMenu( name, func ); _var2:SetImage( image )[/lua] Using unpack on the args directly in the print function, it will print properly because print takes 1 to many arguments. You're only returning the first argument ( because it's being unpacked the same way a function returns many variables in a single return statement without using a table as a return type ) and assigning it to msg.
[QUOTE=Acecool;41510493]The reason you're having the "issue" is because...[/QUOTE] What you mean is instead of making a var i have to use the unpack it self? I don't think it would change if i don't set any variable.
[code]local msg = unpack(args)[/code] The return values from unpack are getting assigned to one variable only (msg) therefore all return values apart from the first will be discarded. You either need to assign the return values from unpack to more variables: [code]local a, b, c, d = unpack(args) print(a, b, c, d)[/code] or use the return values of unpack directly without storing them: [code]print(unpack(args))[/code]
[QUOTE=MakeR;41510661][code]local msg = unpack(args)[/code] The return values from unpack are getting assigned to one variable only (msg) therefore all return values apart from the first will be discarded. You either need to assign the return values from unpack to more variables: [code]local a, b, c, d = unpack(args) print(a, b, c, d)[/code] or use the return values of unpack directly without storing them: [code]print(unpack(args))[/code][/QUOTE] Okay i got it right now, Thank you and solved!
Sorry, you need to Log In to post a reply to this thread.