Quick answer: Data structure with index does not exist. means a ds_* function received an id that does not point at a live structure of that type: it was never created, was already destroyed, or belongs to a previous run of the game. Create the structure before any code uses it, destroy it in exactly one place, set the variable to -1 afterwards, and check ds_exists() where ownership is unclear. Arrays and structs avoid the problem entirely.

Lists, maps and grids in GameMaker are not values. They live in the runtime, and your variable holds a reference to them that keeps its value after the structure is gone. Every version of this error comes from that gap between the variable and the thing it once pointed at. The message itself changed in recent runtimes, so this guide starts with both wordings, then explains how the ids are allocated and recycled, since that explains the cases where a stale id does not crash and corrupts another system’s data instead. The fixes follow, with code for GameMaker 2024 and later.

The exact error

The message most people search for is the short one. Older runtimes print it (player reports of it run through 2023), and so do games still shipping on those runtimes:

___________________________________________
############################################################################################
ERROR in
action number 1
of  Step Event0
for object obj_stage_manager:
Data structure with index does not exist.
at gml_Script_spawn_mob
############################################################################################
gml_Script_spawn_mob (line -1)
gml_Object_obj_stage_manager_Step_0

On current runtimes, where data structure ids are typed handles, crash reports from players show a longer form that names the function and the type instead:

ds_list_size argument 1 invalid reference to (ds_list) - requested 2 max is 3
ds_map_find_value argument 1 invalid reference to (ds_map) - requested -1 max is 8
ds_grid_resize argument 1 invalid reference to (ds_grid) - requested 1 max is 1

When the value is not an id at all, the message is about the type:

ds_map_find_value argument 1 incorrect type (undefined) expecting a ds_map

Treat the first two forms as the same error. A ds_* function was handed a reference, looked it up, and found no live structure of its own type there. In the newer form, requested is the index your variable held. A value of -1 is the giveaway for a variable that was reset after a destroy, a failed json_decode(), or async_load read outside an async event. The third form means the variable held undefined, a string or some other non-id value.

How data structure ids work

Four facts from the GameMaker manual explain every case below.

Put together: a variable initialised to 0 as a placeholder, an id read back from a save file, and a handle kept after its structure was destroyed all look valid to your code. If nothing lives at that index you get this error, which is the good outcome. If something else has taken the index, there is no error. Your code reads and writes a structure that belongs to another object, and the symptom appears far from the cause: wrong items in an inventory, a quest list that contains enemy ids. That is why ds_exists() alone is not a complete defence: it answers whether something lives at that index, and cannot tell you whether it is yours.

What causes it

  1. The structure was destroyed and something still uses the id. Another instance kept a copy of the handle, or code after instance_destroy() in the same event touches it. The manual is explicit that the Destroy and Clean Up events run immediately and the rest of the calling event still executes.
  2. Used before it was created. A placeholder such as items = 0 or global.party = -1, with the real ds_list_create() in an event or object that has not run yet.
  3. Not recreated after a restart or room change. A global structure destroyed at Game End or Room End, with creation code that only runs once. Players describe this as “it crashes after I quit to the menu and start again”.
  4. Destroyed twice. Once in the Destroy event and again in Clean Up, or a nested list destroyed by hand after its parent map already destroyed it.
  5. An id loaded from a save file. Ids are not stable between runs. Saving the variable saves a number, not the contents.
  6. A failed json_decode() or a stored async_load. json_decode() returns -1 when the text is not valid JSON. async_load is a map only inside an async event and -1 everywhere else.
  7. The wrong kind of id. A map passed to a list function, or a list function called on a variable that now holds an array.

How to find which one is yours

Print the variable on the line before the failing call:

show_debug_message($"items = {items} ({typeof(items)}) alive = {ds_exists(items, ds_type_list)}");

To see what is alive across the whole game, debug_event("ResourceCounts") prints the number of live lists, maps, grids and other resources and returns the same data as a struct (listCount, mapCount, gridCount and so on). Call it on entering and leaving a room. A count that drops when you did not expect it points at an early destroy. A count that only climbs is a leak, covered in memory leaks from surfaces and data structures. In the debugger, add the variable to the Watches window: data structures can be expanded there to show their contents, which is the quickest way to notice that a list holds another system’s data.

Fix 1: one owner, one destroy, then -1

Decide which instance owns each structure. Only the owner destroys it, in the Clean Up event, which runs when the instance is destroyed, when the room ends and when the game ends. Then invalidate the variable:

// Create event
items = ds_list_create();

// Clean Up event
if (ds_exists(items, ds_type_list))
{
    ds_list_destroy(items);
}
items = -1;

Do not also destroy it in the Destroy event. The second call either raises this error or, if the index was reused in between, destroys a structure that belongs to something else.

Anything that borrows the handle must not outlive the owner, and must not use it after the owner is gone:

// Broken: the chest destroys its loot list, the window keeps drawing it
var _window = instance_create_layer(0, 0, "UI", obj_loot_window);
_window.loot = loot;
instance_destroy();

// Fixed: the window gets its own copy and owns it
var _window = instance_create_layer(0, 0, "UI", obj_loot_window);
_window.loot = ds_list_create();
ds_list_copy(_window.loot, loot);
instance_destroy();

In the fixed version obj_loot_window destroys its copy in its own Clean Up event. Watch for code that follows instance_destroy() in the same event too. Clean Up has already run by then, so move the destroy call to the last line or exit straight after it.

Fix 2: create it before anything can use it

