This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes
extends Control
class_name DrawThing

func _draw():
    var dest = Vector2(rect_position.x + 100, rect_position.y + 100)
    draw_line(rect_position, dest, Color.white, 5.0)
    pass

If I attach DrawThing to a Control in the scene, it'll draw a line. But if I add it via script:

func _ready():
    var draw1 = DrawThing.new()
    $NodeGrid/Control/Links.add_child(draw1)
    draw1.rect_position = Vector2()
    pass

It won't draw anything. What am I missing?
Having DrawThing extend Node2D solves the issue, but I really wanted to make it work with Control.

in Engine by (190 points)

Have you checked if $NodeGrid/Control/Link is returning what you want?

What do you mean returning?

type

print($NodeGrid/Control/Links)

to see what the engine returns. My guess was that it could be returning null.

But it looks you solved it, mark your own answer as the correct one, so other people know this question is solved.

_draw is a feature inherited from CanvasItem. Both Control and Node2D inherit CanvasItem, so it should have worked with extending a Control and using new... this is quite strange, maybe a bug even?

Oh, NodeGrid/Control/Links wasn't null for both cases.

1 Answer

0 votes
Best answer

I attached DrawThing to a scene and changed the code:

func _ready():
var draw1 = preload("res://src/draw_thing.tscn").instance()
$NodeGrid/Control/Links.add_child(draw1)
draw1.rect_position = Vector2()
pass

It works now. No idea why it won't work with new(), though.

EDIT: created https://github.com/godotengine/godot/issues/34119

by (190 points)
edited by
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.