0 votes

I've been trying to write a function that would use draw() to draw a grid on the screen, but i'm trying to figure out the reason to why the code isn't working

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)

Godot version Godot 3.2.3
in Engine by (34 points)

1 Answer

0 votes
extends Node2D
var color = Color(0,0,0)
var screen_size = OS.window_size
func _draw():
    draw_grid(10,10,color)
func draw_grid(SizeX, SizeY, LineColor):
    for x in range(0,screen_size.x,SizeX):
        draw_line(Vector2(x,0),Vector2(x,screen_size.y),LineColor,1)
    for y in range(0,screen_size.y,SizeY):
        draw_line(Vector2(0,y),Vector2(screen_size.x,y),LineColor,1)
by (454 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.