I'm trying to instantiate a node2d in a scene using this code:
var scene = GD.Load<PackedScene>(file).Instantiate();
(the file is correctly pointing to a scene whose root is a node2d)
but I keep getting this error in this line of code:
ERROR: Script inherits from native type 'Node2D', so it can't be assigned to an object of type: 'Node'.
Help with Instantiate a Node2D
- Edited
also, that is the entire function that I'm using:
private static Room InstantiateRoom(Node root, Chunk chunk)
{
const int roomName = 0;
if (chunk.Name == "0") return null;
var file = chunk.SpecialChunk
? $"{RoomsPath}{chunk.Name}/{chunk.Binary}/{roomName}.tscn"
: $"{RoomsPath}{chunk.Name}_{chunk.Binary}/{roomName}.tscn";
if (FileAccess.FileExists(file))
{
var scene = GD.Load<PackedScene>(file).Instantiate();
GD.Print(scene.GetType() + " " + root.GetType());
root.AddChild(scene);
return null;
}
GD.PushWarning($"File {file} not Find!");
return null;
}
and the Logs:
WARNING: File res://assets/resources/rooms/A/0010/0.tscn not Find!
at: LittleDragonAlpha.scripts.map.Room LittleDragonAlpha.scripts.map.ChunkLoader.InstantiateRoom(Godot.Node, LittleDragonAlpha.scripts.map.grid.Chunk) (res://scripts/map/ChunkLoader.cs:37)
ERROR: Script inherits from native type 'Node2D', so it can't be assigned to an object of type: 'Node'.
at: instance_create (modules/mono/csharp_script.cpp:2420)
Godot.Node2D Godot.Node2D
ERROR: Script inherits from native type 'Node2D', so it can't be assigned to an object of type: 'Node'.
at: instance_create (modules/mono/csharp_script.cpp:2420)
Godot.Node2D Godot.Node2D
ERROR: Script inherits from native type 'Node2D', so it can't be assigned to an object of type: 'Node'.
at: instance_create (modules/mono/csharp_script.cpp:2420)
Godot.Node2D Godot.Node2D
ERROR: Script inherits from native type 'Node2D', so it can't be assigned to an object of type: 'Node'.
at: instance_create (modules/mono/csharp_script.cpp:2420)
Godot.Node2D Godot.Node2D
ERROR: Script inherits from native type 'Node2D', so it can't be assigned to an object of type: 'Node'.
at: instance_create (modules/mono/csharp_script.cpp:2420)
Godot.Node2D Godot.Node2D
ERROR: Script inherits from native type 'Node2D', so it can't be assigned to an object of type: 'Node'.
at: instance_create (modules/mono/csharp_script.cpp:2420)
Godot.Node2D Godot.Node2D
WARNING: File res://assets/resources/rooms/B/0101/0.tscn not Find!
at: LittleDragonAlpha.scripts.map.Room LittleDragonAlpha.scripts.map.ChunkLoader.InstantiateRoom(Godot.Node, LittleDragonAlpha.scripts.map.grid.Chunk) (res://scripts/map/ChunkLoader.cs:37)
WARNING: File res://assets/resources/rooms/D/0100/0.tscn not Find!
at: LittleDragonAlpha.scripts.map.Room LittleDragonAlpha.scripts.map.ChunkLoader.InstantiateRoom(Godot.Node, LittleDragonAlpha.scripts.map.grid.Chunk) (res://scripts/map/ChunkLoader.cs:37)
WARNING: File res://assets/resources/rooms/C/0100/0.tscn not Find!
at: LittleDragonAlpha.scripts.map.Room LittleDragonAlpha.scripts.map.ChunkLoader.InstantiateRoom(Godot.Node, LittleDragonAlpha.scripts.map.grid.Chunk) (res://scripts/map/ChunkLoader.cs:37)
The nodes that I instantiate appears on the scene, but the erros persists, I don't know why
Probably I'm just instancing wrong, I'm new on Godot.
I need to solve this erros because I want to get a node that is a child of each room I am instantiating.
- Edited
ayslangamedev As the error says, your script class inherits Node2D
but is attached to a node of type Node
. You can't do that. The script type cannot be downstream of node type in the class hierarchy.
- Edited
xyz Sorry, but I don't understand, where is a node attached?
Could it be in the function parameters? I've already changed the parameters of all the functions from Node to Node2D and it's still giving me the same error, the value being passed by the 'root' parameter is of type Node2D and the root of the file that is read is also a Node2D
Here are the changes I had already made before:
using System.Collections.Generic;
using System.Linq;
using Godot;
using LittleDragonAlpha.scripts.map.grid;
namespace LittleDragonAlpha.scripts.map;
public static class ChunkLoader
{
private const string RoomsPath = "res://assets/resources/rooms/";
public static List<Room> InstantiateGrid(Node2D root, Grid grid)
{
return (from Chunk chunk in grid.Chunks
select InstantiateRoom(root, chunk)
into room
where room != null
select room).ToList();
}
private static Room InstantiateRoom(Node2D root, Chunk chunk)
{
const int roomName = 0;
if (chunk.Name == "0") return null;
var file = chunk.SpecialChunk
? $"{RoomsPath}{chunk.Name}/{chunk.Binary}/{roomName}.tscn"
: $"{RoomsPath}{chunk.Name}_{chunk.Binary}/{roomName}.tscn";
if (FileAccess.FileExists(file))
{
var scene = GD.Load<PackedScene>(file).Instantiate();
GD.Print(scene.GetType() + " " + root.GetType());
root.AddChild(scene);
return null;
}
GD.PushWarning($"File {file} not Find!");
return null;
}
}
The logs were the same, here they are again, for sanity check(this time running using Node2D as parameter):
WARNING: File res://assets/resources/rooms/A/0010/0.tscn not Find!
at: LittleDragonAlpha.scripts.map.Room LittleDragonAlpha.scripts.map.ChunkLoader.InstantiateRoom(Godot.Node2D, LittleDragonAlpha.scripts.map.grid.Chunk) (res://scripts/map/ChunkLoader.cs:37)
ERROR: Script inherits from native type 'Node2D', so it can't be assigned to an object of type: 'Node'.
at: instance_create (modules/mono/csharp_script.cpp:2420)
Godot.Node2D Godot.Node2D
ERROR: Script inherits from native type 'Node2D', so it can't be assigned to an object of type: 'Node'.
at: instance_create (modules/mono/csharp_script.cpp:2420)
Godot.Node2D Godot.Node2D
ERROR: Script inherits from native type 'Node2D', so it can't be assigned to an object of type: 'Node'.
at: instance_create (modules/mono/csharp_script.cpp:2420)
Godot.Node2D Godot.Node2D
ERROR: Script inherits from native type 'Node2D', so it can't be assigned to an object of type: 'Node'.
at: instance_create (modules/mono/csharp_script.cpp:2420)
Godot.Node2D Godot.Node2D
ERROR: Script inherits from native type 'Node2D', so it can't be assigned to an object of type: 'Node'.
at: instance_create (modules/mono/csharp_script.cpp:2420)
Godot.Node2D Godot.Node2D
ERROR: Script inherits from native type 'Node2D', so it can't be assigned to an object of type: 'Node'.
at: instance_create (modules/mono/csharp_script.cpp:2420)
Godot.Node2D Godot.Node2D
WARNING: File res://assets/resources/rooms/B/0101/0.tscn not Find!
at: LittleDragonAlpha.scripts.map.Room LittleDragonAlpha.scripts.map.ChunkLoader.InstantiateRoom(Godot.Node2D, LittleDragonAlpha.scripts.map.grid.Chunk) (res://scripts/map/ChunkLoader.cs:37)
WARNING: File res://assets/resources/rooms/D/0100/0.tscn not Find!
at: LittleDragonAlpha.scripts.map.Room LittleDragonAlpha.scripts.map.ChunkLoader.InstantiateRoom(Godot.Node2D, LittleDragonAlpha.scripts.map.grid.Chunk) (res://scripts/map/ChunkLoader.cs:37)
WARNING: File res://assets/resources/rooms/C/0100/0.tscn not Find!
at: LittleDragonAlpha.scripts.map.Room LittleDragonAlpha.scripts.map.ChunkLoader.InstantiateRoom(Godot.Node2D, LittleDragonAlpha.scripts.map.grid.Chunk) (res://scripts/map/ChunkLoader.cs:37)
note that I wrote a line GD.Print(scene.GetType() + " " + root.GetType());
just to demonstrate what root and scene are, and the logs are always Godot.Node2D Godot.Node2D
, demonstrating that both are Node2D
- Edited
- Best Answerset by ayslangamedev
ayslangamedev Some Node
somewhere in your project has a script attached to it that inherits Node2D
. It may not even be related to the piece of code you posted.
- Edited
xyz okay, I discovered that my Room class was inheriting from Node2D even though I had made a change so that the script was in a Node... DAMN the logs could have warned me about that, OMG.
anyway, thanks for the help.