Invert GMLscripts.com

select_index

Returns the index of an argument matching a given value.

//  Check the last keyboard character
value = keyboard_lastchar;

//  Match the character to an index.
//  eg. "A" matches the first choice, index 0,
//      "B" matches the next one, index 1, etc...
index = select_index(value, "A", "B", "G", "O");

//  Use the index to select a choice.
//  eg. "B" was pressed, index 1, which selects "Banana"
choice = select(index, "Apple", "Banana", "Grapes", "Orange");
show_message("You selected: " + choice);

In the above example, an item is selected based on the last keyboard key pressed.

select_index(value, choice0, choice1, choice2, ...)
Returns the index of an argument matching a given value.
COPY/// @func   select_index(value, choice0, choice1, choice2, ...)
///
/// @desc   Returns the index of an argument matching a given value.
///         If value matches the first choice, 0 is returned.
///         if value isn't among the choices, undefined is returned.
///
///         eg. select_index("two", "zero", "one", "two", "three") == 2
///
/// @param  {any*}      value       value to find
/// @param  {any*}      choiceN     values to search
///
/// @return {real}      index of selected choice
///
/// GMLscripts.com/license

function select_index()
{
    var value = argument[0];
    var choices = ds_list_create();
    var result = undefined;
    for (var i = 1; i < argument_count; i++) ds_list_add(choices, argument[i]);
    i = ds_list_find_index(choices, value);
    if (i >= 0) result = i;
    ds_list_destroy(choices);
    return result;
}

Contributors: xot

GitHub: View · Commits · Blame · Raw