Creating a new curve through code?

Godot Version

v4.2.2

Question

So im trying to create a new curve and add points to it in a script attached to a root node which is the parent of a Path2D node.

I have defined the Path2D Node as var “path_2d” via @onready.
Now “curve” is a property of Path2D and gdscript recognizes this and even suggests “curve” when typing “path2d.”, however when im trying to do anything with path_2d.curve, I get “Invalid get index ‘curve’(on base ‘Nil’)” as if Path2D doesnt exist. I tried “path_2d.curve = Curve2D.new()” before doing anything else and that part doesnt throw an error when I run it, however the error that occurs once im trying to call the function “add_point()” on “path_2d.curve” remains.
Im using Path2D for the first time but I felt pretty confident about manipulating properties via script. If I want to toggle visibility on a Sprite2D for example I can just do that via “Sprite2D.visible = false/true” and its done. I would appreciate any help or insights on what could cause this error. This is my first project outside of guides so please be gentle :slight_smile: I will post my code below for you to look at. If you have any questions about the tree of the scene let me know. Thanks in advance!

extends Node2D

@onready var path_2d = $Path2D

Called when the node enters the scene tree for the first time.

func _ready():
path_2d.curve = Curve2D.new()

func start(startpos, endpos):

path_2d.curve.add_point(startpos, Vector2(0,0), Vector2(0,0))
path_2d.curve.add_point(endpos, Vector2(0,0), Vector2(0,0))

Just in case anyone stumbles upon this post:

I have moved on to a different solution for my use case but I learned that “Path2D.curve” can be called without a problem if you call it as “curve” in script that is attached to the Path2D Node directly.