[Solved] How to sort a variable into a structure

I’ve started working recently on a mmo project. I designed the project to be very minimal in resource consumption on the server side. I made a database and use network extension to GET/POST to php pages that contain the functions to manipulate the database. What makes it possible is that the client (the gdevelop game) only queries the server when necessary. It loads player stats on startup, and player can save to the database to update whatever they’ve accomplished while playing. Otherwise the game only contacts the server every 5 minutes to sync: a sort of autosave - so it is not a large use of data from people contacting the server constantly. The client keeps running on the players device and anything acquired can be added to the database during the next sync.

Here’s where my question comes in: I am able to post data to the database in bulk: I can send all the players stats in a string expression and update in one query during the 5 minute sync interval. I haven’t yet found a way to retrieve data in bulk. I have to retrieve each variable one request at a time when loading the stats at the start of play. The requests are stretched out over a few moments to minimize server impact. It would be much more efficient if I could get all the data in 1 request to cut down on server requests. But I’m not really sure how to take the response from the server which would be in the form of something like:

“id=1 row1=something1 row2=something2” and sort that into:
var1=something1
var2=something2

I’ve tried to google but I haven’t been able to find the right search parameters to give me what I’m looking for: I have found some documents about converting a json file, which might be a possible solution, but it would require an extra step of saving the server response into a json file and then converting it into a structure. I haven’t entirely disregarded this idea, just wondering if it’s the most efficient solution. I’ve also seen some older discussions using c++ which I guess is from the older version of gdevelop so I mostly disregarded.

So I guess what I’m asking, even if it may be simple to some of you, is how do I go about this? What terminology can I put in google will bring up a proper example or code fragment to build this from? Any suggestion about the best way to proceed would be helpful.

You don’t need to actually save a JSON file, you just need to the request to return data in a JSON format.

For example, I have a game which sends a POST request to the server along with an email address that the player types in, and the server returns something like:

{“status”:“success”,“hasPlayedToday”:“no”,“isWinner”:“no”,“msg”:“this user is not today’s winner”,“r1”:0,“r2”:0,“r3”:3}

Under the network action ‘Send a request to a web page’ the last item is ‘Reponse scene variable’, which will capture that resonse into a variable named whatever you type in this field. In my case I just typed in ‘json’ (without the quotes.)

At that point you’ve got a variable containing a JSON string, which you’ll then want to parse using the “Convert JSON to a scene variable” function, again under network (right over the send a request action.)

Then you should have access to all the variables that were received from the server. You can get at them like this:

bbbbbbb

Here’s I’m changing global variables based off the now parsed JSON string. As per the above example, I’ve now changed the global variable r1 to 0, r2 to 0, and r2 to 3. “returnedVars” is what we’ve called the parsed JSON variable, followed by a . then the name of what we want to retrieve specifically.

So you’d just need to change your server to return the information in a JSON format like:

{“id”:“1”,“row1”:“something1”,“row2”:“something2”}

And it’s pretty easy to work with as I’ve hopefully explained.

1 Like

That’s very helpful; it’ll take me some time to implement a testing model.

Another tip: you can send a json array back, and it will be converted in a structure with number counting up as keys. That way, with a while event, a variable incrementing on each iteration and the Count children in structure expression, you can do an action on every item in it. That way you can work with an arbitrary amount of data.

1 Like

I am happy to report that I have gotten this operation working. It’s a very rough example, but you can see the top row with the last update request. Beneath that the update timer.
Then the json string of stats recently pulled from the server, and lastly the individual variables sorted from the last server request.

Screenshot%20from%202020-11-13%2014-44-34

I had to look up some tutorials on the proper syntax/structure for a json string but these were simple enough to find with the right terms. This is much more efficient.

The idea of using an array and sorting the data is something I will keep in mind for later. I may implement areas of the game that are pvp or co-op using the multiplayer extension, and that method of using an array could be very useful in a situation with multiple players on the same map.