Hello dear coders,
I don't possess the knowledge or experience to figure out the correct syntax for what I'm attempting to do.
So I basically have a simple derma menu that loads up a list of icons of props found within the map:
for k,v in pairs(propmenu) do
local icon = vgui.Create( "SpawnIcon", IconList )
icon:SetModel( v:GetModel() )
IconList:AddItem( icon )
And the issue I'm having is that because these maps contain the same prop multiple times, it creates duplicate icons of the same prop in the derma menu.
Is there any way I could tell it to
if ( icon ) = 1, then IconList:Remove( icon )
Clearly not correct at all, but you get the gist of what I'm attempting.
Thanks in advance for any help
Hello Goku-,
It's hard to be sure whether this solution will work without further information on your code.
Try this:
for k ,v in pairs(propmenu) do
local icon = vgui.Create( "SpawnIcon", IconList )
icon:SetModel( v:GetModel() )
local found = false
for k1, v1 in pairs(IconList) do
if v1:GetModel() == v:GetModel() then
found = true
end
end
if !found then
IconList:AddItem( icon )
end
end
If it does not work then please reply with the error and a full copy of the code.
Hey, so I input it in the code correctly (had to remove an additional end a few lines below) and when opening the derma menu we end up with only a single prop in total as well as this error:
[ERROR] addons/menu/lua/prophunt/sh_init.lua:70: bad argument #1 to 'pairs' (table expected, got userdata)
1. pairs - [C]:-1
2. unknown - addons/menu/lua/prophunt/sh_init.lua:70
3. unknown - lua/includes/modules/concommand.lua:54
And @Shendow , I think I get the gist of what you're saying. But unfortunately I'm not sure how to do that on my own haha. Thanks for the input though.
If this error isn't enough, feel free to ask and I'll just dump the whole code.
Any ideas?
Also I want to edit this post to offer some coins for an answer now, but I can't figure out how.
It appears I can't add coins for an answer after having created the post, and yet I really would like some more help on this script, because I'm just hitting dead-ends every time I try modifying stuff myself.
So I didn't see this in the guidelines, but am I allowed to basically create a new post with the same question?
Thanks.
We could probably help you much easier if you posted a copy of the file your working on using either code tags or pastebin.
I believe the solution is two loops.
[code]
local proplist = {}
for k,v in pairs(propmenu) do
proplist[v:GetModel()] = true --I have it store the model in the key, not the value, because there are no duplicate key entries. The same key may be set to true multiple times, but this has no different effect than if it was set once
end
for k, v in pairs(proplist) do -- now we just loop through the list we just made, and ignore the values, just using the keys to get the model names
local icon = vgui.Create("SpawnIcon", IconList)
icon:SetModel(k)
IconList:AddItem(icon)
end
[/code]
Hello Joshb,
Yes that loop works perfectly.
I had not thought about using your method but that definitely helped me learn. I'm gonna spend some time playing with loops for some of my other scripts.
Thanks again!
Sorry, you need to Log In to post a reply to this thread.