• Godot Help
  • Duplicate a node via code in C# (or GDScript if necessary)

I am trying to duplicate a node that I've made via code. The idea is that frogs should appear on the screen at random intervals. I try this code but it doesn't draw the frog, it also doesn't give any errors and it does successfully change the new node's position even though it doesn't appear to exist.
Also I cannot create it as a child node as the parent node gets erased once it hits a certain point or gets destroyed via command.

var frog1 = new Frog();
Vector2 frog1Pos = new Vector2(900,100);
frog1.Position = frog1Pos;

Can anyone help me to do this properly?

Thanks!

Is Frog a node? Nodes have to be added to the scene tree to be effective.

  • Sabb replied to this.

    DaveTheCoder
    Frog was initially a node but now I've tried to solve this is by making the node into a scene and using this code to instance it:

    var frog1 = (PackedScene)ResourceLoader.Load("res://Scenes/Frog.tscn");
    However, it compiles but closes because "Specified cast is not valid." Even though the path is correct. Any ideas why? Is there another way to do this?

    Also thanks for replying so often to my many questions, it doesn't go unnoticed!! And FYI if you know any solution in GDScript I can probably translate it.

      Sabb making the node into a scene

      It's still a node, which happens to also be a scene.

      You said you can't make it a child because the parent gets erased. Can you make it a child of another node that doesn't get erased?

      • Sabb replied to this.