Create the structure in the Create event of the object that owns it, not in a Step event behind a flag and not in another object. For structures the whole game shares, use one controller object that is created first, or a global initialised once in a script:

// Broken: any ds function called before setup runs gets the placeholder
global.party = -1;

// Fixed: the variable is never a placeholder
global.party = ds_list_create();

A placeholder of 0 is worse than -1. Index 0 is the first structure of every type, so ds_list_add(items, "sword") on a variable holding 0 succeeds as soon as any list exists and adds your sword to it. If you need “not created yet”, use -1 and guard the use:

if (!ds_exists(global.party, ds_type_list))
{
    global.party = ds_list_create();
}
ds_list_add(global.party, new_member);

Fix 3: rooms, restarts and persistence

game_restart() does not re-run code that initialises globals, and it does not clean up or recreate data structures. If a Game End or Clean Up event destroyed a global structure and the code that creates it sits behind an “only once” check, the second run starts with a dead id. Either leave global structures alone for the life of the process or create them in an event that runs on every start, such as the Create event of a controller in the first room.

Room changes cause the same thing on a smaller scale. A non-persistent instance is removed when the room ends and its Clean Up event destroys its structures. A persistent object, or a global, that still holds one of those handles fails in the next room. Persistent objects should own what they use, and anything passed across a room change should be copied.

Fix 4: saves, JSON and async events

Never write a handle to a save file. Write the contents, and build a new structure when loading:

// Saving
ini_open("save.ini");
ini_write_string("player", "items", ds_list_write(items));
ini_close();

// Loading into a list this instance already created
ini_open("save.ini");
var _saved = ini_read_string("player", "items", "");
ini_close();
if (_saved != "")
{
    ds_list_read(items, _saved);
}

The same warning applies to handles stored inside a struct or another list that you serialise. What gets written is a reference, and after a restart that index belongs to whatever was created at that point in the new run.

Check the result of json_decode() before using it, and destroy only the top-level map. The lists and maps it creates underneath are destroyed with it, as is anything you attached with ds_map_add_list(), ds_map_add_map() or the ds_list_mark_as_* functions:

var _map = json_decode(_text);
if (ds_exists(_map, ds_type_map))
{
    hp = _map[? "hp"];
    ds_map_destroy(_map);
}

Inside an async event, copy what you need out of async_load into your own variables. Storing async_load itself and reading it in a later Step event fails, because the variable is -1 again once the event is over.

Fix 5: use arrays and structs where you can

The manual now recommends arrays and structs over DS lists and maps: they have similar features and are garbage collected. A value that is collected when nothing refers to it cannot be destroyed early, destroyed twice or recycled under you. For most inventories, lookups and save data this removes the error for good.

// Before
inventory = ds_list_create();
stats = ds_map_create();
ds_list_add(inventory, "sword");
stats[? "hp"] = 100;
// plus a Clean Up event that destroys both

// After
inventory = [];
stats = { hp: 100 };
array_push(inventory, "sword");
// no Clean Up code
DS callArray or struct equivalent
ds_list_add(list, v)array_push(arr, v)
ds_list_size(list)array_length(arr)
ds_list_delete(list, i)array_delete(arr, i, 1)
map[? "hp"]stats.hp or stats[$ "hp"]
ds_map_exists(map, "hp")struct_exists(stats, "hp")
json_encode / json_decodejson_stringify / json_parse

Grids and priority queues are the structures worth keeping, since their region and ordering functions have no direct array counterpart. Grid coordinate mistakes produce their own messages, covered in ds_grid index out of bounds.

How to confirm the fix

Catching the state-dependent cases

These crashes depend on the order of rooms and restarts, so they tend to appear for players and not in a short test run. Bugnet’s GameMaker SDK installs an exception_unhandled_handler in bugnet_init() that records the message, the stack, the name of the room the game was in, the game version and the platform, then files the report on the next launch. The room name and the function named in the message usually identify which structure died. What no error handler can see is the silent version, where a stale id points at another live structure and nothing throws. Those arrive as player-written reports (“my inventory filled with the wrong items”), which is what the in-game form is for. Set up both from the getting started guide.

Frequently asked questions

What does Data structure with index does not exist mean in GameMaker?

A ds_list, ds_map, ds_grid or other data structure function was called with an id that does not refer to a live structure of that type. The structure was never created, has already been destroyed, or the id came from a save file or a previous run.

Why do I get this error after restarting the game or changing rooms?

Something destroyed the structure when the room or game ended, and the code that creates it did not run again. Globals are not re-initialised by game_restart, and persistent objects keep handles to structures that non-persistent instances destroyed in their Clean Up events.

Should I check ds_exists before every data structure call?

Use it at ownership boundaries, such as before destroying or lazily creating a structure. It is not a full defence, because index numbers are recycled: a stale id can point at a newer structure that exists but is not yours. Resetting the variable to -1 after destroying is the more reliable habit.

Can I save a ds_list id to a file and load it later?

No. The id is only meaningful in the process that created the structure. Save the contents with ds_list_write or ds_map_write, or as JSON, and read them into a newly created structure when loading.

Do arrays and structs have this problem?

No. Arrays and structs are garbage collected, so they stay alive while any variable refers to them and cannot be destroyed while in use. The GameMaker manual recommends them over DS lists and maps for that reason.

A crash is the lucky version of this bug. The same stale id, a few frames later, can point at a different live structure and corrupt it without any error, so set the variable to -1 in the same place you destroy.