Grid system not being drawn

So I’m pretty new to Godot, and I need help with a script not properly working. It involves drawing a grid not drawing. I sorta messed around with scenes and nodes, until the grid stopped appearing. Last time it worked, but now it doesn’t, and I grabbed this from chatgpt for a grid. (not saying I will be relying on chatgpt, but asked for a simple script making a grid)

Here is the code if needed :

extends Control

var grid_size = Vector2(32, 32)  # Adjust the grid size as needed

func _draw():
	for x in range(0, get_viewport_rect().size.x, grid_size.x):
		draw_line(Vector2(x, 0), Vector2(x, get_viewport_rect().size.y), Color(1, 1, 1))

	for y in range(0, get_viewport_rect().size.y, grid_size.y):
		draw_line(Vector2(0, y), Vector2(get_viewport_rect().size.x, y), Color(1, 1, 1))
func snap_vector(pos):
	return Vector2(
		int(pos.x / grid_size.x) * grid_size.x,
		int(pos.y / grid_size.y) * grid_size.y

Any help or pieces of vital information would be very much appreciated.

Have a look here, it might help you:

1 Like

here is an example that draws a line through a button. imho chatgpt probably isn’t the best option

extends Button
func _draw():
	var v_rect : Rect2 = get_rect()
	draw_line(Vector2(0,0), v_rect.size, Color.RED, 10)

I might’ve not been clear about my topic, but I’m looking for a solution for the grid drawing script (as shown in the topic description), to be displaying properly. I just used chatgpt for a quick look at a potential grid drawing system that I could use. I won’t probably be using it for any future code.

part of being a good programmer is leaning how to write code from scratch. we provided you information on how to draw lines. here is code that draws lines horizontally. you can add a loop that draws the vertical lines.

extends Button
func _draw():
	for j_position_y in range(0,300,25):
		draw_line(Vector2(0,j_position_y), Vector2(200,j_position_y), Color.RED, 5)

1 Like

Ok well, it appears the root of the problem is the _draw not really “drawing” anything. I copied your code, pasted it into a script, and attached the script to a button instance, and doesn’t display anything whenever I run the project. I know about the parts of loops like for loops to make patterns, but that’s beside the point.

Though, I will analyze your code for future references to figure it out on my own.

Read the help I linked and try those examples.

And don’t feel shy about asking here. We all start fresh, every time we learn something new!

1 Like

I read the examples in the link you provided before, but I can’t seem to make any of the _draw functions display anything. Unless I use “Run Current Scene”, it won’t appear. Is there a setting that is affecting this? Or are the scenes messing it up?

Ah, you need to use the tool command!

@tool at the top (Godot 4, or tool in 3) makes it run in the editor. Try that.

when i run my project i see the lines

I meant that whenever I start up the project it doesn’t show anything, but when I use “Run Current Scene”, the line appears. I looked up what the @tool meant, which happens to be useful for running anything in the editor, but it doesn’t help with my situation.

Unless I’m being too vague or making my words misunderstood, I’m trying to say that the _draw function doesn’t display any drawings or lines of anything when I run the project/play my project. Its until I “Run Current scene”, that it shows up.

Are you sure the right scene is set as the ‘main’ one? i.e when you hut run (F5) it may be running an old scene.

can you attach a screenshot of your project?

I don’t have anything on it, no lines, no nothing. I even got the control node attached to the script I wanted it to do. Just a blank canvas.

I’m not sure if I am running the right scene as the main one, nor know if I even am on the right scene.

Right click the scene you know you want to run and choose “Set as Main Scene”

You can also put some text or an image in it so you can see what is running.

1 Like

the “set as main scene” will highlight blue


1 Like

Ah, found it, and apparently, it had the toggle visibility on too. Seems like I forgot that I enabled it. Thank you!