How to set 'node_a' and 'node_b' properties on a physics joint 2D using a script?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By TBUk

Hello! I am trying to dynamically create a physics joint between two rigidbodies using a script, but I cannot figure out how to set the node_a and node_b properties, as every time I do it returns Invalid set index 'node_a' (on base: 'DampedSprigJoint2D') with value of type 'RigidBody2D ()'.

Here is my code:

var constraint =
constraint.position = Vector2.ZERO
constraint.node_a = self
constraint.node_b = body
constraint.length = 10

How could I set these properties? Hovering over the property in the inspector shows node_a and node_b as the property names.

:bust_in_silhouette: Reply From: Zylann

The properties exist: Joint2D — Godot Engine (stable) documentation in English
They expect NodePath, not the nodes themselves.

The problem is actually that you are trying to set them with variables of the wrong type. Try this instead:

constraint.node_a = self.get_path()
constraint.node_b = body.get_path()