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

GDScript 2.0: Using both StringName and String as Dictionary keys at the same time results in unexpected behaviour #62957

Closed
Kubulambula opened this issue Jul 12, 2022 · 4 comments · Fixed by #68747

Comments

@Kubulambula
Copy link
Contributor

Kubulambula commented Jul 12, 2022

Godot version

4.0.alpha11

System information

Windows 10

Issue description

When using both StringNames and Strings as Dictionary keys at the same time, getting the value of that key is inconsistent and appears to be based on the key order in the Dictionary.
This is was probably implemeted intentionally to make it less confusing when the dictionary uses StringName keys, but the user uses Strings.
Also when using the Dictionary.key syntax it should be probably treated as String as the use of StringNames as keys would be more rare.

It should either be forbidden to make two keys with the same text value or make the dictionary interpret these two variations consistently.

Steps to reproduce

Just copy and paste this example into any project:

var dict = {
	"key": "String",
	&"key": "StringName",
}

var reversed_dict = {
	&"key": "StringName",
	"key": "String",
}

var simple_dict = {
	&"key": "StringName"
}

var second_simple_dict = {
	"key": "String"
}


func _ready():
	print(dict["key"]) # StrignName - expected String
	print(reversed_dict["key"]) # String
	
	print(dict[&"key"]) # StrignName
	print(reversed_dict[&"key"]) # String - expected StringName
	
	print(dict.key) # StringName - expected String
	print(reversed_dict.key) # String
	
	print(simple_dict["key"]) # StringName - expected invalid index error
	print(simple_dict[&"key"]) # StringName
	print(simple_dict.key) # StringName - expected invalid index error
	
	print(second_simple_dict["key"]) # String
	print(second_simple_dict[&"key"]) # String - expected invalid index error

Minimal reproduction project

No response

@Calinou
Copy link
Member

Calinou commented Jul 12, 2022

Dictionary internally converts StringNames to Strings, which is likely the cause of this issue.

@Calinou Calinou added this to the 4.0 milestone Jul 12, 2022
@Calinou Calinou changed the title Using both StringName and String as Dictionary keys at the same time results in unexpected behaviour GDScript 2.0: Using both StringName and String as Dictionary keys at the same time results in unexpected behaviour Jul 12, 2022
@noobyogi0010
Copy link

Hey @Calinou
I would like to work on this issue if no one else has already taken it up.
Thanks!

@Calinou
Copy link
Member

Calinou commented Jul 12, 2022

@noobyogi0010 Feel free to open a pull request for this 🙂

@cdemirer
Copy link
Contributor

Since the results are intended, I believe the bug is that the first 2 Dictionaries should have caused a compile-time error ("Key ... was already used in this dictionary") the same way having 2 equal Strings would.

On the other hand, maybe those errors should be warnings instead, because you can do the same (without error) using variables at runtime. But I guess that's another issue/proposal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants