clean syntax of something like "@export var myVar:Array[{int, PackedScene}]" ??

Hello,

I have:

@export var room_pool:Array[PackedScene] = [ preload("res://testRoom.tscn") ]
@export var weights:Array[int]= [1]

And you can probably guess it, it’s gonna be for a procuderal generation of some kind of dungeon. The rooms are scenes with tilemaps and stuff, the weights are gonna detemine how much a room is gonna be used in the procedural generation over another.

But I can’t make the weights array automatically have the same weight as the other one, and I can’t give it default values. I’d love something like

@export var room_pool:Array[ ??? ] = [ {id: 1, weight: 2, scene: preload(…) }, {…}, {…} ]

and have this still be adjustable with drag and drop in the scene. Is this possible? I experimented a bit with resources, but I haven’t figured out yet how it works in Godot. Is there a cleaner way? How would you guys do it?

Can’t you just use a dictionary?

dictionaries don’t work as I would like them to. After all I’m exporting this variable and accessing from the scene, and they don’t even let me select packed scene.

I believe resources allow you to what you’re looking for, if I understood.

Create a new script, and in script extend from Resource. Put in some @exports there, like @export var myScene: PackedScene.

Then, in the editor, you can create a new resource, and in the list you can find this new resource you created. You can also, in a scene, @export var myResourceArray: Array[MyResource] and in the inspector, you can then add elements to this array and also edit the properties of the elements you added.

See this tutorial from godotneers for the full layout: https://youtu.be/4vAkTHeoORk?t=851

Thanks, I got it to work with resources. They need a few extra clicks in the scene inspector though, so now I gotta figure out which implementation I prefer…

In 4.4.1 you can use a typed dictionary

@export var room_pool: Dictionary[int, PackedScene] = {1: preload("res://testRoom.tscn")}

And an array of that dictionary? Throws “nested typed collections are not supported”, unfortunately.

You can use Array[Dictionary], which is untyped and the best you’ll get cause you can’t create a typed dictionary with two types (int and PackedScene) for the values.

If you really want an ID, weight, and scene, you may need to create a class_name’d resource instead.

extends Resource
class_name RoomData

@export var id: int
@export var weight: int
@export var scene: PackedScene

# in pooling.gd
@export var room_pool: Array[RoomData]
1 Like

Well, ok. I wish there were more options, but resources are fine, gonna go with it for the moment. Thanks everyone for the help.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.