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#: Fix typed arrays not saving properly #82976

Closed
wants to merge 1 commit into from

Conversation

Repiteo
Copy link
Contributor

@Repiteo Repiteo commented Oct 7, 2023

Fixes a typo in ScriptPropertiesGenerator.cs that was making the passed hint TypeString instead of ArrayType. While I'm not certain of the full ramifications of fixing this error, one immediate benefit is having resources correctly save arrays as typed if they're exported as such (like GDScript)


Example file to save a single "123" entry

public partial class Main : Node2D
{
    [Export]
    private Godot.Collections.Array<int> _array;
}

Before:

_array = [123]

After:

_array = Array[int]([123])

@Repiteo Repiteo requested a review from a team as a code owner October 7, 2023 16:48
@AThousandShips AThousandShips added this to the 4.2 milestone Oct 7, 2023
@raulsntos
Copy link
Member

From what I can see GDScript also uses PROPERTY_HINT_TYPE_STRING for exported arrays:

variable->export_info.hint = PROPERTY_HINT_TYPE_STRING;
variable->export_info.hint_string = hint_prefix + ":" + variable->export_info.hint_string;
variable->export_info.type = Variant::ARRAY;

And changing the hint type would also require changing the hint_string, because I think the %s/%s:%s format is only meant to be used with PROPERTY_HINT_STRING.

I'm not entirely sure what hint and hint_string exported arrays are supposed to use, there was some previous discussion about this: #79366 (comment). But my guess is that these changes are not enough for what you are trying to fix, and the current behavior seems correct. So what issue is this trying to solve? Does it matter if the array is stored as untyped in the serialized scene? I'd assume it makes no difference.

@Repiteo
Copy link
Contributor Author

Repiteo commented Oct 7, 2023

Huh! That's certainly strange, but can't argue with the fact the live version works

Regardless of if there's a difference in serialization of a typed vs untyped array, I imagine it'd be convenient if resources saved uniformly. So if this hint string was required in the first place, then it must mean the actual serialization utilizes some other means to tell if an array is typed or not. This method takes effect for GDScript, but not CSharp

@raulsntos
Copy link
Member

So I tested exported typed arrays in Godot v4.1.2.stable.mono.official.399c9dc39, and this is how the scene is serialized:

[gd_scene load_steps=3 format=3 uid="uid://b4e5y5y8e3b15"]

[ext_resource type="Script" path="res://MyNode.cs" id="1_ja7li"]
[ext_resource type="Script" path="res://MyNode.gd" id="2_n82wm"]

[node name="Main" type="Node"]

[node name="MyNodeCS" type="Node" parent="."]
script = ExtResource("1_ja7li")
NumberArray = Array[int]([1, 2, 3])

[node name="MyNodeGD" type="Node" parent="."]
script = ExtResource("2_n82wm")
number_array = Array[int]([1, 2, 3])

So it seems it's already using the typed array for serialization in both C# and GDScript. How did you get it to serialize using the untyped array? What version of Godot were you using?

@YuriSizov YuriSizov changed the title C# - fix typed arrays not saving properly C#: Fix typed arrays not saving properly Oct 27, 2023
@Repiteo
Copy link
Contributor Author

Repiteo commented Oct 28, 2023

Huh… I'm not sure what happened then. The dev build I was testing on had this bug, but that's not the case for 4.1.2 nor the recent 4.2 beta3. I'll have to do some digging to see what happened

@Repiteo
Copy link
Contributor Author

Repiteo commented Oct 28, 2023

As this doesn't seem to represent the live nor upcoming godot versions, I'll be closing this PR. If I find a consistent means of replication, I'll make a new PR with whatever new context came up

@Repiteo Repiteo closed this Oct 28, 2023
@akien-mga akien-mga removed this from the 4.2 milestone Oct 28, 2023
@akien-mga akien-mga removed the bug label Oct 28, 2023
@Repiteo Repiteo deleted the c#-typed-array-fix branch October 28, 2023 19:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants