+1 vote

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...}

in Engine by (199 points)

1 Answer

+2 votes

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.

by (71 points)

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}
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.