0 votes

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

Godot version the newest
in Engine by (12 points)

1 Answer

0 votes

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.

by (16,852 points)

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)


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 = KinematicBody2D.new()

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.

Thank you for your fast answer

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.