One DrawPoly works great. The second does not. (Help needed!)
1 replies, posted
So I am working on a small game mode. I have been working with Lua for about three years now, but I still get some problems that I can't figure out.
So, after my game mode starts up, there's two buttons in the shape of a hexagon popping up. They both use the same function to plot the six coords it takes to make a hexagon, and they are the same exact buttons only with different identifiers. The related code is this:
[CODE]
local Panel = vgui.Create( "DPanel" )
Panel:SetPos( 0, 0 )
Panel:SetSize( ScrW(), ScrH() )
Panel:MakePopup()
Panel.Paint = function()
surface.SetDrawColor( 0, 0, 0, 255 )
surface.DrawRect( 0, 0, ScrW(), ScrH() )
end
local Hexagon = create_hex( 100 ) -- function listed below
Char_Create = vgui.Create( "DButton", Panel )
Char_Create:SetPos( ScrW()/2, ScrH()/2 )
Char_Create:SetSize( 200, 200 )
Char_Create:SetFont( "Unique1" )
Char_Create:SetText( "CREATE CHARACTER" )
Char_Create.Paint = function()
surface.SetDrawColor( 220, 220, 220, 255 )
surface.DrawPoly( Hexagon )
end
Char_Select = vgui.Create( "DButton", Panel )
Char_Select:SetPos( ScrW()/2-200, ScrH()/2-200 )
Char_Select:SetSize( 200, 200 )
Char_Select:SetFont( "Unique1" )
Char_Select:SetText( "SELECT CHARACTER" )
Char_Select.Paint = function()
surface.SetDrawColor( 220, 220, 220, 255 )
surface.DrawPoly( Hexagon )
end
[/CODE]
[CODE]
function create_hex( radius )
local tab = {}
local increment = 1
while ( increment <= 6 ) do
tab[increment] = { x = math.sin(math.rad(increment*60))*radius+100, y = math.cos(math.rad(increment*60))*radius+100 }
increment = increment + 1
end
return tab
end
[/CODE]
Now, which ever button (Char_Create or Char_Select) is places on top in the script will have its white haxagon drawn. It doesn't matter which one, either of them will work if it's on top. Since the create_hex( radius ) function is only ran once, the Hexagon table that holds the table of values should hold the same coordinates and hence each button should look the same.
Also, I have tried ignoring the Hexagon table and just running create_hex( radius ) in each of the separate surface.DrawPoly() statements, essentially running the create_hex function every frame. Extremely innefficient (debug purposes only), but it still didn't work.
Any ideas? I would love some hints in the right direction. Also, if you see anything that I've done in my script that is bad practice, and you have the time to share, please let me know so that I can improve. I am always trying to improve my scripting skills.
P.S One thing I haven't tried is having my local Hexagon = create_hex( radius ) for the first hexagon, and then running Hexagon = create_hex( radius ) for the second one (to run it twice). The only reason this would be necessary (in my mind) is if one you assigned the table to one hexagon, it cleared the table of its data. I haven't tried it yet because going off of my knowledge of data structures in any programming/scripting language, that is definitely not how it works. (([B]EDIT! Just tried this anyways and it did not work. [/B]))
Thank you in advance!
[thumb]http://images.akamai.steamusercontent.com/ugc/387669574546065517/D2A32FFCC8F0604EB9295294B1DE24C0C7C8E3E8/[/thumb]
[I]In regards to the image above, the one on top is written after the bottom hexagon in the script (as I listed it on here)[/I]
[editline]21st November 2015[/editline]
I can not continue without this being fixed. While I have been waiting for a couple hours, I have tried some more debugging which will hopefully be useful to any one who has no idea what may be going on.
For the second button, I manually wrote a table out with x/y coords in it and threw it into my surface.DrawPoly(). Still didn't show up. I also verified the integrity of my game cache (that's how lost I am). Nothing.
I then removed the DrawPoly statement completely and replaced it with a simple surface.DrawRect(), and this worked fine (a square).
My assumption is that it is exculsively a problem with surface.DrawPoly(). Yes, I could change the button to a texture or material instead, but then I lose the flexibility and other options that I have with DrawPoly and I would like to not have to resort to that as long as my scripting is fine.
Nevermind. I got it working. If anybody EVER happens needs help with this (ha), here is my solution:
Reformatted the function to create the hexagon coordinates. Here it is:
[CODE]
function WriteHexagon()
local Radius = 100
local tab = {}
local number = 1
while number <= 6 do
table.insert( tab, number, { x = math.Round(math.cos( math.rad(60*(number-1)) ) * Radius)+100, y = math.Round(math.sin( math.rad(60*(number-1)) ) * Radius)+100 } )
number = number+1
end
return tab
end
[/CODE]
Basically there are no longer any parameters to the function, and instead of using the iteration it is on to store the key value, I used table.insert
I also added draw.NoTexture() before my surface.DrawPoly()'s. I forgot to do it before and though that did not fix the problem at first, it is good practice.
Sorry, you need to Log In to post a reply to this thread.