Add a Kinematic Body via Code

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

I want to add a Kinematic Body, which I have already created, several times via code. Is that possible?

:bust_in_silhouette: Reply From: jgodfrey

Yeah, definitely possible. If you’ve already created the KinematicBody, just add it to the scene, at the appropriate location by calling add_child(your_kinematic_body) on the node you want to be the parent.

Note though, you can’t add the same KB to multiple places in the tree. You’ll need to create a unique KB for each one that you want to place in the scene.

Thank you for this fast answer, but i didn’t understand it yet. In the picture you see my nodes and i want to add the “met” again via code.(Please ignore the pathMeteroit)

godot.PNG - Google Drive

Basti | 2020-12-21 17:46

As I mentioned originally, you can’t add the exact same instance of a node to the scene tree multiple times. If you need multiple KB2D nodes in the scene, you’ll need to create each of them as a separate instance.

That said, you can do that via code.

You could create a separate scene that contains the KB2D itself (and anything else you want to part of the scene). Then, you could create instances of that by preload()ing the scene, creating copies as necessary via the instance() function, and then adding them to the scene via add_child().

Or, you could create new KB2D’s from scratch in code via:

var new_kb =

Then, you can set whatever properties you want on the new kb, and then finally add that to the scene via add_child().

Either way will work. Using the first method, you’re creating instances of a scene that you created ahead of time. Using the second method, you’re creating a brand new KB2D from scratch and setting it up completely in code.

jgodfrey | 2020-12-21 18:03

Thank you for your fast answer

Basti | 2020-12-22 09:59