• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Using json parse returns the values in different orders from time to time

Aaron Craig

Member
I'm working on a card game and have created a json file to import all the card details. The problem is the parsed json changes the order it comes in, seemingly at random. It seems like if I add any extra code into my project, anywhere, it may change it. It only seems to alternate between two values, not totally change, but I can't understand why.
Here's the parsing code:
GML:
//Read in the cards from the file in the dataFiles folder of project
if (file_exists(working_directory + "creatures.json")) {
    json = "";
    //Open the file
    file = file_text_open_read(working_directory + "creatures.json");
    //Read the entire file as one string
    while(file_text_eof(file) == false) {
        json += file_text_readln(file);
    }
    file_text_close(file);
    //Save the json in the global variable
    global.MasterCardList = json_parse(json);
}
Then I take those values and save them:

GML:
//Assign globals to the struct names
var array = variable_struct_get_names(global.MasterCardList[0]);

global.CardName = array[1]
global.CardType = array[4]
global.CardAttack = array[5]
global.CardDefense = array[6]
global.CardSpecies = array[7]
global.CardElement = array[8]
global.CardLevel = array[0]
global.CardAbilityText = array[2]
global.CardFlavorText = array[3]
I know I could build a sorting function for the returned values, but I thought this might be a language issue or bug. Has anyone else experienced this?
 

Attachments

FrostyCat

Redemption Seeker
Your decode result shows a struct at global.MasterCardList[0], not an array. Here's an example of what you should have done:
GML:
var card = global.MasterCardList[0];

global.CardName = card.Card_Name;
Starting with GMS 2.3.1, you could also do it like card[$ "Card_Name"].
 
Top