Vector VS String - which is best as dictionary keys?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AiTechEye

In my game there are thousands of dictionares in a dictionary

Vectors are just to set, no extra code needed to treat the key, but strings are the opposite, but are vectors using more ram/cpu?

Which one are best to use?

	[Vector3(56,1,543)] = {...stuff...}
	["(56,1,543)"] = {...stuff...}

:bust_in_silhouette: Reply From: lacethespace

It is better to use strings as keys, as they are immutable while arrays are mutable.

var d = {[1] : "test"}
for key in d:

What do you think the result will be? I tried it and got {[1, 2]:Null}, which means that the key was successfully modified but the value “test” is no longer reachable.

This kind of in-place modification of keys is never a good idea and it’s best to use immutable types as keys, so basically strings or numbers.

If you wrote more about the problem you are solving maybe a better solution could be proposed.

There isen’t a problem yet, already using strings as keys.

So vectors can be changed while editing dictionares?

var pos = Vector3(1,2,3)
var objects_list = {}

for x in range(pos.x,pos.x+100):
for z in range(pos.z,pos.z+100):
	var v = objects_list.get(Vector3(x,0,z))
	if v == null:
		objects_list[Vector3(x,0,z)] = {object,data,etc}

AiTechEye | 2020-03-02 14:33