GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2021-05-22 23:44:29

felres
Game Designer
From: CR
Registered: 2021-05-22
Posts: 4
Website

Merge 2 lists, return 1. ds_list_merge(a,b)

Hi, this is a self explanatory script. Takes 2 list data strucutures and returns 1. Thanks.

Expand/// ds_list_merge(a,b);
//
//      Returns a list with values from both lists. Will always destroy list a
//
//      a       list, to be destroyed
//      b       list, to be returned
//
//
/// GMLscripts.com/license
{
    var a,b;
    a = argument0;
    b = argument1;
    
    if !ds_exists(a,ds_type_list) && !ds_exists(b,ds_type_list)
        return b;
    else if ds_exists(a,ds_type_list) && !ds_exists(b,ds_type_list)
        return a;
    else if !ds_exists(a,ds_type_list) && ds_exists(b,ds_type_list)
        return b;
    else
    {
        for( var i = 0; i < ds_list_size( a ); i++ )
        {
            ds_list_add( b,  ds_list_find_value( a, i ));
        }
        ds_list_destroy(a);
        return b;
    }
}

Offline

#2 2021-05-25 14:06:42

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

Re: Merge 2 lists, return 1. ds_list_merge(a,b)

Thanks for the submission and welcome to the forum. I'm somewhat surprised this doesn't already exist here. Perhaps someone else has submitted something like this in the past but it was never added.

I would make some changes to this. I don't feel it quite does what it suggests. It actually inserts the first list into the second list, changing it. That could be useful but that's an important side-effect. Also, the list that is returned is inconsistent. Usually, it's the second list but sometimes it's the first. It makes some sense but it doesn't sit right with me.

In general, I think it would be more useful, more predictable, and less dangerous if the script/function returns an entirely new list. Let the user decided what to do with the original lists after calling this script. If either of the lists is invalid, let the script fail and produce an error. The user is probably doing something wrong and the script should not obscure that.


Abusing forum power since 1986.

Offline

#3 2022-12-16 00:47:36

felres
Game Designer
From: CR
Registered: 2021-05-22
Posts: 4
Website

Re: Merge 2 lists, return 1. ds_list_merge(a,b)

xot wrote:

I'm somewhat surprised this doesn't already exist here. Perhaps someone else has submitted something like this in the past but it was never added.

Right? I`ve actually wondered why the scripts have not been updated in so long!

Offline

Board footer

Powered by FluxBB