Search code examples
c#godot4

Godot 4.3 C# - Instantiate() not returning correct class


I am new to Godot (using v4.3) so I decided to follow a bunch of youtube tutorials on how to generate a voxel landscape. This works and was made in gdscript.

I am now trying to get it to work in C#, which seems to be working when I build and run the project, but not when running it from the editor. I have an exported bool that I can check and when it is checked it calls the GenerateChunk method in a loop.

The problem is the following:

I have a scene called WorldGenerator.tscn with the root node a Node3D with a WorldGenerator.cs script attached. This script instantiates a Chunk.tscn and adds as a child to a specific node. The Chunk.tscn scene has a Node3D as root with the Chunk.cs script attached.

In the WorldGenerator script I have the following method:

private void GenerateChunk(Vector3I position)
{
    GD.Print("=====");
    chunkPrefab = GD.Load<PackedScene>("res://scenes/WorldGeneration/ChunkCs.tscn");
    GD.Print(chunkPrefab.GetType());

    var scene = chunkPrefab.Instantiate();
    GetNode("Chunks").AddChild(scene);

    GD.Print(scene.GetType());

    var scene2 = chunkPrefab.Instantiate<Chunk>();
    GetNode("Chunks").AddChild(scene);

    GD.Print(scene2.GetType());
    GD.Print("=====");
    // chunk.Initialize(position, chunkSize);
}

For each chunk that I make, I need to call the custom Initialize() method in the Chunk.cs script, but the method is not known.

The output of the GenerateChunk() method is this:

=====
Godot.PackedScene
Godot.Node3D
  /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/ExceptionUtils.cs:113 - System.InvalidCastException: Unable to cast object of type 'Godot.Node3D' to type 'WorldGeneration.Chunk'.
(mode error stacktrace)...

This means it can instantiate the scene, but only as a Node3D and not a the Chunk. Again, when I run the project it works, but not when it is triggered in the editor.

I don't understand why it is not working as I have gdscript code with the same setup that does work in the editor. I suspect it has something to do with not everything loaded when it is triggered from the editor when using C# (as that is the only difference I can think of), but how can I make this work in the editor as well?

I tried loading the Chunk.cs manually, but that didn't change anything. What can I try next?

Update: The Chunk.cs class

using Godot;

namespace WorldGeneration
{
    public partial class Chunk : Node3D
    {
        // Called when the node enters the scene tree for the first time.
        public override void _Ready()
        {
        }

        // Called every frame. 'delta' is the elapsed time since the previous frame.
        public override void _Process(double delta)
        {
            
        }

        public void Initialize(Vector3 position, int chunkSize)
        {
            GD.Print(position, chunkSize);
        }
    }
}

Solution

  • So apparently all I had to do was add [Tool] to the Chunk class.