Drawn Lines not Showing

Godot Version

Godot Engine v4.2.2.stable.official.15073afe3

Question

I am trying to draw a grid on screen.

As best I can work out, I am drawing the lines, but nothing is shown on screen so I am clearly missing something. The coords seem sensible to me, but even if they werent I’d expect to see some lines on screen!

Code and result below. Any help would be appreciated!

## world.gd

func _draw() -> void:
	print("hit draw")
	if _show_grid:
		_draw_grid()

func _draw_grid() -> void:
	print("draw_grid called")
	var start: Vector2i = Vector2i.ZERO
	var end: Vector2i = Vector2i.ZERO
	var colour: Color = Color.PINK

	for x in range(0, grid_size.x + 1):
		start = Vector2i(x * _grid_scale, 0)
		end = Vector2i(x * _grid_scale, grid_size.y * _grid_scale)
		print("Draw horizontal (", start, end, ")")
		draw_line(start, end, colour, 2)

	for y in range(0, grid_size.y + 1):
		start = Vector2i(0, y * _grid_scale)
		end = Vector2i(grid_size.x * _grid_scale, y * _grid_scale)
		print("Draw vertical (", start, end, ")")
		draw_line(start, end, colour, 2)

Console output:

Toggle show grid. Now: true
hit draw
draw_grid called
Draw horizontal ((0, 0)(0, 24))
Draw horizontal ((4, 0)(4, 24))
Draw horizontal ((8, 0)(8, 24))
Draw horizontal ((12, 0)(12, 24))
Draw horizontal ((16, 0)(16, 24))
Draw horizontal ((20, 0)(20, 24))
Draw horizontal ((24, 0)(24, 24))
Draw horizontal ((28, 0)(28, 24))
Draw vertical ((0, 0)(28, 0))
Draw vertical ((0, 4)(28, 4))
Draw vertical ((0, 8)(28, 8))
Draw vertical ((0, 12)(28, 12))
Draw vertical ((0, 16)(28, 16))
Draw vertical ((0, 20)(28, 20))
Draw vertical ((0, 24)(28, 24))

Result:


Scene tree:

Cannot reproduce this! You didn’t share your values for _show_grid, grid_size and _grid_scale, but I set them to true, Vector2(600, 600) and 1.0 respectively, and it worked fine. Although it only shows a pink square, since you’re drawing a new line every pixel instead of (like in your console output) every 4. Also, in the screenshot of your scene tree, there seems to be no node with any script attached to it whatsoever?

1 Like

Thanks for taking a look. I think it’s something to do with the Z-index. When I’ve changed the size and scale I can see the grid (admittedly in the wrong coords!) poking out of the bottom. Assuming it’s an issue with draw order, what are the options? Googling suggests using a Line2D instead, so that I can set the Z value, is that the best way? Otherwise am I right in thinking I’d need to move the draw_line to a node lower in the tree?

Also, any idea why the grid is drawing in a different place to the rest of the game? Coords are starting at 0,0. :thinking:

Map size: (496, 384) | Grid Size: (15, 12)
Draw horizontal ((0, 0)(0, 384))
Draw horizontal ((32, 0)(32, 384))
Draw horizontal ((64, 0)(64, 384))
Draw horizontal ((96, 0)(96, 384))
Draw horizontal ((128, 0)(128, 384))
Draw horizontal ((160, 0)(160, 384))
Draw horizontal ((192, 0)(192, 384))
Draw horizontal ((224, 0)(224, 384))
Draw horizontal ((256, 0)(256, 384))
Draw horizontal ((288, 0)(288, 384))
Draw horizontal ((320, 0)(320, 384))
Draw horizontal ((352, 0)(352, 384))
Draw horizontal ((384, 0)(384, 384))
Draw horizontal ((416, 0)(416, 384))
Draw horizontal ((448, 0)(448, 384))
Draw horizontal ((480, 0)(480, 384))
Draw vertical ((0, 0)(480, 0))
Draw vertical ((0, 32)(480, 32))
Draw vertical ((0, 64)(480, 64))
Draw vertical ((0, 96)(480, 96))
Draw vertical ((0, 128)(480, 128))
Draw vertical ((0, 160)(480, 160))
Draw vertical ((0, 192)(480, 192))
Draw vertical ((0, 224)(480, 224))
Draw vertical ((0, 256)(480, 256))
Draw vertical ((0, 288)(480, 288))
Draw vertical ((0, 320)(480, 320))
Draw vertical ((0, 352)(480, 352))
Draw vertical ((0, 384)(480, 384))

That would be one way, yes.

That would be the other way, yes.

If the coords of your drawing start a (0, 0), then my best guess would be that your TileMap (or rather: the position of the top-leftmost tile) doesn’t?

1 Like

Thanks again for your help. I’ve tried with Line2D and thankfully can get it to show on top by amending the z_index.

World, the Node2D at the top most level, was set to a non-zero position. I set that to (0, 0), but needed to then move everything else so that its top left was at (0, 0), meaning their actual position was what World was set to originally. Does that make sense? Is that right?

position (unlike global_position) is relative to the position of the parent node. So if e.g. World was previously at position (100, 100) and you now moved it to (0, 0), then all of its child nodes should also move by that amount.

But judging from the screenshot you provided, it looks like you got it right now. :slight_smile:

1 Like

Thanks very much, buddy. :slightly_smiling_face: