How do I properly attach a node_b to a PinJoint2D during runtime?

Godot Version

4.7.2 Stable

Question

I’m wondering how to properly attach another physics body to a pin joint 2D during runtime. When I try to do it like how I have below, the node_b (the player in this case) doesn’t seem to be affected by any physics that the PinJoint should exert. There’s also weird behavior when I try to print out the node_b, as it prints out empty lines. I can’t see it in the output, but apparently there are messages that fill it up as indicated by the exclamation mark. Any help is greatly appreciated, thank you.

This is what the output looks like.

@onready var player : Player

func _ready() -> void:
	player = get_parent()

func _process(_delta: float) -> void:
	if is_colliding():
		var collider = get_collider()
		if collider is hookable_object:
			var pin_joint : PinJoint2D = collider.find_child("PinJoint") 
			if pin_joint:
				pin_joint.set("nodes/node_b", player.get_path()) #sets node b
				print(pin_joint.node_b)

Joint2D does not have a nodes/node_b setter so that won’t work. Assign it directly to the Joint2D.node_b property and use Node.get_path_to() instead as it needs to be relative to the Joint2D node: pin_joint.node_b = pin_joint.get_path_to(player)

Sorry, it still doesn’t work. I used the code you suggested

Give more information then. Is your Player a CharacterBody2D? It won’t work in that case as a CharacterBody2D is fully controlled by code.

It may work

Use joint_make_pin

Ah, I didn’t know that. It is a CharacterBody2D. I switched it out (in certain cases) for a RigidBody2D. Thanks for the info