• Object properties in for in loop
    11 replies, posted
I decided to learn lua this morning, and I ended up making a derma menu. The problem is, I don't know how to set a custom property of an object, or use self. Here's the part of the code I'm using: [code] for k,v in pairs(Table) do _G["BPImg"..k] = vgui.Create('DImageButton') _G["BPImg"..k]:SetParent(DPanel1) _G["BPImg"..k]:SetSize(50, 50) _G["BPImg"..k]:SetPos(5 + ((k-1)*55), 5) _G["BPImg"..k]:SetImage('') _G["BPImg"..k].IDName = v --Here's where i'm trying to set the custom property _G["BPImg"..k].DoClick = function() NameLabel:SetText(self.IDName) --Here's where i'm trying to read from it end end [/code] Can anyone help?
Why do you need the table and the for loop? You could just declare all the different control by themselves, that would be much easier.
[QUOTE=c0baltha1l;24118347]Why do you need the table and the for loop? You could just declare all the different control by themselves, that would be much easier.[/QUOTE] well, the table gets edited later on, and the SetImage part is going to end up looking like "SetImage("materials/"..v..".vtf")." Also, the table can be anywhere from 1-6 values long.
[code] for k,v in pairs(Table) do _G["BPImg"..k] = vgui.Create('DImageButton') _G["BPImg"..k]:SetParent(DPanel1) _G["BPImg"..k]:SetSize(50, 50) _G["BPImg"..k]:SetPos(5 + ((k-1)*55), 5) _G["BPImg"..k]:SetImage('') _G["BPImg"..k].IDName = v _G["BPImg"..k].DoClick = function() NameLabel:SetText(self.IDName) end end [/code] This looks like it would work, also maybe they should be part of their parent object's table (DPanel1) instead of the global table, which they will be polluting.
[QUOTE=Crazy Quebec;24118800] -code- This looks like it would work, also maybe they should be part of their parent object's table (DPanel1) instead of the global table, which they will be polluting.[/QUOTE] Oh, sorry, the vgui: thing isn't there in the file, I was trying it because it said to do something similar for a SWEP that had problems with self.
The error is on the self part: [code]cl_init.lua:67: attempt to index global 'self' (a nil value)[/code] That's [code] NameLabel:SetText(self.IDName) [/code]
Is that line of code in a function that starts with a system variable like SWEP: or ENT: or GM: or SWEP:?
[QUOTE=c0baltha1l;24122278]Is that line of code in a function that starts with a system variable like SWEP: or ENT: or GM: or SWEP:?[/QUOTE] It's in a DoClick function on a derma image button.
Is self supposed to be indexing the derma frame? If not, what are you coding this for? (swep, entity, gamemode, etc)
self is supposed to be indexing the ImageButton. it's for a gamemode.
So, the code sets the value to the I'd of the imagebutton? If so, I'll assume your button is called mybutton so put this code at the top of the file out of any functions: [code]local mybutton[/code] Replace mybutton with the variable name of your imagebutton. In the code with the error, replace self with the name of your button.
I can't do that, because it's in a for loop. That would make each button do the same thing. I can't do "_G["BPImg"..k].IDName" either, because then each time any of the image buttons are pressed, it will try to do "BPImg7.IDName," which doesn't exist. [b]EDIT[/b]: Hmm, I guess lua is different than actionscript. Using "NameLabel:SetText("Name: ".._G["BPImg"..k].IDName)" worked. Thanks anyway.
Sorry, you need to Log In to post a reply to this thread.