[GDScript] Allow typed arrays to be declared via a variable containing a type? #9352
Replies: 2 comments
-
It works fine if you use: const type = preload(path_to_script) This is likely never going to work with a variable type, as it's compile time, now there is the limitation that it doesn't work with: const type = MyClass That I believe is a bug that might be reported somewhere Edit: Seems that part is tracked here:
But using a |
Beta Was this translation helpful? Give feedback.
-
The types in type hints must be known at compile time. If you want to create a typed array at runtime and the element type is not known in advance, then you can use the appropriate array constructor |
Beta Was this translation helpful? Give feedback.
-
I'm putting a simple database in my game, consisting of a dictionary of arrays. I'd prefer the arrays to be typed, and for accessors to the database to be able to create entries if they don't exist. While variables can contain types, these variables can't be used in array declarations. For instance,
var type = SomeClass
var array :Array[type]
doesn't work. What's curious is that this doesn't even throw an error in the parser- It only catches it at runtime, with the message "Identifier not found: Array".
One workaround is to have a new function for each type of array you'd like to create, but it'd be much more convenient to be able to declare arrays via a variable storing a type.
Edit: It seems that it does catch this in parser, but not if the variable containing type is a function parameter?
Beta Was this translation helpful? Give feedback.
All reactions