Different Dictionary behavior when there are breakpoints vs when there isnt

Godot Version

v4.5.stable.official [876b29033]
(was v4.3.stable.official [77dcf97d8], I just updated to see if it would fix the problem, it did not)

Question

Okay so im making a multiplayer game, so I have a server, which is written in go, and the Godot client. My issue is from decoding a packet from the server.

Here is all of the relevant code for where the packet is received:

func recieve_packet(packet: String) -> void:
	print("Packet: ", packet)
	
	var pkt = JSON.parse_string(packet)
	
	if pkt["type"] == "chunk":
		main.receive_chunk_data(pkt["content"])
func receive_chunk_data(data: Dictionary):
	#print(data["coords"])
	
	var coords = data["coords"]
	var tiles = data["tiles"]
	
	loaded_chunks.append(coords)
	map.generate_chunk(coords[0], coords[1], tiles)

So if I run this code normally i get this error:

Invalid access to property or key ‘tiles’ on a base object of type ‘Dictionary’.

thats on the line: var tiles = data["tiles"]
and if I look at the local stack variables, data is indeed a dictionary with only the field “coords”

and the print statement gives this, which shows that the “tiles” field is there:

Packet: {“type”:“chunk”,“content”:{“coords”:[248,248],“tiles”:[[[6,0,0],[6,0,0],[…

the tiles field is a 64x64x3 array

but if i add breakpoints to the two lines:

var coords = data["coords"]
var tiles = data["tiles"]

then when it stops at the first line and I check what the data variable contains it has both the “coords” and “tiles” fields and works just fine, but i have to manually tell it to continue every time.

So I would appreciate any insight into this problem and how I might be able to solve it!
Let me know if there is anything im missing that could help!

Can you try to print out the packet after you JSON.parse_string it? Maybe Godots JSON implementation reads the string wrong or has some trouble with 3 dimensional arrays

if i print pkt right after parse string, I still get the dictionary containing “tiles”

Parsed: { “type”: “chunk”, “content”: { “coords”: [248.0, 248.0], “tiles”: [[[6.0, 0.0, 0.0], [6…

Can you try to call the has-method of the dictionary:

if data.has("tiles"):
    print(data["tiles"])
else:
    print("Tiles does not exist: ", data)

I could also imagine that packet may be a dictionary, BUT not the “content” part or atleast the tiles-array. As you said in the local stack there is only the coords-variable. Your tiles-array may not be read correctly, though im not sure

1 Like

When I tried this originally it was still being weird, data did have tiles and printed successfully but then it still errored out later on, but putting the rest of what was necessary in the if statement seemed to fix it!

func receive_chunk_data(data: Dictionary):
	if data.has("tiles"):
		#print(data["tiles"])
		var tiles = data["tiles"]
		var coords = data["coords"]
		
		loaded_chunks.append(coords)
		map.generate_chunk(coords[0], coords[1], tiles)
	else:
		print("Tiles does not exist: ", data)

Its still kind of acting weird, like every chunk that loads also prints “Tiles does not exist:” but it works.

func generate_chunk(chunk_x, chunk_y, data):
	var chunk_data = data

I did need some jank at the start of the following function to fix data being null for some reason, but it seems to be in working order now, Thank you!!

1 Like

Just out of curiosity: are you working with multiple threads?

This behaviour shouldnt happen and should probably be reported in the github or there is something we are missing.

1 Like

I am not working with multiple threads, its possible that there’s something missing, especially because its still printing out tiles does not exist for every one even though they load successfully.
its possible that its making some sort of double for each one?

But yeah I can make a github issue in case there is something unintentional happening here.

1 Like

i guess its working. my most likely explanation is also duplicates. Maybe the backend sends them twice? or godot handles the packets twice for some reason, but that would mean it also prints the packets twice. I dont expect you to try to read through the print statements and fix that.
Its just weird behaviour.

Good luck with your project!

1 Like

Yeah, its definitely very strange and thank you!

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