0 votes

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?

in Engine by (693 points)

1 Answer

+3 votes
Best answer

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)
by (5,274 points)
selected by

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!

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.

Good to know. Thank you!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.