Create instance of object in Module

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ImNotDenisLeary
:warning: 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

:bust_in_silhouette: Reply From: Akien

Best way to find that out would be to check the code of the editor tools, which are in the end just an embedded Godot module. See e.g.: https://github.com/godotengine/godot/blob/master/tools/editor/plugins/script_editor_plugin.cpp#L2353-L2354

file_menu = memnew( MenuButton );
menu_hb->add_child(file_menu);

Also check the documentations on the core classes: http://docs.godotengine.org/en/latest/reference/_developing.html
Especially: Object class — Godot Engine (latest) documentation in English

Yes, I have tried that, but using memnew you only creates a new class instance.
When using:

var newInstanceObject = SomeObject.instance()

you create a new instance of a object, with all child nodes, scripts and whatnots.

Would I have to first create the parent node, and then add all children, scripts and set variables manually?

My plan is to create (or spawn) instances of a object for a puzzle game.

ImNotDenisLeary | 2016-03-09 11:16

Well I don’t have much experience with C++ modules myself, but I believe it would work the same in GDScript and in C++. When you create a new instance of your module object, it will call its constructor and thus instance child nodes in any.

Akien | 2016-03-09 11:55

I believe so too, but I haven’t found any methods, which does the same in C++ as in GDScript.

I will have to search through the source code as see if I find something, when I have some time over.

ImNotDenisLeary | 2016-03-10 09:22