0 votes

I have a backend setup which sends the user a random reward upon finishing the game.
Currently everything is working well however I don't know how to read the result and display an image and label depending on what reward the user gets.
Any suggestions?
What I have currenly is:

func _on_Barrel01Area_input_event(viewport, event, shape_idx):
    if (event is InputEventMouseButton && event.pressed):
        get_tree().get_root().set_disable_input(true)
        $SmasherSprite/SmasherAttack.play("smash")
        yield(get_tree().create_timer(1.1), "timeout")
        $Barrel01.play("idle")
        yield(get_tree().create_timer(1.1), "timeout")
        #random.randomize()
        var x =1
        if x == 1:
            var reward = "Lawnmower"
        $Barrel01.hide()
        $Barrel02.hide()
        $Barrel03.hide()
        $SmasherSprite.hide()
        $SelectedRewards.show_Lawnmower()
        get_tree().get_root().set_disable_input(false)
        var url = "http://localhost:8080/endGame"
        var body = {"score": globals.score,"time": globals.time}
        var token = JavaScript.eval("localStorage.getItem('TOKEN')")
        var jsonBody = JSON.print(body)
        $HTTPRequest.request(url, ["Authorization: %s" % token, "Content-length: %d" % jsonBody.length(), "Content-type: application/json"], false, HTTPClient.METHOD_POST, jsonBody)
        #$HTTPRequest.request(url, [], true, HTTPClient.METHOD_POST)

And

Godot version Godot 3.3.2
in Engine by (27 points)

1 Answer

+1 vote
by (636 points)

I've managed to get it to work however a thing I have no clue how to do is json matching, so if my data is equal to lets say {name: Vino}. How would I make something happen if the Json result is exactly Vino?
I've tried this but it does not work, tried different ways to write the name part such as {name: Vino} etc.

    var json = JSON.parse(body.get_string_from_utf8())
    print(json.result)
    if (json.result) == 'name: "Vino"':
    $SelectedRewards/Lawnmower.show()
    $SelectedRewards/LabelLawnmower.show()

The result of var json = JSON.parse(body.get_string_from_utf8()) is a JSONParseResult

Reading that page you should (assuming it is a Dictionary as it can also be an Array)

if json_result.error == OK:
  var my_data :Dictionary = json.result
  if my_data.name == 'Vino':
    $SelectedRewards/Lawnmower.show()
    $SelectedRewards/LabelLawnmower.show()

See also the print example.

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.