0 votes

Hi guys and gals.

I need to draw debug line from point to point with ImmediateGeometry in Editor with tool script... and I just can't do it. I got part of the code from one of the examples. Tool writing logic is really weird for me though. Easy examples I can find on the internet on writing tool scripts make sense but it doesn't help with this problem. So the code I have now:

tool
extends ImmediateGeometry


var m = SpatialMaterial.new()

export(NodePath) var begin_path setget set_begin_node
export(NodePath) var end_path setget set_end_node

var begin_pos
var end_pos

func set_begin_node(path):
    if Engine.is_editor_hint():
        begin_node = get_node(path)
        begin_pos = begin_node.get_transfom().origin


func set_end_node(path):
    if Engine.is_editor_hint():
        end_node = get_node(path)
        end_pos = end_node.get_transform().origin

func draw_line():
    m.flags_unshaded = true
    m.flags_use_point_size = true
    m.albedo_color = Color(1.0, 1.0, 1.0, 1.0)
    set_material_override(m)
    clear()
    begin(Mesh.PRIMITIVE_POINTS, null)
    add_vertex(begin_pos)
    add_vertex(end_pos)
    end()
    begin(Mesh.PRIMITIVE_LINE_STRIP, null)
    end()


func process(delta):
    if Engine.is_editor_hint():
        draw_line()

Any idea how to make it work? Thanks.

asked 6 days ago in Engine by mronetwo (26 points)
edited 6 days ago by mronetwo

2 Answers

0 votes
Best answer

Ok I got it working. There is room for improvement but at least it works. It really needs a refactoring. Even variable names are horrible because I didn't really understood what I was doing at the time. I will improve on this script and update it. For now it's a starting point.

tool
extends ImmediateGeometry


var begin = Vector3()
var end = Vector3()
var begin_path
var end_path
var m = SpatialMaterial.new()

onready var start_node = Spatial.new()
onready var end_node = Spatial.new()

var path = []
var draw_path = true

export(NodePath) var draw_start setget set_draw_start, get_draw_start
export(NodePath) var draw_end setget set_draw_end, get_draw_end


func set_draw_start(val):
    if Engine.is_editor_hint():
        begin_path = val
        start_node = get_node(val)
        draw_path_tool( \
            start_node.get_transform().origin - start_node.get_transform().origin, \
            end_node.get_transform().origin - start_node.get_transform().origin)
    else:
        pass


func set_draw_end(val):
    if Engine.is_editor_hint():
        end_path = val
        end_node = get_node(val)
        draw_path_tool( \
            start_node.get_transform().origin - start_node.get_transform().origin, \
            end_node.get_transform().origin - start_node.get_transform().origin)
    else:
        pass


func get_draw_start():
    return begin_path


func get_draw_end():
    return end_path


func _ready():
    if Engine.is_editor_hint():
        draw_path_tool(start_node.get_transform().origin, end_node.get_transform().origin)
        set_process(true)
        m.flags_unshaded = true
        m.flags_use_point_size = true
        m.albedo_color = Color(1.0, 0.0, 0.0, 1.0)

        if get_tree().get_root().has_node("start"):
            print('ok')
        else:
            print('nok')
        if get_tree().get_root().has_node("end"):
            print('ok')
        else:
            print('nok')

func _process(delta):
    if Engine.is_editor_hint():
        draw_path_tool(start_node.get_transform().origin, end_node.get_transform().origin)






func draw_path_tool(_start_pos, _end_pos):
    var p = Array()
    p.append(_start_pos)
    p.append(_end_pos)
    path.invert()

    if (draw_path):
        set_material_override(m)
        clear()
        begin(Mesh.PRIMITIVE_POINTS, null)
        add_vertex(_start_pos)
        add_vertex(_end_pos)
        end()
        begin(Mesh.PRIMITIVE_LINE_STRIP, null)
        for x in p:
            add_vertex(x)
        end()
answered 2 days ago by mronetwo (26 points)
0 votes

Try changing the first begin(Mesh.PRIMITIVE_POINTS, null) call to

begin(Mesh.PRIMITIVE_LINES)

and remove begin(Mesh.PRIMITIVE_LINE_STRIP, null) and the matching end().

answered 6 days ago by mollusca (1,322 points)

I didn't receive a mail that you anwsered :< Thank you for your help. I used NavMesh example and just deleted code until I had what I wanted and then expanded (and debugged when I broke it ;)). I will post the code as an answer in a sec.

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 How to use this Q&A? before posting your first questions.