You are currently viewing the GMLscripts.com static mirror. Forum access and script submissions are not available through this mirror.

Invert GMLscripts.com

select_relative_wrap

This is useful for sequentially selecting values from a list based on a current value.

//  Move forwards {+1} through a list of colors
color = select_relative_wrap(color, +1, c_red, c_orange, c_yellow, c_green, c_blue);

In the above example, if the current value of color is c_orange, then its new value would be set to the next value in the list, c_yellow.

//  Or move backwards {-1} through a list of colors
color = select_relative_wrap(color, -1, c_red, c_orange, c_yellow, c_green, c_blue);

If a relative position is beyond the range of given choices, the position "wraps around" to the other end of the list. In the second example, if the current value of color is c_red, then its new value would be set to c_blue.

If current value isn't among the choices, the returned value is undefined.

select_relative_wrap(current,delta,choice1,choice2...)
Returns an argument in a position relative to a given value.
COPY/// select_relative_wrap(current,delta,choice1,choice2...)
//
//  Returns an argument in a position relative to a given value.
//  If a relative position is beyond the range of given choices,
//  the position is wrapped until it is within range. If current
//  value isn't among the choices, the return value is undefined.
//
//      current     value matching a given choice
//      delta       relative position of desired choice, integer
//      choiceN     value to return, if selected
//
//  eg. select_relative_wrap("Name", -2, "Hello", "Doctor", "Name") == "Hello"
//      select_relative_wrap("Name", 2, "Hello", "Doctor", "Name") == "Doctor"
//
/// GMLscripts.com/license
{
    var current = argument[0];
    var delta = argument[1];
    var size = argument_count - 2;
    var choices = ds_list_create();
    for (var i = 2; i < argument_count; i++) ds_list_add(choices, argument[i]);
    i = ds_list_find_index(choices, current);
    if (i < 0) return undefined;
    i = (((i + delta) mod size) + size) mod size;
    var result = ds_list_find_value(choices, i);
    ds_list_destroy(choices);
    return result;
}

Contributors: xot

GitHub: View · Commits · Blame · Raw