0 votes

The Set() seems to not working with the dictionary value ( object.set("dict:key", value) ),

Using Expression: "object.dict[key] = value" give me an error (Error: Cannot use "=" In expression.)

Workaround[1] Is that you get an entire Dictionary then set the value then return them back. This is not very efficient.

Workaround[2] Is using tween just to set a value because it's for some reason working with "dict:key" because tween can take NodePath but Set() does not? And this is still not very efficient since you literally have to add a node to set a variable.

edit: yeah do this object.get(dict_name)[key] = value

Godot version Godot 3.3
in Engine by (52 points)
edited by

1 Answer

+1 vote
Best answer

Hi,
you dont need an expression, just address the dictionary directly.

var object = get_node("path to the node") #or use any other method to get the node
var key = "my key"
var value = "my value"
object.dict[key] = value
by (3,922 points)
selected by

I tried that and it doesn't work. I'm guessing .get() is getting the value but didn't refer to it.

Godot Documentation states: Dictionaries are always passed by reference.

This will work!

a nodes script:

extends Node2D

func _ready():
    print("Before")
    print($child.dict)
    var object = get_node("child")
    object.get("dict")["other"] = "another"
    print("Now")
    print($child.dict)

the child named "child"

extends Node2D

var dict ={
    "this": "that"
}

this will print ...

Before
{this:that}
Now
{other:another, this:that}

??? I tried that again, it didn't work the first time but it works the second time. I didn't changed any code wth
I guess this is the solution then

The solution is basicall this object.get(dict_name)[key] = value

Also,

Godot Documentation states: Dictionaries are always passed by reference.

This is actually new to me, I've been trying to find some information for something like this, but Godot is just too hard to actually navigate to what you actually wanted, man. Thank you

you can find more informations about that here:
https://docs.godotengine.org/en/stable/getting_started/scripting/gdscript/gdscript_advanced.html#dynamic-nature

but beware ... even here it is not mentioned that poolarrays are passed by value and not by reference. This acception can only be found in the poolarray docs.

It's somewhat tricky ... but the docs are not too bad like some other api docs are.

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 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 webmaster@godotengine.org with your username.