How to convert variables into an array.

0 favourites
  • 9 posts
From the Asset Store
Color Variable
$4.50 USD
10% off
Easily store, modify, read and manipulate colors with Color Variables!
  • I would like to use AJAX to pass in-game data to PHP and save it in a database.

    However, I don't know how to structure the in-game data as an array.

    The ideal format would look something like this:

    {"score":{"id":1,"position_x":"0","position_y":"0","boss_counter":"0"}}

    I tried implementing it as shown in the attached image, but the variables are not being populated correctly (debugging shows them as empty).

    Additionally, I am unable to set custom names for the keys (only numbers starting from 0 are used).

    If anyone knows how to solve this, I would appreciate your help!

    Tagged:

  • Hi. It takes too long to explain, you haven't even learned how to use an array.

    You can pass an array structure or just collect it into a list and parse it after receiving it via tokenat.

    But all of this is a bad idea, storing player data on your server in a database is a bad idea, once the players are more than 100 thousand begin huge problems.

    I advise you to use ready-made solutions, such as firebase, here is an example.

    kibaga.org/c3-example/file_uploads/index.php

  • As I explained in your previous post, that data format is JSON, not an array.

    So you need to use JSON object.

  • Thank you for your response.

    I apologize for asking so many basic questions due to my lack of knowledge.

    I will look into how to use Firebase with Construct.

    Regarding JSON, I have a question.

    When I added variables to JSON as shown in the example you provided, the numbers all turned into 0.

    Specifically, it looks like this:

    json

    {"score_table":{"id":1,"position_x":"0","position_y":"0","boss_counter":"0"}}

    All values after id are written as 0.

    However, the variable position_x is not actually 0.

    I confirmed that a number was correctly assigned to position_x at the stage when it was being set into the JSON.

    Yet, it still appears as 0 in the JSON output.

    Additionally, username (String type) is not written in the JSON either.

    Do you know what might be causing this?

    Thank you as always!

  • When I added variables to JSON as shown in the example you provided, the numbers all turned into 0.

    Additionally, username (String type) is not written in the JSON either.

    Where do you see this? Check in the debugger, I bet the values in JSON are all correct.

    Maybe your PHP script expects text values instead of numbers? In this case you need to use str(variable) to convert numbers to text.

    JSON set "keyname" to str(variable)

    Also in your first comment the parent key in JSON was "score", but now you say it's "score_table"

  • You can just send data by collecting it in a string, that's how I used to do it.

    The screen shows the POST request and the server script.

    Nowadays I would suggest encrypting the data before sending and storing it on the server in encrypted form. But it will do for an example.

  • Show ekitabi1129's deleted post

    Both of you, thank you very much for your detailed responses.

    Where do you see this? Check in the debugger, I bet the values in JSON are all correct.

    Regarding this, as shown in the attached image, I tried displaying it as text on the screen.

    As a result, it shows:

    {"score_table":{"id":1,"position_x":"0","position_y":"0","boss_counter":"0"}}

    I have a thought about why the parent key name in the JSON is score_table, as in the above result.

    Before running this code, I parsed a JSON string named "score_table" using

    Parse JSON string AJAX.LastData.

    Could this be having an effect?

    By the way, when I input set text to position_x to display the text, it correctly shows "2025".

    Apologies for the repeated beginner-level questions.

  • Both of you, thank you very much for your detailed responses.

    Where do you see this? Check in the debugger, I bet the values in JSON are all correct.

    Regarding this, as shown in the attached image, I tried displaying it as text on the screen.

    As a result, it shows:

    {"score_table":{"id":1,"position_x":"0","position_y":"0","boss_counter":"0"}}

    I have a thought about why the parent key name in the JSON is score_table, as in the above result.

    Before running this code, I parsed a JSON string named "score_table" using

    Parse JSON string AJAX.LastData.

    Could this be having an effect?

    By the way, when I input set text to position_x to display the text, it correctly shows "2025".

    Apologies for the repeated beginner-level questions.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • It's hard to tell what's happening without seeing the entire project. The function on your screenshot should definitely create "score" key. If you previously parsed another data with "score_table", then the JSON should contain both keys:

    {"score_table":{"id":1,"position_x":"0","position_y":"0","boss_counter":"0"}, "score":{"id":1,"position_x":"0","position_y":"0","boss_counter":"0"}}

    If this doesn't happen, maybe the function doesn't work? Or another AJAX request immediately overwrites JSON with the old data from the server? I can only guess.

    Learn to use the debugger tool (Shift+F4). Temporarily disable the loading function and test that the saving function works - check the contents of JSON in the debugger.

  • Thank you for your response.

    When I checked using the debugger tool, it was displayed just as in the example you provided.

    The issue has been resolved.

    Thank you so much for your help this time!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)