array_value_exists - check to see if a value exists in an array

descrubb

Member
Is there some already-in-place function that could replace this custom method? I couldn't find any in the 2.3 docs...

GML:
///@desc returns the number of times a value appears in an array
///@arg array    array to search in
///@arg value    value to search for
function array_value_exists( array, value )
{
    var length, count
    length = array_length(array)
    count = 0
    for (var i = 0; i < length; i++)
    {
        if typeof(array[i]) == typeof(value) && array[i] == value 
                count += 1
    }
    return count
}
 

samspade

Member
No, you have to do it your self as far as I can tell. I'd probably rename it as you're actually doing a count of values rather than just returning true or false and type of is only moderately helpful in GML as most different types are actually different enough that they can't be confused (e.g. array versus number versus string) and things which can be confused (e.g. object index and integers) don't have specific types. So if you're going to be doing this a lot it'd be worth it to just check if array == value.
 
Top