How to visualize Vector2ds?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jarlowrey
:warning: Old Version Published before Godot 3 was released.

I am attaching a script to a Position2 that exports a vector2 and would like to visualize this vector in the editor or in-game. In game I suppose I could manually draw a line, is there anything I can do to see it in the editor?

:bust_in_silhouette: Reply From: avencherus

It is the same process, but you will want to enable tool mode, and be careful how you write the tool code by checking the editor hint flag.

Here’s a basic example, you’ll notice it draws to the canvas item of the node only in the editor. I used continuous update in the event you want to change the vector and have it redrawn, you should change that to meet your conditions.

tool
extends Node2D

onready var in_editor = get_tree().is_editor_hint()

func _ready():
	
	if(in_editor):
		set_fixed_process(true)
		
func _fixed_process(delta):
	update()
	
var A = Vector2()
var B = Vector2(100, 100)
	
func _draw():
	
	if(in_editor):
		draw_line(A, B, Color(1,1,1), 3)

Does the entire script have to be a tool? My script has game logic in it and shouldn’t be executed all the time in the editor, but I’d still like my vectors to be visible. Am I overthinking this and it’s fine, or should I create a second script? It looks like your provided code/algorithm would be efficient, but IDK if there are performance ramifications to making a script a tool down the road.

Besides that, it worked great, thank you!

jarlowrey | 2017-09-19 02:51

Currently tool sets the whole script into tool mode. You have to carefully branch where code is editor only and for runtime. The only way to answer your concern is by profiling the code when it is done, to see if the extra if statements really matter.

If it is performance halting, creating different scripts for release is one solution.

avencherus | 2017-09-19 03:28

Good to know. Thank you!

jarlowrey | 2017-09-19 20:24