Godot Version
v4.2.2
Question
I’m trying to instantiate a class that I’m importing through [Export], however, when I call New() on the CSharpScript object, I get the following error:
E 0:00:00:0666 GD.cs:366 @ void Godot.GD.PushError(string): Invalid call. Nonexistent function 'new' in base 'Godot.CSharpScript'.
It seems like the error is saying that CSharpScript doesn’t contain a method called new(), but the documentation here lists “new” as one of the methods available for the class.
My call is in the following code
[Export]
Array<CSharpScript> Moveset;
...
public override void _Ready()
{
if(Moveset.Count > 0)
{
BattleAction action = (BattleAction) Moveset[0].New().Obj;
Debug.WriteLine(Moveset);
}
}
Any help is greatly appreciated.
It’s odd that the error you’re getting puts 'new'
in lowercase when you call the New()
method - that might be part of the binding translation layer?
Just to check, what does the constructor for BattleAction
look like? Does it require any arguments?
Hello,
It’s a bit hard to understand exactly what you’re trying to achieve, but here’s how I would approach it. I’ve never instantiated anything using ‘New().Obj’, and I’m not even sure if it’s intended to be done that way. Here’s how I’d do it:
public override void _Ready()
{
if(Moveset != null && Moveset.Count > 0)
{
BattleAction action = new()
{
Moveset = Moveset[0],
}
GD.Print(Moveset);
}
}
Also, you need to type your field as a derived class instead of a base class, like this:
[Export] public Array<Moveset> Moveset { get; set; }
Hello everyone,
I’ve found the main reason behind my issue. When importing a C# script through the Editor using [Export], the imported class must extend the Godot.Resource class.
If importing this script through an Array, I’ve personally had issues, but otherwise this is the solution I’ve found.
Thanks to everyone who replied, and I’m sorry for not being more clear in my initial post.