Skip to content

Invalid get index of type StringName on base Dictionary when key is an empty StringName #77894

Description

@maniacalipsis

Godot version

4.0.3 stable

System information

Kubuntu 22.04

Issue description

I have a dictionary with string keys, and one of the keys is "" (empty string). To get values from dictionary by the key, the StringName parameter is used. So all works fine unless the key is an empty StringName.
Context: dictionary stores some data about input actions, that are type of StringName (and certain Input and InputEvent methods expects StringName arguments), that's why this type is prefered rather than String. And one of the dictionary elements is kinda nil-element, used to eliminate if _dict.has(key) checks.

Steps to reproduce

  1. Make *.gd script
  2. Add a Dictionary variable, e.g. var _dict:={}
  3. Add some value with empty key _dict[&""]=someval or _dict[""]=someval
  4. Try to get this value like print(_dict[&""])

Minimal reproduction project

Example declaration:

class_name Something extends Node

var _dict:={&"":"--emptyness--",&"a":"A"}

func get_val_by_strname(key:StringName):
   return _dict[key]

func get_val_by_strname_converted(key:StringName):
   return _dict[key as String]

func get_val_by_str(key:String):
   return _dict[key]

Example usage:

print(Something.get_val_by_str("A"))  #Will print "A".
print(Something.get_val_by_strname_converted(&"A"))  #Will print "A".
print(Something.get_val_by_strname(&"A"))  #Will print "A".

print(Something.get_val_by_str(""))  #Will print "--emptyness--".
print(Something.get_val_by_strname_converted(&""))  #Will print "--emptyness--".
print(Something.get_val_by_strname(&""))  #Will throw an error "Invalid get index of type StringName on base Dictionary".

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions