Dictionary becoming <null>

Godot Version

Godot4.3

Question

say there are 2 dictionaries:

var dict1={}
for i in range(30_000):
dict1[i] = Color.RED

var dict2={}
for i in range(40_000):
dict2[i] = Color.RED

dict1 works as intended but dict2 becomes null. So this is most likely a memory issue? I’m not 100% certain. Why is this happening and how can I fix it? (If i even can)

Works for me, do you get a specific error, or have different code than the sample provided?

1 Like

no, there are no errors. The dictionary just silently becomes null.

Also this is the entire code. It’s on a test script.

Could you retry with a much bigger number like 1million and see if that makes it null?

A lot of thing will happen before memory runs out. On windows a “page” file will be written to disk as a super-slow version of ram; similarly on linux a “swap” partition, file, or compression depending on the set up. Generally your operating system will kill programs before acknowledging it’s out of ram.

I’ve bumped it to one million colors is dict2

func _ready() -> void:
	var dict1={}
	for i in range(30_000):
		dict1[i] = Color.BLUE

	var dict2={}
	for i in range(1_000_000):
		dict2[i] = Color.RED

	print(var_to_bytes(dict2).size()) # 28000008
	
	print(dict1[200]) # (0, 0, 1, 1)
	print(dict2[1_000]) # (1, 0, 0, 1)

The prints indicate a dictionary of 1 million colors uses about 28MB, I’m sure it’s a bit off but it’s far from eating up all of your system memory.

1 Like

If you wanted to compress your dict2s to a PackedColorArray the size would end up as 16MB, so I think the original figure of 28MB is accurate.

var dict2: PackedColorArray = []
dict2.resize(1_000_000)
for i in 1_000_000:
	dict2[i] = Color.RED

print(dict2.to_byte_array().size()) # 16000000
1 Like

okay, so this is what was happening.

When I hover over dict1 it shows null, so that’s why I thought it was becoming null.

Also it’s displayed as null on the debugger tab’s variables.

But testing it like you (dict1[10_000]) returned the values as normal.

So the dictionary is not actually null, but only displayed as null. Weird.

It’s probably out of scope, where was your breakpoint?

1 Like

this is the entire script.

I guess it’s technically a bug? The script prints (1,0,0,1) so the dictionary definitely isn’t null.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.