Getting data from dictionary
Asked Answered
O

10

0

Hi all, got a small issue here trying to get information from a dictionary. I'm creating a chunk system for my game and storing the node of a chunk and a basic coordinate system into a dictionary. The start area is chunk 0,0 and the next chunk to the right is 1,0.

this is the simple code written on the Chunk script:
Core.all_chunks[Vector2(x,z)] = self

I have a Core script that runs stuff for the game, and includes the dictionary. So far this works just fine, if I run a Print by passing in a Vector2 I get the appropriate Chunk node.

But for some reason, when I try to get the Chunk in other areas, its not working, and I don't know why.

I've got this function on the Player to check which Chunk they are in after moving:
`func get_current_chunk() -> Vector2:
var xPos
var zPos
if position.x >= 0:
xPos = floor(position.x / 64)
else:
xPos = ceil(position.x / 64)

if position.z >= 0:
	zPos = floor(position.z / 64)
else:
	zPos = ceil(position.z / 64)
return Vector2(xPos, zPos)`

The Chunk size is 64 tiles, so I'm just taking the player location and dividing by 64 then rounding to see where the player is. This seems to be working just fine so far. But when I pass this information to the code above to get which Chunk the player is in, I get an error and I'm not sure why.

If I try to set the Current_Chunk variable on the Player to the current Chunk that they are on:
Current_Chunk = Core.all_chunks[get_current_chunk()]
I get errors. It says I have an invalid get index '(0,0)' which doesn't make a ton of sense. All the chunks are added to the dictionary long before the player is instantiated and needs to know which chunk its on. Why would I be getting this error? I can even check the dictionary to make sure the Chunk is there, and it is there.

Oosperm answered 28/2 at 20:38 Comment(0)
M
0

Oosperm Don't use floating point values as dictionary keys. Use Vector2i instead of Vector2. You may have other issues but do this first as floats could raise their ugly head and cause problems at any time.

Meritocracy answered 28/2 at 20:58 Comment(0)
O
0

Meritocracy That seems to be a good place to start!

I replaced all instances of Vector2 with Vector2i, and I am still getting the same error.

So weird thing: When I start the game all the chunks are adding themselves to the dictionary. I can find the correct chunk by passing in a vector2. So I know I can get the Chunk from the dictionary.

But for some reason when doing it here, its not working. If I just print the "get_current_chunk" result its currently 0,0, as it should be. But when I try to pass that value to the dictionary, for some reason its all wrong. This is even after changing everything to Vector2i. What could I be missing?

Oosperm answered 28/2 at 21:9 Comment(0)
O
0

Meritocracy If I run this on my player:

print(core.all_chunks[0])

I get the first Chunk node.

but if I run this:

print(core.all_chunks[Vector2i(0,0)])

I have an invalid get index. So I guess the problem is somewhere in how I am setting up the dictionary? WHich doesn't make much sense, since I can run on the Chunk after adding to the dictionary:

core.all_chunks[Vector2i(0,0)] = self
print(core.all_chunks[Vector2i(0,0)])

and it works perfectly. It is able to pull the correct Chunk from the dictionary by using the Vector2i. So if I run the code from the Chunk, works fine. If I run the code from the Player, error.

Oosperm answered 28/2 at 21:22 Comment(0)
M
0

Oosperm Print the whole dictionary.

Meritocracy answered 28/2 at 22:0 Comment(0)
O
0

Meritocracy when I print the whole dictionary it just displays the Chunk objects, doesn't like the Vector2i keys. When I print from the Chunk.

Let me try from the player. (Gonna be a few hours. Gotta do life things haha)

Oosperm answered 28/2 at 22:29 Comment(0)
O
0

Meritocracy Alright when I print the dictionary from the player, I do get both Chunks that have been added to it.

[World_Test:<Node3D#48268051703>, Test_Chunk_2:<Node3D#49106912809>]

So they are there, and I can grab the correct dictionary

Oosperm answered 29/2 at 17:24 Comment(0)
M
0

Oosperm That looks like an array, not a dictionary. There's no Vector2i keys.

Meritocracy answered 29/2 at 17:30 Comment(0)
O
0

Meritocracy That does seem to be the case. But I am not sure why it would be an array and not a dictionary. Perhaps its how I am setting the values in the dictionary?

In the Core script, I have the dictionary:
var all_chunks = {}

In the Chunk script, I am adding to the dictionary:

@export var chunk_id: Vector2i
func _ready():
	core.all_chunks[chunk_id] = self

If I understand correctly, this should be creating an entry in the dictionary with they key of chunk_id (which is a Vector2i) right?

Oosperm answered 29/2 at 17:44 Comment(0)
M
0

Oosperm You may be accidentally re-assigning an array to core.all_chunks somewhere in your script code. So search for all appearances of this variable in your project and see what happens with it.

To catch this more easily you can statically type the variable at definition:

var all_chunks: Dictionary = {}
Meritocracy answered 29/2 at 17:53 Comment(0)
O
0

Meritocracy Ah, that did immediately show an error, which I think was the problem. I had a line of code that was grabbing all the chunks in the level and adding just like it was an array. I think that might have been the problem!

Thanks for helping walk me through it!

Oosperm answered 29/2 at 18:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.