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.
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.
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.
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?
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.