0 votes

I connect to a server a number of times to receive certain responses when interacting with an object in the game. This is working fine and I get the response and it prints to the screen. However, I am getting a bug where the game crashes when I interact with an object AFTER having interacted with my http client (no error message in the debugger). I thought it may have been something to do with the threading but the same issue happens even when the HTTP request is not threaded. I've tried loads of things but nothing seems to work. Can anybody else see a problem with my code, i've placed an issue up on godot 3.0 issues to see if it may be a bug with editor.

var thread = Thread.new()

func enter_question():
            var text = get_node("Container/LineEdit").get_text()
            thread.start(self, "send_question", text)

func send_question(text):
    var inputDict = {question=text}     
    get_node("/root/HTTPClientScript").talkToServer("url", "bot", inputDict)
    canSend = true
    thread.wait_to_finish()

Below is my HTTP script:

extends Node

var HEADERS = PoolStringArray()
var RESPONSE = 0
var HTTP = 0

func talkToServer(url, mode, data):
    # Connect to host/port
    HTTP = HTTPClient.new()

    RESPONSE = HTTP.connect_to_host("url", 443, true)
    # Wait until resolved and connected
    while HTTP.get_status() == HTTPClient.STATUS_CONNECTING or HTTP.get_status() == HTTPClient.STATUS_RESOLVING:
        HTTP.poll()
        OS.delay_msec(300)

    # Error catch: Could not connect
    assert(HTTP.get_status() == HTTPClient.STATUS_CONNECTED)
    # Check for a GET or POST command
    if data == null:
        HEADERS = PoolStringArray(["key:access_key", "Content-Type: application/json", "Accept: */*"])
        RESPONSE = HTTP.request(HTTPClient.METHOD_GET, url, HEADERS)
    else:
        var js = to_json(data)
        print(js)
        HEADERS = PoolStringArray(["key:access_key", "Content-Type: application/json"])
        RESPONSE = HTTP.request(HTTPClient.METHOD_POST, url, HEADERS, js)
    # Make sure all is OK
    assert(RESPONSE == OK)
    # Keep polling until the request is going on
    while (HTTP.get_status() == HTTPClient.STATUS_REQUESTING):
        HTTP.poll()
        OS.delay_msec(300)
    # Make sure request finished
    assert(HTTP.get_status() == HTTPClient.STATUS_BODY or HTTP.get_status() == HTTPClient.STATUS_CONNECTED)
    # Set up some variables
    var RB = PoolByteArray()
    var CHUNK = 0
    var RESULT = 0
    # Raw data array
    if HTTP.has_response():
        # Get response headers
        var headers = HTTP.get_response_headers_as_dictionary()
        while HTTP.get_status() == HTTPClient.STATUS_BODY:
            HTTP.poll()
            CHUNK = HTTP.read_response_body_chunk()
            if(CHUNK.size() == 0):
                OS.delay_usec(100)
            else:
                RB = RB + CHUNK
            HTTP.close()
            RESULT = RB.get_string_from_ascii()
            # Do something with the response
            if mode == 'bot':
                print(str(RESULT))
                get_node("/root/World/node").return_string_from_bot(RESULT)
asked Nov 6, 2017 in Engine by stubbsy345 (282 points)
edited Nov 6, 2017 by stubbsy345

Please log in or register to answer this question.

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.