How to set multiple dictionary variables with the same value?

Godot Version



Hi all I have a question about assigning values to dictionary items. Below some code from a function that I have written:

	var Status = {"Xvalue" : 0.0, "Yvalue" : 0.0}
	if MouseActive and InputEventMouseMotion:
		Status.Xvalue = Value * Input.get_last_mouse_velocity().x * MouseSensitivity
		Status.Yvalue = Value * Input.get_last_mouse_velocity().y * MouseSensitivity
	if Input.is_action_pressed(KeyPositive):
		Status.Xvalue = Value
		Status.Yvalue = Value

It works fine, but I was wondering if the assignment

	if Input.is_action_pressed(KeyPositive):
		Status.Xvalue = Value
		Status.Yvalue = Value

can be made on a single line as they should both receive the same value?

I’m asking this, so within the future I could make eg. a ‘power up’ for all or selected inventory items in a dictionary. I’m still a beginner in GdScript. Thanks for any reply.



Are you sure Status isn’t an enum?

dictionary sets look like:

Status[XValue] = Value

Anyway, writing to a dictionary from a variable is slightly more performant that writing to a dictionary keypair from another dictionary keypair.

Not with one line, but with two you can do that:

var my_dict := {"a": 0, "b": 0,  "c": 0,  "d": 0,  "e": 0,  "f": 0}
# Let's suppose you want to change all of them to 1 now
# Create a for loop, each loop the key variable will one of keys 
# from the dict (ex: Will start being "a", next loop "b", "c", etc) 
for key in my_dict:
	my_dict[key] = 1
1 Like