Cant seem to add dictionary keys

Godot Version

4.3 Stable x64 for Win

Question

I am bashing my head into a wall with Dictionaries. I have looked through the documentation profusely and attempted nearly every single syntax suggested (as well as the get_or_add() method) and I cannt get my custom resource to append a Vector2i key or its ENUM value to the resource dictionary.

the key and ENUM prints fine in the parent function, and I can manually add both in the custom resource by declaring them inside the empty dictionary. It also doesnt matter if I convert the vector2i to string, that doesnt seem to work either. I am sure it is something I am missing - Help?

ChunkResource (holds the dictionary I am attempting to modify)

class_name ChunkResource
extends Resource

var chunk_index : Vector2i
var block_dict : Dictionary
var entity_dict : Dictionary

func generate_chunk_res(index: Vector2i) -> ChunkResource:
	chunk_index = index
	return self

World Generator (attempting to store block data on specified chunk)

func generate_new_chunk(chunks: Array[Vector2i], world_resource: WorldResource) -> ChunkResource:
	var chunk_resource = ChunkResource.new()

	for chunk in chunks:
		chunk_resource.generate_chunk_res(chunk)

		for x in range(0, world_resource.CHUNK_SIZE):
			for y in range(-world_resource.CHUNK_SIZE, 0):
				var noise = world_resource.noise_resource.noise.get_noise_2d(
					x * world_resource.CHUNK_SIZE,
					y * -world_resource.CHUNK_SIZE)
				
				if noise > world_resource.noise_resource.noise_threshold:
					var key:= Vector2i(x, y)
					chunk_resource.block_dict[key] = Blocks.block.STONE
	
	chunk_generated.emit()
	return chunk_resource
  • Are you getting any errors?
  • Is the condition noise > world_resource.noise_resource.noise_threshold ever true?
  • Does this issue arise after you save and then load your resource from storage memory?
1 Like

Did you try adding a breakpoint to the chunk_resource.block_dict[key] = Blocks.block.STONE line? If so, what does the dictionary’s content look like afterwards?

1 Like

@indicainkwell @ratrogue yes - I figured it out! The resource saving/loading and if statement was fine. The issue was in the declaration:

I needed to define the dictionary this way:
var block_dict = {}

Not:
var block_dict : Dictionary

I suppose I could do Dictionary.new(), but the problem was the new dictionary actually wasn’t created.

I knew it was something dumb on my end lol. Thanks for your help!!

You could also use:

var block_dict : Dictionary = {}

This defines the type and initialize it to an empty dictionary.

This is interesting.

I ran your code from the original post with some mock data in v4.3.stable and it works as you would expect. Including with the var block_dict: Dictionary declaration. I thought that dictionaries were default initialized to a non-null value. Which appears to be true on my machine.

And makes me wonder?

If you have a moment could you upload a minimal project to test locally?

1 Like

Yes, a Dictionary is not an object like a Node and cannot be null. My guess is that @freakyautomn’s error was actually somewhere else.

var dict : Dictionary

func _ready() -> void:
	dict["a"] = "foo"
	print(dict)

outputs

{ "a": "foo" }

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