Data structure with index does not exist

W

William Lundin

Guest
Hello, so i am new to GML and i am trying to load data from a JSON file into game maker and save that data as variables. I am able to load the data from JSON file, but unable to use the variables?? when i try to add the variables into a ds_list for example, it then throws the error saying "Data structure with index does not exist", and i do not know why. Printing out to the console before works, and it clearly prints the values im loading in, so why is it that i can print out to console using a variable name but not add it to a list?

my code:
Code:
var json = json_decode(stringg);
var list = ds_map_find_value(json, "Quests");

for(var i = 0; i < ds_list_size(list); i++)
{
var map = ds_list_find_value(list, i);

var ids = ds_map_find_value(map, "id");
show_debug_message(ids); //this prints out perfectly normal, '2' as from the json file

ds_list_add(QuestList(), ids); // If i comment out this line, i get no error on load, but if i leave it
//like this, then it gives error "Data structure with index does not exist" and points to ids variable.
}
 

Attachments

Last edited by a moderator:

FrostyCat

Redemption Seeker
If QuestList is a variable, get rid of the () that you put after it. If it is an actual script, make sure its return value is really that of a valid list ID.

In any case, stop posting code in plain text and screenshots. The correct form is to do it between [code] and [/code] tags.
 
W

William Lundin

Guest
If QuestList is a variable, get rid of the () that you put after it. If it is an actual script, make sure its return value is really that of a valid list ID.

In any case, stop posting code in plain text and screenshots. The correct form is to do it between [code] and [/code] tags.
Okay sorry, this is my first post so i did not know. Also, adding a return statement for QuestList script made it work. I do not know why, because in all my other scripts, i use QuestList() and that have worked fine all the way up to this point : O
 
W

William Lundin

Guest
Nothing is being added to the list if i add an return to the QuestList script. Inside that script, i only have a

Code:
var questjournal = ds_list_create();
return questjorunal;
that return satement makes all my other scripts with add functionality not add the items to the list, so leaving it only as

Code:
var questjournal = ds_list_create();
works for everything except the code posted in main question.
 
Last edited by a moderator:

FrostyCat

Redemption Seeker
Okay sorry, this is my first post so i did not know. Also, adding a return statement for QuestList script made it work. I do not know why, because in all my other scripts, i use QuestList() and that have worked fine all the way up to this point : O
They worked up until this point because that list happened to have an ID of 0 (i.e. no other lists exist until that point), and VM exports return 0 by default if you don't include an explicit return statement. Now that you started working with multiple lists, your luck is out and the runner is calling you out.
Nothing is being added to the list if i add an return to the QuestList script. Inside that script, i only have a

Code:
var questjournal = ds_list_create();
return questjorunal;
that return satement makes all my other scripts with add functionality not add the items to the list, so leaving it only as

Code:
var questjournal = ds_list_create();
works for everything except the code posted in main question.
Have you traced out what you're doing with this? If you did, you should know that you are creating multiple brand new lists, adding a single quest ID into each and then losing the reference to them on purpose (i.e. memory leak).

If you intend to have a single "quest journal" across your entire project, at the beginning of the game you should set up a global variable holding that list:
Code:
global.questjournal = ds_list_create();
Then add quest IDs onto that when loading from the JSON file, don't keep creating new ones by calling your QuestList() script on loop. Data structures are NOT garbage-collected in GML.
 
W

William Lundin

Guest
They worked up until this point because that list happened to have an ID of 0 (i.e. no other lists exist until that point), and VM exports return 0 by default if you don't include an explicit return statement. Now that you started working with multiple lists, your luck is out and the runner is calling you out.

Have you traced out what you're doing with this? If you did, you should know that you are creating multiple brand new lists, adding a single quest ID into each and then losing the reference to them on purpose (i.e. memory leak).

If you intend to have a single "quest journal" across your entire project, at the beginning of the game you should set up a global variable holding that list:
Code:
global.questjournal = ds_list_create();
Then add quest IDs onto that when loading from the JSON file, don't keep creating new ones by calling your QuestList() script on loop. Data structures are NOT garbage-collected in GML.
Ah alright thanks! Ye that totally works. Appriciated.
 
Top