Multiple 'For Loops'

Is it possible to use multiple for loops in each other?
I have scanned through wiki, it only shows single for loops.

I am trying to have 10 buttons create with a for loop.
I can get the buttons to spawn, but my next for loop below the 10 button creation is naming the buttons from a table. I can also get that to work.
The issue being is that the buttons spawn, but it’s an infinite loop of them.
(Using DHorizontalScroller).

I don’t understand. Is this what you want?

[lua]for i1 = 1, 10 do

for i2 = 1, 10 do

end

end[/lua]

[lua]
local CScroller = vgui.Create(“DHorizontalScroller”, MFrame)
CScroller:SetSize(ClassPanel:GetWide() - 8, 40)
CScroller:AlignBottom(1)
CScroller:AlignLeft(1)
CScroller:SetOverlap(-5)

local ClassButtons = {}
local ButtonsText = {"Citizen", "CP", "Mayor", "Gangster", "DrugLord", "Doctor", "Chef", "Chief", "Reporter"}



for i=1, 10 do 
    for k, v in pairs(ButtonsText) do
        ClassButtons* = vgui.Create("DButton", CScroller) --Create the button
        ClassButtons*:SetSize(140, 40)
        ClassButtons*:SetText(v)
        CScroller:AddPanel(ClassButtons*) --Add each button to our Scroller after we create it.    
    end
end

[/lua]

Current code ^
That is causing infinite loop.

That code works perfectly for me, save that it is creating each button 10 times.

-snip- misread.

Thats the problem, I don’t want to have each button created *10.
I only want 10 total, buttons, 1 for each class to be created.
Yet this, for loop I have doesn’t allow that, as you said, it creates each button *10.

Toss one of the loops and you’re done.

Toss the tables loop, you have to add 40’odd more lines of code,
Toss the button spawn loop, again, you have 40’odd lines of code.
I thought using a loop would help optimize it.

  • I wouldn’t mind learning how to work with multiple for loops, in case I need to use them in the future.

[lua]
local CScroller = vgui.Create(“DHorizontalScroller”, MFrame)
CScroller:SetSize(ClassPanel:GetWide() - 8, 40)
CScroller:AlignBottom(1)
CScroller:AlignLeft(1)
CScroller:SetOverlap(-5)

local ClassButtons = {}
local ButtonsText = {"Citizen", "CP", "Mayor", "Gangster", "DrugLord", "Doctor", "Chef", "Chief", "Reporter"}


for k, v in pairs(ButtonsText) do
    ClassButtons* = vgui.Create("DButton", CScroller) --Create the button
    ClassButtons*:SetSize(140, 40)
    ClassButtons*:SetText(v)
    CScroller:AddPanel(ClassButtons*) --Add each button to our Scroller after we create it.    
end

[/lua]

There you go.

Thank you, after removing the *, it worked like a charm.
How ever I have run into another dilemma, Instead of using DButton in the Scroller, I would like to use SpawnIcons. How ever when attempting to do this, I get the following error;

[lua]
local ClassPanel = vgui.Create( “DPanelList”, MFrame)
ClassPanel:EnableHorizontal(true)
–ClassPanel:EnableVerticalScrollbar(false)
ClassPanel:SetPadding(1)
ClassPanel:SetSize(425, 90)
ClassPanel:SetPos(50, 35)

local CScroller = vgui.Create("DHorizontalScroller", MFrame)
CScroller:SetSize(ClassPanel:GetWide() - 8, 40)
CScroller:AlignBottom(1)
CScroller:AlignLeft(1)
CScroller:SetOverlap(-5)

local ClassButtons = {}
local ButtonsText = {"models/Humans/Group01/male_06.mdl", "models/player/police.mdl", "models/player/breen.mdl", "models/player/group03/male_01.mdl", "models/player/gman_high.mdl", "models/player/kleiner.mdl", "models/player/mossman.mdl", "models/player/police.mdl", "models/Eli.mdl"}


for k, v in pairs(ButtonsText) do
    ClassButtons = vgui.Create("SpawnIcon", CScroller) --Create the button
    ClassButtons:SetIconSize(64)
    ClassButtons:SetModel(v)
    CScroller:AddPanel(ClassButtons) --Add each button to our Scroller after we create it.    
end

[/lua]

Try ClassPanel:AddItem(ClassButtons) and below the for loop CScroller:AddPanel(ClassPanel)

Still receiving that error.

[lua]for k, v in pairs(ButtonsText) do
ClassButtons = vgui.Create(“SpawnIcon”, CScroller) --Create the button
ClassButtons:SetModel(v)
ClassPanel:AddItem(ClassButtons) --Add each button to our Scroller after we create it.
end
CScroller:AddPanel(ClassPanel) [/lua]

I’ll fire up the good 'ol notepad and Gmod Wiki. If I’m not ninja’d i’ll try to fix.
:f5:
[editline]12:19AM[/editline]

Idea: have you used SetUp on the scrollbar?

Negative.

facepalm Nevermind, scratch that. I misread somthing

[editline]12:29AM[/editline]

Try this:



local ClassPanel = vgui.Create( "DPanelList", MFrame)
    ClassPanel:EnableHorizontal(true)
    --ClassPanel:EnableVerticalScrollbar(false)
    ClassPanel:SetPadding(1)
    ClassPanel:SetSize(425, 90)
    ClassPanel:SetPos(50, 35)
    
    local CScroller = vgui.Create("DHorizontalScroller", MFrame)
    CScroller:SetSize(ClassPanel:GetWide() - 8, 40)
    CScroller:AlignBottom(1)
    CScroller:AlignLeft(1)
    CScroller:SetOverlap(-5)
    
    local ClassButtons = {}
    local ButtonsText = {"models/Humans/Group01/male_06.mdl", "models/player/police.mdl", "models/player/breen.mdl", "models/player/group03/male_01.mdl", "models/player/gman_high.mdl", "models/player/kleiner.mdl", "models/player/mossman.mdl", "models/player/police.mdl", "models/Eli.mdl"}
    
    
    for k, v in pairs(ButtonsText) do
        ClassButtons[k] = vgui.Create("SpawnIcon", CScroller) --Create the button
        ClassButtons[k]:SetIconSize(64)
        ClassButtons[k]:SetModel(v)
        CScroller:AddPanel(ClassButtons[k]) --Add each button to our Scroller after we create it.    
    end


[editline]12:30AM[/editline]

(You tried to add a table as a panel, and that was a problem. You previously told lua that ClassButtons was a table)

I am still getting the exact same error

That’s very odd. I’ll talk to you in a minute, I have to check somthing

No worries.
Appreciate the help you’re giving.

I don’t think SpawnIcon will work for that. I doubt SpawnIcon has ApplySchemeSettings as a function.

[editline]12:40AM[/editline]

Sadly, I think you will have to compensate.

[editline]12:41AM[/editline]

Confirmed, you have to compensate.

That’s a real shame.
I was experimenting with DPanelList and Horizontal scroll bar, buggered if I could get the H-bar to show.
I’ll work on it, see how it goes.
Once again, thank you for the help.