Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C#: Array.Contains() doesn't work properly with enums #98595

Closed
InkONat opened this issue Oct 27, 2024 · 11 comments · Fixed by #98545
Closed

C#: Array.Contains() doesn't work properly with enums #98595

InkONat opened this issue Oct 27, 2024 · 11 comments · Fixed by #98545

Comments

@InkONat
Copy link

InkONat commented Oct 27, 2024

Tested versions

4.4.dev3

System information

Windows 11 - Godot 4.4.dev3

Issue description

I have an enum called ItemType, belonging to a custom resource called ItemManager:

image

When I try to check if an Array<ItemType> contains STATS from this enum, unexpected behavior happens:

image

image
image

Steps to reproduce

  • Make a new C# project
  • Make a new custom resource on C# with a public enum
  • Make a typed Array with that enum, containing one of it's elements
  • Check if that element is inside the array

Minimal reproduction project (MRP)

N/A

@a-johnston
Copy link
Contributor

I couldn't reproduce this on 61accf0, potentially it's been fixed between dev3 and that commit.

For that commit, cs_array_contains.zip has the output:

Enum Array: [0, 1, 2]
  Contains MyEnum.A (0): True
  Contains MyEnum.B (1): True
  Contains MyEnum.C (2): True
Int Array: [0, 1, 2]
  Contains MyEnum.A (0): True
  Contains MyEnum.B (1): True
  Contains MyEnum.C (2): True

@InkONat
Copy link
Author

InkONat commented Oct 28, 2024

I couldn't reproduce this on 61accf0, potentially it's been fixed between dev3 and that commit.

For that commit, cs_array_contains.zip has the output:

Enum Array: [0, 1, 2]
  Contains MyEnum.A (0): True
  Contains MyEnum.B (1): True
  Contains MyEnum.C (2): True
Int Array: [0, 1, 2]
  Contains MyEnum.A (0): True
  Contains MyEnum.B (1): True
  Contains MyEnum.C (2): True

Were you able to reproduce it on normap 4.4 dev3 though?

@a-johnston
Copy link
Contributor

I could not reproduce it on 4.4 dev3 either; the output is the same as the current head. If you're certain this is an engine bug and not a project bug, can you compare your code to the zipped project I linked and make an MRP?

@raulsntos
Copy link
Member

Sounds like a duplicate of #98309. Make sure the array property is typed when serialized to the scene, it should look like: Array[int]([0, 1, 2])

@a-johnston
Copy link
Contributor

a-johnston commented Oct 28, 2024

Ah interesting, thanks for linking that. Yeah that seems to trigger specifically for int arrays and the value 0 as when I change the exports (and the int array print, whoops!) in the project above to

[Export] Array<MyEnum> enumArray = new();
[Export] Array<int> intArray = new();

the output changes to

Enum Array: [0, 1, 2]
  Contains MyEnum.A (0): True
  Contains MyEnum.B (1): True
  Contains MyEnum.C (2): True
Int Array: [<null>, 1, 2]
  Contains MyEnum.A (0): False
  Contains MyEnum.B (1): True
  Contains MyEnum.C (2): True

@InkONat
Copy link
Author

InkONat commented Oct 28, 2024

Sounds like a duplicate of #98309. Make sure the array property is typed when serialized to the scene, it should look like: Array[int]([0, 1, 2])

Doing new Array<int>{0}; did not work
([0]) doesn't work

@InkONat
Copy link
Author

InkONat commented Oct 28, 2024

image
image
image
Even if I don't use 0 and use another index like 1, it doesn't work

@a-johnston
Copy link
Contributor

Sounds like a duplicate of #98309. Make sure the array property is typed when serialized to the scene, it should look like: Array[int]([0, 1, 2])

Doing new Array<int>{0}; did not work ([0]) doesn't work

they're referring to how the array is serialized by the resource/scene. For example, in the example project I linked above, main.tscn has these two lines in it (in the case of having a non-null default value):

enumArray = Array[int]([0, 1, 2])
intArray = [null, 1, 2]

@InkONat
Copy link
Author

InkONat commented Oct 28, 2024

Sounds like a duplicate of #98309. Make sure the array property is typed when serialized to the scene, it should look like: Array[int]([0, 1, 2])

Doing new Array<int>{0}; did not work ([0]) doesn't work

they're referring to how the array is serialized by the resource/scene. For example, in the example project I linked above, main.tscn has these two lines in it (in the case of having a non-null default value):

enumArray = Array[int]([0, 1, 2])
intArray = [null, 1, 2]

#98595 (comment)
image

The array is already set to another value in this function, which is called in an Initialize() function of the ItemManager
In the same node script where the behavior happens:
image

@InkONat
Copy link
Author

InkONat commented Oct 29, 2024

A workaround I found was just using int[] instead of Array<int>, since Godot thinks of int[] as a PackedInt32Array. But the issue persists with normal Arrays.

@InkONat
Copy link
Author

InkONat commented Nov 3, 2024

Can someone add the confirmed label?

@Repiteo Repiteo added this to the 4.4 milestone Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants