Custom object not available in _draw-function

Godot Version



In a Node2D I’d like to draw some lines they are stored in an object of my custom class:
A member-variable contains a PackedVector2Array for the polyline. From the script I can print the content, but as soon as I try to access it from the _draw()-function I get an Error:
Invalid get index 'line' (on base: 'NIL').

I found out: If I instantiate the object in the _ready()-function it is possible to draw as planned, but of course I need to create the object at runtime.
It is probably related to the tree-structure and the overwritten _draw()-function (of the parent?) but I don’t find the right way to address the object, can somebody help?

Much more I would like to draw directly out of the custom class, but I guess this is not possible?
Thanks a lot!

You need to show the code otherwise we have no idea where you have gone wrong. Somewhere you are calling thing.line but thing does not have a member named line at all.

Ok, here is the code:

The class-definition:

class_name ObjDraw
var line: PackedVector2Array
func _init():
	line = [Vector2(0, 0), Vector2(120, 0), Vector2(120, 400), Vector2(0, 400), Vector2(0, 0)]

The Main-code in Node2D:

extends Node2D

var Obj1 : ObjDraw   # Instance
var ObjExists        # helper for not existing instance

func _ready():
	var button1 =   # Button for creating the instance
	button1.set_position(Vector2(200, 0))
	button1.set_size(Vector2(100, 50))
	button1.text = "New Block"
	button1.connect("pressed", self._pressed_NewBlock)
	# using following line works but is not helping me:
	#Obj1 =

func _process(_delta): # Called every frame.

func _draw():
	if ObjExists:
		# here the error occurs:
		draw_polyline(Obj1.line, Color("WHITE"), 3.0)

func _pressed_NewBlock():
	var Obj1 =
	print (Obj1.line)
	ObjExists = true

OK, I just found my bug:
The keyword “var” was the problem: I created a local instance… :see_no_evil: