redrc Im trying to understand whats not working? If you say the save file is being created, does it record the score values?
So the problem is reading values?

Can you maybe post minimal reproduction project that we could run and test this particular piecie of code?

create .zip and upload it here. just delete .godot dir from that zip.

redrc still no luck

You need to add print statements, or use the debugger and set breakpoints and inspect variables, so that you can determine exactly which lines of code are working or not working.

Posting a picture of some code and saying that it doesn't do what you want is not useful.

redrc Yes the file exists, and its time stamp modified/updated (per func _ready> save, as expected, -& not without it).

Then I am not sure what the problem is. How big is your file and did you open it and look inside it? And did you try printing the values that you loaded from the file or looking at them in the debugger?

Also what values are you trying to save? Be aware that store_8() and store_16() only store 8 and 16 bit values and your values must not be outside that range. Also, if you store a value like "0" or "3" and try to open the file with a text editor then you will probably not see anything, because those would be unprintable ASCII characters. So you have to use a hex editor to check your file.

Try saving a simple string with store_string() just to see what happens or try using store_var().

Hey Guys,
I fixed it. It was actually saving to file - But saving "Zero" (0) instead.
I was following a You Tube tut, and had HUD on Canvas item.
FileAccess does not inherit this-so I Moved it to Control child node- it worked!

The variables GlobalsVariables.high_score and GlobalsVariables.life_score were both zero?

  • Edited

YES-- both. Even with Lifescore tested up to 128.

Screen shot below shows: Despite HUD showing high_score 22, the debug showed saved as zero.

..
Just to explain: I even added buttons to test manually saving and loading score .
Because there was some conflict upon returning to this transition page (between levels) _onready would reload the old high-score, negating any advance made in level.

  • Edited

NOW BACK TO MAIN TOPIC- SAVING TO WP DB:

I FOUND THE FOLLOWING SNIPPETS (AND ADAPTED BETWEEN THEM any missing code):
I HAVE INCLUDED PERHAPS SOME DUPLICATION FOR USERNAME - BECAUSE I WOULD LIKE TO DISPLAY WP USERNAME IN GAME PLAY:
-TO SEE WHICH IS BETTER- CAN WE REFINE THIS ...

GDScript:

`var dataText = "testData"
var score = 10
var dict = {name: dataText,score: 10 }
var http_request = HTTP Request.new()
add_child(http_request)

func _ready():
http_request.request_finished.connect( _on_request_finished)

func _on_button_pressed():
http_request.request(
"https://mysite/wp_json/wp/v2/users/me"
)
_make_post_request(myurl, dict, false)
print("data sent")

func _make_post_request(url, data_to_send, use_ssl):
var query = JSON.print(data_to_send)
var headers = ["Content-Type: application/json"]
$HTTPRequest.request(url, headers, use_ssl, HTTPClient.METHOD_POST, query)

func _on_request_finished(result, error_string):
if result == OK
var data = JSON.parse( http_request.get_data().get_string())
var username =data["username"]
$Label.text = "username: " +username
else:
$Labe.text = "Error = " +error_string`
.

THEN SERVER_SIDE PHP Script:

<?php
include 'DatabaseConfig.php' ;
$json = file_get_contents('php://input');
$_POST = json_decode($json);
//echo $_POST;
$con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);
$name = $_POST['name'];
$score = $_POST['score']
$Sql_Query = "insert into game_test (Name, score) values ('$name', '$score')";
if(mysqli_query($con,$Sql_Query)){
echo 'Data Submit Successfully';
}
else{
echo 'Try Again';
}
mysqli_close($con);
?>

  • Toxe replied to this.
    4 days later
    • Edited

    Hi Toxe,
    Yes of course.
    As part of it, WP core files are pretty secure against SQL injection. On top of that, on WP I have Wordfence plugin- it seems to stop an incredible number of attacks ~ over 50 attacks a day are blocked! They all try to log in fake IDs . Upon failing with frustration, some try & send me loaded email links- malware, ransomeware, which I recognize with mouse-hover, and delete. Also WP verifies if the user is email verified & logged in; and user permissions attributes.

    But you are right, I think with HTTP requests on Godot we need more. (which is another reason why I say to use internal php query rather than http requests on the database.)

    Reading through the 1st Link you suggested, I'm a little lost- They suggest using "mysqli_real_escape_string() function.", but I don't see it in their example code. How /where do I write this?

    Also, WP script is different syntax. I'm compiling code which I will put up soon for comment.

    7 days later

    The 1st thing , I need to capture (and display) the user name. Let's try the local machine 1st. The following code from tuts i'm following gives me an error:- What should I add? The Get statement?as in get_data().get_string()) . Any complete tuts on this?

      redrc The string you're passing to parse_string() is not valid json.

      redrc why you need json to parse a string into json? its like cooking egg on a sun and watering it with a garden hose so that it wont burn.