GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2015-06-30 06:05:05

Threef
Member
Registered: 2015-06-30
Posts: 1

ds_list_find_ds / ds_list_delete_ds

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.

Last edited by Threef (2015-06-30 13:52:04)

Offline

#2 2015-07-01 00:15:23

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: ds_list_find_ds / ds_list_delete_ds

Thanks for contributing these.

This exposes a limitation of ds_list_find_index() I wasn't aware of. I might even consider it a GM:Studio bug.

In fact, it has recently been reported and assigned.

http://bugs.yoyogames.com/view.php?id=18014

This is a related issue concerning arrays.

http://bugs.yoyogames.com/view.php?id=17678

I'm going to wait a bit before adding these until we see where these bug reports go.


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB