Godot Version
4.2
Question
How to set global position on Line2d in Control nodes? (or get global_position on next TextureRect)
I am drawing a line that connects two TextureRects.
The script I wrote does this perfectly in the editor (using @tool). But in the game itself, the lines are restricted to the TextureRect container.
How to draw all the same lines from TextureRect to TextureRect or will I have to remake on Node2d?
- marker_out is used to define the start coordinates Line2d , marker_in - the end.
- I’m adding the next tiles
- Pampam… and it works in the editor
My Tool Code - GameMapTile
@tool
extends TextureRect
class_name GameMapTile
@onready var marker_out: Marker2D = $marker_OUT as Marker2D
@onready var marker_in: Marker2D = $marker_IN as Marker2D
@export var next_tiles : Array[GameMapTile]:
set(new_next_tiles):
next_tiles = new_next_tiles
if marker_out:
draw_connectors(next_tiles)
@export_subgroup("Line Connectors Settings")
@export var line_width : int = 2
@export var line_color : Color = Color("646365")
func draw_connectors(tiles_for_connecting:Array[GameMapTile] = next_tiles) -> void:
clear_lines()
if tiles_for_connecting.size() > 0:
for next_tile:GameMapTile in tiles_for_connecting:
create_line(marker_out.global_position,next_tile.marker_in.global_position)
func create_line(start_point : Vector2, end_point : Vector2) -> void:
var new_line : Line2D = Line2D.new() as Line2D
marker_out.add_child(new_line) #add to self (TextureRect) It doesn't work either.
new_line.clear_points()
new_line.add_point(new_line.to_local(start_point))
new_line.add_point(new_line.to_local(end_point))
new_line.default_color = line_color
new_line.width = line_width
new_line.begin_cap_mode = Line2D.LINE_CAP_ROUND
new_line.end_cap_mode = Line2D.LINE_CAP_ROUND
func clear_lines():
for line in marker_out.get_children():
line.queue_free()
UPD. I think it doesn’t work because the coordinates from next_tile.marker_in are the same as from self.marker_out