add PinJoint2D to two existing RigidBody2d via GDScript

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jospic
:warning: Old Version Published before Godot 3 was released.

Hi,
how to make a PinJoint2D between two existing RigidBody2D from script?
Thanks in advance.
-jospic

This is a Q&A site, not a forum. You should not add “Resolved” to the title, but instead, select/accept an answer.

Bojidar Marinov | 2016-03-08 10:01

I’m sorry , I correct immediately…

jospic | 2016-03-08 10:53

:bust_in_silhouette: Reply From: jospic

I may have found, the code should be this :

	var joint = PinJoint2D.new()
	joint.set_node_a(get_path())    
	joint.set_node_b()    #this is a problem with instance object

The problem comes with the object instances . The argument of " set_node_a " and " set_node_b " functions takes the path to the object.
But how to do when the object is created at runtime (instance()) ?
Thanks.
-jospic

:bust_in_silhouette: Reply From: jospic

Resolved: before create joint must be to set a name for object instance

    for i in range(NUM_PARTS): 
	
	  var p = part.instance()
	
	  p.set_name("Part"+str(i))
	
	  var pospartX = get_pos().x - 24*(i+1)
	  var pospartY = get_pos().y
	
	  p.set_pos(Vector2(pospartX, pospartY))
	
	  add_child(p)

    for i in range(2,NUM_PARTS):
	
	  var j = PinJoint2D.new()
	  j.set_node_a("Part"+str(i))
	  j.set_node_b("Part"+str(i-1))

Hello, I am trying to do a rope and I have a similar problem.
In which function did you run that code? the _ready? or _init?

Nodragem | 2021-02-24 23:42

In _ready function
Regards
-j

jospic | 2021-02-25 08:03