Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | ImNotDenisLeary | |
Old Version | Published before Godot 3 was released. |
When creating a instance of a object in GDscript, its pretty straight forward:
var newInstanceObject = SomeObject.instance()
add_child(newInstanceObject)
How do I do this with C++ in a module?
Basically what I want to do in the module, is to take a object and make a new instance of it.
Have you tried actually using .instance
in C++ on a PackedScene?
Bojidar Marinov | 2016-03-10 18:07
Is that really the correct way?
I don’t really see a connection between a object and that class…
ImNotDenisLeary | 2016-03-11 17:12
Um… what is the type of SomeObject? Also, maybe you can use Object::call
?
Bojidar Marinov | 2016-03-11 17:55
In my current case, the Object is a Rigidbody2D object.
If I would take a scene (with my Rigidbody2d object within) and instance that one, then the PackedScene class would make more sense. But I can’t see any way to actual access the class, unless you can type cast your Object * to a PackedScene object.
ImNotDenisLeary | 2016-03-14 09:55
Then, you should go with add_child(memnew(RigidBody2D))
Bojidar Marinov | 2016-03-14 10:06
Yes, I have looked at that one, but the memnew only accepts a class, and not a object.
I want to make a copy/new instance of the Object (in this case; rigidbody2D), including child objects, script(s) etc.
I could go about and use the scene object (*.scn) instead, and make a instance of that, if that is possible.
ImNotDenisLeary | 2016-03-14 10:13
And, you haven’t tried the duplicate
method yet?
Bojidar Marinov | 2016-03-14 10:22
No, I haven’t tried that one. I will try it out when I have the time. Thanks for the tips!
ImNotDenisLeary | 2016-03-14 10:25