• For loop help!
    3 replies, posted
Hello facepunch, I'm wondering if someone could help me with creating buttons in an orderly fashion. I'm trying to make 9 buttons using the for loop, but on each like they has to be three so that means they'll be 3 lines with 3 buttons on each line right? The problem is, I can't get the buttons to reset back to a postion then work from that and so on, any help? [CODE] for i=1, 9 do local Buttoni = vgui.Create( "DButton", Frame ) Buttoni:SetText( i ) Buttoni:SetTextColor( Color( 255, 255, 255 ) ) Buttoni:SetPos(20 * 1, i == 3 * ( 20 ) ) // I tried to do this so, every three buttons it'll automatically move down and so on... Buttoni:SetSize( 50, 50 ) Buttoni.Paint = function( self, w, h ) if !Buttoni:IsHovered() then draw.RoundedBox( 0, 0, 0, w, h, Color( 169,169,169, 250 ) ) -- Draw a lightgray button else draw.RoundedBox( 0, 0, 0, w, h, Color( 128,128,128, 250 ) ) -- Draw dgr button end end Buttoni.DoClick = function() end end [/CODE] Help will be much appreciated, thanks
use the modulus operator. Here is one way. I also changed your paint function a bit [code] local down = 50 for i=1, 9 do local Buttoni = vgui.Create( "DButton", Frame ) Buttoni:SetPos( 20 * 1, down ) Buttoni:SetSize( 50, 50 ) Buttoni:SetTextColor( Color( 255, 255, 255 ) ) Buttoni:SetText( i ) Buttoni.Paint = function( self, w, h ) draw.RoundedBox( 0, 0, 0, w, h, self:IsHovered() and Color( 169,169,169, 250 ) or Color( 128,128,128, 250 ) ) end Buttoni.DoClick = function() end if ( i % 3 == 0 ) then down = down + 50 end end [/code]
[QUOTE=Invule;51942688]use the modulus operator. Here is one way. I also changed your paint function a bit [code] local down = 50 for i=1, 9 do local Buttoni = vgui.Create( "DButton", Frame ) Buttoni:SetPos( 20 * 1, down ) Buttoni:SetSize( 50, 50 ) Buttoni:SetTextColor( Color( 255, 255, 255 ) ) Buttoni:SetText( i ) Buttoni.Paint = function( self, w, h ) draw.RoundedBox( 0, 0, 0, w, h, self:IsHovered() and Color( 169,169,169, 250 ) or Color( 128,128,128, 250 ) ) end Buttoni.DoClick = function() end if ( i % 3 == 0 ) then down = down + 50 end end [/code][/QUOTE] I did try that before, but the outcome was 3 buttons going down of 3, 6 ,9.
You can always use two for loops, one that controls horizontal and one that controls vertical. [lua]for i = 1,3 do for j = 1,3 do ... button:SetPos(i*20, j*20) ... end end[/lua]
Sorry, you need to Log In to post a reply to this thread.