I’ve read here and here about how to initialize typed arrays.
However, i’m struggling to do this for instances of a specific class, like
extends Node
class_name Pics
class Pic:
var name : String
static func create(name : String) -> Pic:
var instance = Pic.new()
instance.name = name
return instance
@export var pics : Array[Pics.Pic] = [ Pic.create("asdf"), ] # Error: Trying to assign an array of type "Array" to a variable of type "Array[Pic]".
Using type casting seemed to eliminate the error, at least for me. Instead of @export var pics : Array[Pics.Pic] = [ Pic.create("asdf"), ], try @export var pics := [ Pic.create("asdf"), ] as Array[Pics.Pic].
See GDScript reference - Casting and Static typing in GDScript - Type casting.
Export type can only be built-in, a resource, a node, or an enum.
This is because Pic currently is a class, but not one that inherits from Resource. You can fix this by making the Pic class extend Resource or another class that inherits from it.
extends Node
class_name Pics
class Pic:
extends Resource
var name : String
static func create(name : String) -> Pic:
var instance = Pic.new()
instance.name = name
return instance
@export var pics := [ Pic.create("asdf"), ] as Array[Pics.Pic]