How to initialize an dictionary in a function in GDScript

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

I wonder if it is possible to update the dictionary with new content (funct test_init_dict) and not only update its existing content.

Using ‘test_init_dict’ in the example bellow, the dictionary is not updated.

I would use it to keep de response from a http request, in a request_completed method, passing the dictionary as parameter. For each request I could inform a different dictionary. As the http request would return either an json array or a json object, I don’t want to manually iterate the response to update the dictionary.

thanks in advance

onready var enderecamentos : Dictionary = {"x":88, "z":99}

func _ready():
	
	test_init_dict(enderecamentos)
	
	print("enderecamentos 1: ", enderecamentos)
	
	update_dict(enderecamentos)
	
	print("enderecamentos 2: ", enderecamentos)

func test_init_dict(dict):
	dict = {"a":"cccc","b":123}

func update_dict(dict):
	dict.erase("z")

output:

enderecamentos 1: {x:88, z:99}
enderecamentos 2: {x:88}

I moved my comment to a potential Answer

backendcoder | 2021-01-29 01:57

:bust_in_silhouette: Reply From: backendcoder

In your test_init_dict function, you pass in a reference value that is stored in the local variable dict. This is then replaced by a new reference value locally.

What you could do is have a dictionary key for the captured request data such as dict[“data”]. Then the update is reflected back to the external dictionary.

Also, you don’t need the onready statement unless you are waiting for the node tree to be built.

Thanks for your response,

I think having a key for the new data is a good alternative.
In my case I’ve added 3 extra parameters in the _on_request_completed, the request itself, wich will be freed at the end, the dictionary requested_data with all the responses, and the request_type that is the key where it will be stored the response.


enum RequestType {
	ENDERECAMENTOS,
	CARGAS
}

var requested_data : Dictionary = {}

func _ready():

clear_objects()

var http_request = HTTPRequest.new()
add_child(http_request)
http_request.connect("request_completed", self, "_on_request_completed", [http_request, requested_data, RequestType.ENDERECAMENTOS])
http_request.request("http://localhost:8080/...estrutura")

func _on_request_completed(result, response_code, headers, body, request, requested_data, request_type):

print("result: ", result)

print("response_code: ", response_code)

print("headers: ", headers)

var json_response = JSON.parse(body.get_string_from_utf8())

match (request_type):
	RequestType.ENDERECAMENTOS:
		requested_data[request_type] = json_response.result
		
		var http_request = HTTPRequest.new()
		add_child(http_request)
		http_request.connect("request_completed", self, "_on_request_completed", [http_request, requested_data, RequestType.CARGAS])
		http_request.request("http://localhost:8080/...cargas")
	
	RequestType.CARGAS:
		requested_data[request_type] = json_response.result
		populate()

print("error: ", json_response.get_error())


request.queue_free()

brian721 | 2021-01-29 12:45