Trying to figure out problem with code for grid generation

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Megatt4

I´ve been trying to create a function that would generate a grid using draw(), but for some reason the function is only creating two lines, not just that but the Points array that is supposed to contain the coordinates for the points seems to be null

What´s the problem in the code?

Here´s the code:

extends Node2D

var SizeX = 3
var SizeY = 3
var LineColor = Color(1.0, 0.0, 0.0)

func draw_grid(SizeX, SizeY, LineColor):

#Reminder of the square size
var SquareSize = Vector2(50, 50)

#Point Type Check
var IsPassingLeftX = true
var IsPassingTopY = true

#Num of points
var PointsX = SizeX + SizeX
var PointsY = SizeY + SizeY
var NumOfPoints = PointsX + PointsY

#Spacing between squares
var SpacingValueX = Vector2(0, 50)
var SpacingValueY = Vector2(50, 0)


#Starting points
var StartingPoint = Vector2(100,100)
var StartingPointLeftX = Vector2(100,100)
var StartingPointTopY = Vector2(100,100)
var StartingPointRightX = Vector2(100,100) + SpacingValueX * SizeX
var StartingPointDownY = Vector2(100,100) + SpacingValueY * SizeY

#Array of points
var Points = PoolVector2Array()

#Get all X points from the left side of screen
for i in PointsX:
	if(IsPassingLeftX == true):
		Points.push_back(StartingPointLeftX)
		StartingPointLeftX = StartingPointLeftX + SpacingValueX
		IsPassingLeftX = false
	else:
		Points.push_back(StartingPointRightX)
		StartingPointRightX = StartingPointRightX + SpacingValueX
		IsPassingLeftX = true
		

#Get all Y points from the top side of the screen
for i in PointsY:
	if(IsPassingTopY == true):
		Points.append(StartingPointTopY)
		StartingPointTopY = StartingPointTopY + SpacingValueY
		IsPassingTopY = false
	else:
		Points.append(StartingPointDownY)
		StartingPointDownY = StartingPointDownY + SpacingValueY
		IsPassingTopY = true


#draw the grid
for i in range(NumOfPoints - 1):
	draw_line(Points[i], Points[i + 1], LineColor)
	i = i + 1
func _process(delta):
update()

func _draw():
draw_grid(SizeX, SizeY, LineColor)

I’m not sure what’s going on with your code, but I personally wouldn’t use the draw() function to create a grid. I would create a grid by using instances, instancing a ColorRect() square into my current scene, setting it’s color and position there, and then moving on to the next square. If you would like, I could put an example project/code here. Though, you’d have to wait a while for me to create it.

TheJokingJack | 2021-07-11 16:02

Thanks for the answer!

A while after asking the question i was able to make a simpler code that worked, but i might still try your suggestion because it does sound easier and more eficient for my purposes

Megatt4 | 2021-07-11 17:55

Sure! The easy thing about the method I use is you can be a lot more flexible with what you do with each scene. You could add an Area2D, a sprite, and animatedtexture, really, anything. Ask me about it when you’d like.

TheJokingJack | 2021-07-11 19:23