0 votes

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

Using 'testinitdict' 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}
Godot version 3.2
in Engine by (15 points)

I moved my comment to a potential Answer

1 Answer

+1 vote
Best answer

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.

by (72 points)
selected by

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 onrequest_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()
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.