Howdy again, DB, $:^ J
The
return keyword is good stuff. Did you try reading about it in the manual? Either way, I'll give a quick sample.
You know how instance_create() works, right? It's a built-in function which creates an instance using these three arguments:
argument0 -> x
argument1 -> y
argument2 -> object_index
But, it doesn't simply create an instance and then leave you hanging. It also
returns the id of the newly created instance. This way you can go...
id_bullet = instance_create(x, y, o_bullet);
id_bullet.image_blend = irandom($ffffff);
...which stores the
returned id in a variable (id_bullet), then uses that variable to access that instance to change its image_blend to some random color.
Same with choose()
returning whatever random option and max()
returning the highest value.
You can create scripts that do this same thing -- scripts which
return a value!
Code:
/// value = scr_random_element(array);
var _arr_input = argument0,
_index_random;
if (!is_array(_arr_input) {
return undefined; // NOTE: the script STOPS running if it hits this line!
}
_index_random = irandom(array_1d_length(_arr_input));
return _arr_input[_index_random];
So now you can plug any (1D especially) array into your new script and it'll output some random value from it...
// after setting my_awesome_array, such as in a Create Event
my_new_something_something = scr_random_element(my_awesome_array);
if (scr_random_element(my_awesome_array) == BEANS) { /* do stuff */ }
draw_text(x, y, string(scr_random_element(my_awesome_array)); // which will show a new random element from the array every drawing frame
// etc. !
Is it clear after all this time, or was the example too abstract/whatever?
Bob