Skip to content

Typed Arrays cannot erase pairs with null/invalid keys #1310

Description

@ValiantMaverick

Tested versions

Reproducible in v26.1

System information

Fedora-based Linux

Issue description

The erase() function in typed Dictionaries won't accept an invalid instance of node types and therefore cannot remove key-value pairs where the key is a node that has been freed.

Steps to reproduce

Throw this in a script and execute it:

extends Node2D

var typed_dict: Dictionary[Node2D,int]
var untyped_dict: Dictionary

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	var arr: Array[Node2D]  # Keep a reference to the nodes we create
	for i in range(4):
		arr.append(Node2D.new())
		typed_dict[arr[i]] = i
		untyped_dict[arr[i]] = i
	arr[1].free() # Free one of the key nodes in both dictionaries
	
	print("Both dictionaries loaded. One node key freed.")
	
	test_untyped()
	test_typed()

# Test removing the invalid/null key from untyped dictionary
func test_untyped() -> void:
	print("Testing untyped dict: ")
	for node in untyped_dict.keys():
		if not is_instance_valid(node):
			print("Found the bad one")
			untyped_dict.erase(node)
	print("Did it get removed?") # Note: there is only one invalid key to remove
	for node in untyped_dict.keys():
		if not is_instance_valid(node):
			print("NOPE!! Untyped failed. See error.")
			return
	print("Yep. Untyped succeeded.")

# Test removing the invalid/null key from typed dictionary
func test_typed() -> void:
	print("Testing typed dict: ")
	for node in typed_dict.keys():
		if not is_instance_valid(node):
			print("Found the bad one")
			typed_dict.erase(node)
	print("Did it get removed?") # Note: there is only one invalid key to remove
	for node in typed_dict.keys():
		if not is_instance_valid(node):
			print("NOPE!! Typed failed. See error.")
			return
	print("Yep. Typed succeeded.")

Minimal reproduction project (MRP)

test-project-please-goof-off.tar.gz

test-project-please-goof-off.zip

Metadata

Metadata

Assignees

Type

No type

Projects

Status
Open

Relationships

None yet

Development

No branches or pull requests

Issue actions