Need to display the MySQL data on game's screen (currently uses "echo" data from PHP code)

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

I have successfully used the advice on this YouTube video to send data and receive data from Godot to MySQL.

The code provided by the video sends the info from MySQL and is shown only in the output area of the Godot game engine. How can I get the output to show up on a label or the game’s screen?

My code is not much different than what was written in the video.

I have searched every idea I have. I need to be sent in the correct direction.

:bust_in_silhouette: Reply From: jgodfrey

Minimally, you need add to one or more Label controls into your scene and set itstext property to the data you received from MySQL. So, for example, if you have a child Label node named Label, you can set the text it displays on-screen like:

$Label.text = "my text value"

So, if you change my text value to something you received from MySQL, the label will display that instead.

Thank you, but I am not certain that I am asking the right question. I do not know what to put in “my text value” since I do not know how the information that the PHP code sends as : echo $string
is actually received by Godot. It looks like a complicated dictionary or array.
This is what shows in the Godot output area.

Response Body:
{“error” : “none”,
“command” : “get_scores”,
“response” : {“0”:{“username”:“[bbbbb]”,“score”:“1123”},“1”:{“username”:“”,“score”:“1123”},“2”:{“username”:“[fred]”,“score”:“1123”},“3”:{“username”:“[fff]”,“score”:“1123”},“4”:{“username”:“[ffff]”,“score”:“1123”},“5”:{“username”:“[fffff]”,“score”:“1123”},“6”:{“username”:“[dddd]”,“score”:“1123”},“7”:{“username”:“[gggg]”,“score”:“1123”},“8”:{“username”:“[bgfdd]”,“score”:“1123”},“9”:{“username”:“[bb]”,“score”:“1123”},“10”:{“username”:“[bbb]”,“score”:“1123”},“size”:11}}

I do not know how to capture this as a string of text or anything else that I can use to share the scores. The above matches my MySQL database, so that is working.

charsei1936 | 2023-02-27 20:37

Ah, OK - understood. A quick scan through the linked video makes me think that everything should be in pretty good order. And, since you’re getting the expected output (posted above), it should be pretty easy from there.

In the http_request_completed() callback function of the video, the server response is converted to a Godot Dictionary via the call to parse_json(response_body) and stored in the response variable. So, you can access any part of it as necessary using standard Dictionary access syntax.

So, based on your posted output, this (for example):

	print("%s: %s" % [response.response["0"].username, response.response["0"].score])

Should print:

[bbbbb]: 1123

jgodfrey | 2023-02-27 21:00

Further, based on the above, you can do things like:

$Label.text = response.response["0"].score

To put selected content into a Godot-based GUI.

jgodfrey | 2023-02-27 21:03

I want to thank you for your help. I am getting closer because of it. When I placed your “print” code in my game I got this error: “The identifier “response” isn’t declared in current scope”
So then I decided to be creative and add a variable “var response = {“0”:{“username”:”[cas]“,“score”:“1123”}… " matching what the PHP is sending. It didn’t work until I removed the “response.” from the code
print(”%s: %s" % [response[“0”].username, response[“0”].score])

This printed the data from the game code and I was able to add it to the label, just as you suggested. But of course, I placed the variable in the code and it is still not grabbing it from the output from the PHP (I proved that to myself).
I have two thoughts. One is that I notice the output response is slower than the buttons I push so maybe I need to pause to get it. But that does not explain the first error of the code you gave me stating “response: is not declared”.
Second thought is the PHP code that I am using echos more than just the “response”.
It sends this:
Response Body:
{“error” : “none”,
“command” : “get_scores”,
“response” : {“0”:{“username”:“[bbbbb]”,“score”:“1123”},“1”:{…

I am going try and have the PHP just send the response and see what happens. It may take a while to try this due to my schedule. But I am enjoying the journey.
Thanks again, I feel I am on the right track.
I guess I do have a question. For the following:
[response.response[“0”].username
why did you write “response.response” rather than just “response” and what is the difference between the two. One “response” is because of the name defined in the dictionary string, which one is that? Because of the initial error I tried to change one of them to a different word, that definitely was the wrong direction for me.

charsei1936 | 2023-02-28 13:49

So, in the video, the http_request() builds a Dictionary from the response in this line of code:

var response = parse_json(response_body)

… and stores it in a variable named response. So, that’s just a Godot Dictionary version of the JSON body that was returned by the request. Also in the video, the code is printing out the value of response_body (so the JSON).

That’s what’s generating this string you posted above.

Response Body:
{"error" : "none",
"command" : "get_scores",
"response" : {"0":{"username":"[bbbbb]","score":"1123"},"1":{"username":"[]","score":"1123"},"2":{"username":"[fred]","score":"1123"},"3":{"username":"[fff]","score":"1123"},"4":{"username":"[ffff]","score":"1123"},"5":{"username":"[fffff]","score":"1123"},"6":{"username":"[dddd]","score":"1123"},"7":{"username":"[gggg]","score":"1123"},"8":{"username":"[bgfdd]","score":"1123"},"9":{"username":"[bb]","score":"1123"},"10":{"username":"[bbb]","score":"1123"},"size":11}}

So, the response variable will contain the same content as above, just in Dictionary form.

Looking at that data, I see that it contains a key named response here:

"response" : {"0":{"username" ...

That’s the data I was accessing in my earlier post.

So, the below code is the response variable, followed by the response key contained within it:

response.response

That can also be written as response['response'] if it’s easier to read / understand.

From there, it’s jut a matter of specifying what elements you want to access, which should directly map to the structure of the Dictionary. Hopefully, you can see where this element is located:

response.response["0"].username

And, again, that could be written as:

response['response']['0']['username']

I will add that I haven’t watched the entire video, so it’s not clear to me whether the author has provided a mechanism to access the Dictionary data. I’m just (attempting to) provide an example of accessing it directly within the http_response() method.

jgodfrey | 2023-02-28 15:03

Thanks for your answer. Like I said before, you have definitely cleared up some of my issues, such that I can go in a better direction.

charsei1936 | 2023-02-28 15:36

$GSLabel07.text = (“%s: %s” % [response[‘response2’][comment6-“3”].username, response[‘response2’][comment6-“3”].score])

This what I ended up using. I had to change one of them to “response2” so I could follow the needed code changes and different error messages. I also had to place it in the correct location in the Godot code. That was why I got my initial error, after your first response.

In the end… ten usernames & scores show up in order on the screen.

I am working on making it look good and fixing other things. Then add it to the game and adjust as needed.

Your help is the only reason I could get this to work. I was stuck.

Many Thanks

charsei1936 | 2023-03-01 13:17