• Why doesn't this function work?
    5 replies, posted
Yes, me again. Don't worry though, this is probably a stupid problem that will be easy to solve. I have a function that should help with creating a lot of buttons on a vgui, but I have a problem, when I try to print the vgui button that should have been created by this function, it keeps returning 'nil'. I simplified the code quite a lot, and I'm pretty horrible at localizing, so the problem probably lies around something like that. [CODE] local function ButtonCreate(buttonname, buttonsize, buttontext, extraargs) local buttonname = self.list:Add( "DButton" ) buttonname:SetSize( buttonsize, 70 ) buttonname:SetText( buttontext ) extraargs() return buttonname end ButtonCreate(buttontest, 200, "Make the victim say something!", function() print(buttontest) end) [/CODE] I'm sure it's something obvious I missed. [editline]30th March 2015[/editline] By the way, I left out self.list from the example, as it shouldn't matter.
Firstly, you are asking for an argument that you overwrite in the first line. Secondly, you pass a nil argument (Buttontest is undefined) to the function. Thirdly, same thing for print. Fourthly, you don't have access to a the self variable in this instance. Only when you are defining a function like this do you have access to it: [lua]function Meta:Func() end [/lua] In Lua, you can't pass references like in C++. What you want to achieve is done like this: [lua] local function ButtonCreate(buttonname, buttonsize, buttontext, extraargs) _G[buttonname] = self.list:Add( "DButton" ) _G[buttonname]:SetSize( buttonsize, 70 ) _G[buttonname]:SetText( buttontext ) extraargs() return _G[buttonname] -- No idea why you are returning it but sure. end ButtonCreate("buttontest", 200, "Make the victim say something!", function() print(buttontest) end) [/lua]
I want the button to be local though (to the code calling the function), and _G means global, right? Or, just global to the code calling it (if that makes sense)?
[QUOTE=MPan1;47422230]I want the button to be local though (to the code calling the function), and _G means global, right? Or, just global to the code calling it (if that makes sense)?[/QUOTE] That makes sense, the way you formulating your code made be think you wanted them global. For local instances, you would do it like this: [Lua] local function ButtonCreate( buttonsize, buttontext, extraargs) local buttonname = self.list:Add( "DButton" ) buttonname:SetSize( buttonsize, 70 ) buttonname:SetText( buttontext ) extraargs() return buttonname end local buttontest = ButtonCreate(200, "Make the victim say something!", function() print(buttontest) end) [/lua]
[QUOTE=James xX;47422235]That makes sense, the way you formulating your code made be think you wanted them global. For local instances, you would do it like this: [Lua] local function ButtonCreate( buttonsize, buttontext, extraargs) local buttonname = self.list:Add( "DButton" ) buttonname:SetSize( buttonsize, 70 ) buttonname:SetText( buttontext ) extraargs() return buttonname end local buttontest = ButtonCreate(200, "Make the victim say something!", function() print(buttontest) end) [/lua][/QUOTE] Note that this'll still print nil because buttontest hasn't been assigned when extraargs() is run. You should probably do this: [lua] local function ButtonCreate( buttonsize, buttontext, extraargs) local buttonname = self.list:Add( "DButton" ) buttonname:SetSize( buttonsize, 70 ) buttonname:SetText( buttontext ) extraargs() return buttonname end local buttontest = ButtonCreate(200, "Make the victim say something!") print(buttontest) [/lua] If, however, you desperately want to manipulate it in a callback, you can pass it as an argument. [lua] local function ButtonCreate( buttonsize, buttontext, extraargs) local buttonname = self.list:Add( "DButton" ) buttonname:SetSize( buttonsize, 70 ) buttonname:SetText( buttontext ) extraargs( buttonname ) return buttonname end local buttontest = ButtonCreate(200, "Make the victim say something!", function(buttontest) print(buttontest) end) [/lua]
@Lixquid Yep, I desperately wanted to manipulate it! Thanks a LOT!
Sorry, you need to Log In to post a reply to this thread.