Array stays empty when populated through Inspector

Godot Version

Godot v4.2.2

Question

Hi, I’ve recently started developing game and trying to finally make a progression
I want to add an array that will hold scenes of different enemies so I can spawn them, and for convenience I want to do it through Inspector, so I made this line

@export var enemy_preload : Array[PackedScene] = []

But when I’m trying to use this array, it says that index is out of bounds and it’s actually empty.
I really don’t want to add scenes paths manually in code (though it works), what can be a problem?
Thanks in advance

How are you populating this array? You can drag/drop into the Inspector or via code. Just creating an @export variable inits an array with no members.

Are you sure that the array you’re editing in the inspector is definitely the same array that you are trying to access in code? You might be getting this error if you actually have more than one node or resource of this type, and you are editing one in the inspector but trying to access the array in a different one in code. Of course, each instance of a node or resource will have its own array independent from the others. If you wanted multiple instances to share a same array, you could do that by having them all refer to a common resource, for example.

That worked, thanks!
I have a separate spawner scene , and I added enemy scenes to it, but spawners in main game node didn’t update for some reason, I’ve just clicked on them couple times, saved, did some more clicks in their inspector and now it works great. Still don’t understand why that didn’t work yesterday, but at last. Thanks again

Of course, I’ve used @export just for the reason that I wouldn’t need to manually type paths, just dropping scene into inspector.
Maybe another small question to you, if I change the location of scene I added, will editor automatically update path, or I need to drag/drop scenes again?

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