Exporting array of enum

Godot Version

v4.6.stable.mono.official [89cea1439]

Question

I have a scene with this script attached, so I can create and fill enum array through GUI. It appears in GUI, and I can properly fill it with objects.

    [ExportSubgroup("Settings")]
    [Export] private Godot.Collections.Array<EnemyType> _mobs;

    public override void _Ready()
    {
        GD.Print($"{_mobs.Count}");
    }

The issue is that this throws null reference exception (_mobs is null). Which is a lie, since array exists and has elements.

If I do this:

    [ExportSubgroup("Settings")]
    [Export] private Godot.Collections.Array<EnemyType> _mobs = new();

    public override void _Ready()
    {
        GD.Print($"{_mobs.Count}");
    }

There are no errors, but _mobs has 0 elements. new() overrides what was set in GUI and clears the array.

There is also this approach:

    [ExportSubgroup("Settings")]
    [Export] private EnemyType[] _mobs;

    public override void _Ready()
    {
        GD.Print($"{_mobs.Length}");
    }

But this doesn’t even compile, since enums are not supported export members.

I have tried everything, I’m out of ideas, beside going around it and breaking a bunch of coding principles in the process.

Okay, it does work, but the reason why it didn’t is because I had a brain fart. I was spawning an object through script (due to how my object pool works I couldn’t just drag and drop to the editor), and I did it like this:

Object obj = new();
AddChild(obj);

Which is of course not how you should do it, you need to instantiate packed scene. This is why things that have no right to happen were happening.

1 Like

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