# Drawing a polygon 2d Attention Topic was automatically imported from the old Question2Answer platform. Asked By ashish

Hi,
I am trying to draw a polygon with the help of the method drawpolygon method.
The drawpolygon method requires a PoolVector2Array() and PoolColorArray as parameters.
I want to do 3 things:
(a) Draw a rectangle through this method under the virtual function _draw()
(b) Set the colour of the rectangle in consonance with the position of a VSlider node.
(c) Change the colour of the rectangle at runtime by moving the slider position up and down.
I am not sure if I am correctly passing values in the parameters, for I am getting a triangle as opposed to a rectangle. And, neither am I able to get the desired colour nor am I able to change colours by sliding the VSlider at runtime.

Here is my code:

``````extends Control

slider.value = 100

func _draw():
var col = {}
var points = PoolVector2Array()
var colour = PoolColorArray()
points = [Vector2(100,100), Vector2(200,100), Vector2(200,200),Vector2(100,200)]
colour = [slider.value,1,1,1]
draw_polygon(points,colour)
``````

Here is the output that I am getting: Reply From: Thomas Karcher

Almost correct.

1. Draw is called only once when the node enters the tree. Afterwards, you need to call it via update()
2. The PoolColorArray requires elements of type Color

This code should work:

``````extends Control

slider.value = 1 # must be between 0 and 1
slider.connect("value_changed", self, "redraw")

func redraw(_value):
update()

func _draw():
var points = PoolVector2Array()
var colour = PoolColorArray()
points = [Vector2(100,100), Vector2(200,100), Vector2(200,200),Vector2(100,200)]
colour = [Color(slider.value,1,1,1)]
draw_polygon(points,colour)
``````

Thank you very much. This works.
I made the slider.value as a fraction of 100 when calling the Color method. The slider was set to 1 to 100 with steps of 1. Therefore, I made it into a fraction of 100.

One question, however:

Why have you put an underscore before value in redraw function? Is that a typical coding style or is it required?

Thanks again.

ashish | 2021-04-08 09:23

Glad it works! Regarding the underscore: It’s used to suppress the editor warning about unused arguments. If you remove it, the code will still work as before, but the editor will show a warning.

Thomas Karcher | 2021-04-08 09:43