Godot Version
4.2.2
Question
How can I create an array of enum?
I have enum like:
enum Status {
OPEN,
LOCK,
USING
}
I tried to create an array like var status_arr = [Status.OPEN]
but I have an exception:
Invalid set index ‘status_arr’ (on base: ‘PackedScene’) with value of type ‘Array’.
Thanks!
I don’t know what are you doing to get an error “On base: PackedScene” but in my script it works fine:
[0, 1]
Maybe something else is causing it?
I also print like you. But when I assign for another class variable I got exception. For example:
class_name ClassA
enum Status {
OPEN,
LOCK,
USING
}
func _pass_param():
var status_arr = [Status.OPEN]
print(status_arr) # Printed
var class_b_new = ClassB.new()
class_b_new.status_pass = status_arr # Error occurred
In class B
extends TextureButton
class_name ClassB
var status_pass: Array;
func _ready():
print(status_pass)
Works fine for me:
And of course, as @AntiVirusJ said, you could add more elements to that array, even repeat them if you want (let’s say you want to keep the status of 8 fixed objects/chests/inventory slots/etc…). It’s not the best practice, in general you should define a variable for each object, like:
var status : Status = Status.OPEN;
but do it however makes sense to you.
1 Like
ClassA:
ClassB:
Result:
You definitely doing something more, maybe trying to set PackedScene to it from another script
1 Like
Sorry this is my fault, in class B I using camel case (statusPass) and class A I used underscore (status_pass)
Well, you’ve solved it, good luck)
1 Like
system
Closed
September 5, 2024, 9:55am
8
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.