GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 Script Submission » ds_list_find_ds / ds_list_delete_ds » 2015-06-30 06:05:05

Threef
Replies: 1

GM:Studio lets us nest data structures in each other, but got hard time finding them inside ds_lists, which sometimes is needed while working with JSON.

Expand/// ds_list_find_ds(list id, ds id)
//
//  Finds nested data structure inside ds_list
//  Usefull when working with JSON
//
//  Sometimes you want to hold one data structure in another.
//  In this example in ds_list. it works fine, you just save its id.
//  Problem is when you got id of nested structure and want to find it inside list
//  ds_list_find_index() returns -1, but we can compare every value in list to id
//
//      list id     id of ds_list, real
//      ds id       id of any data structure saved inside ds_list, real
//
//  Returns position or -1
//
/// GMLscripts.com/license

var m=argument0;
var r=-1;
for (var i=0; i<ds_list_size(m); i++) {
    if(m[|i]==argument1) {
        r=i;
        break;
    }
}

return r;
Expand/// ds_list_delete_ds(list id, ds id)
//
//  Finds and deletes nested data structure inside ds_list
//  Usefull when working with JSON
//
//  Sometimes you want to hold one data structure in another.
//  In this example in ds_list. it works fine, you just save its id.
//  Problem is when you got id of nested structure and want to find it inside list
//  ds_list_find_index() returns -1, but we can compare every value in list to id
//
//      list id     id of ds_list, real
//      ds id       id of any data structure saved inside ds_list, real
//
//  Returns true on success
//
/// GMLscripts.com/license

var m=argument0;
var r=false;
for (var i=0; i<ds_list_size(m); i++) {
    if(m[|i]==argument1) {
        ds_list_delete(m, i);
        r=true;
    }
}

return r;

ds_list_delete_ds() can be changed to delete only one position if structure is found more than one time by adding break.

Board footer

Powered by FluxBB