

GAMEMAKER STUDIO 2 ARRAYS CODE
The code then creates a method to be used in the array_sort() function that returns the difference between each of the X positions of the instances in the room. The debug message will show something like the following: The above code will create an array of 10 instances placed at random positions within the room. _a = instance_create_layer(_xx, _yy, layer, obj_Bullet) The sorting function takes the two array elements as elm1 and elm2 and performs a subtraction on them. If elm1 is greater than elm2, the subtraction results in a positive number which pushes elm1 to be after elm2, hence putting them in an ascending order. The above code uses a custom sorting function to sort the array in an ascending order.

The sort type ( true for ascending or false for descending) or a function reference to use for sorting. All other data types will be sorted based on their numerical value, the exact values of which will depend on the data type itself (for example, an array of buffers would be sorted based on the numerical value of their memory addresses).Īrray_sort(variable, sorttype_or_function) Argument If the array contains a set of strings, then the strings will be sorted alphabetically based on the English alphabet when using the default ascending/descending sort type. Floating point values less than 1 will be read as 0, so you must use sign() or round() if your function works with floating point values. NOTE The value returned by your custom function must be an integer.

You supply the array index value and the function will return an integer value representing the number of entries the array contains. To create a sorted copy, use array_copy() to duplicate the array first, and then pass that into this function. With this function you can get the length (number of entries) of an array dimension. This function will modify the contents of the original array that was supplied.
