SEMilfred
4/23/2020 - 11:21 AM

Macros for "with each of" behaviour

A set of macros and scripts which allows for "with each of" behavior in GameMaker: Studio 2. Acompanied by an extension script/extension macros for further filtering.

/// @function     macros__each_of(...object_indexes)
/// @description  Test if the current id is either of the entered objects or instances.
/// @argument     ...object_indexes
/*
  The macros "each" and "of" allows for
  another with-esque loop type, where
  you loop through multiple kinds of
  objects and instances.
  
  Example of usage:
  with each of (obj_object1, instance1, _obj_object2) {
    // Your code here targeting the listed instances.
  }
  
  You can also use the keyword "not",
  as in "not of" to revert the lookup.
  
  Example:
  with each not of (obj_object1, instance1, _obj_object2) {
    // Your code here targeting all but the listed instances.
  }
*/

#macro each (all) if
#macro of macros__each_of

for (var i=0; i<argument_count; i++) {
  if (instance_exists(argument[i])) {
    // If same object_index, same id or of ancestry
    if (object_index == argument[i] || id == argument[i] || object_is_ancestor(object_index, argument[i])) {
      return true;
    }
  }
}
return false;
/// @function     macros__each_of__where_conditions(conditions)
/// @description  Return the outcome of the income.
/// @argument     conditions
/*
  The macros "where" and "condition/conditions" are
  extensions of "each of", allowing to filter instances
  by conditions. "condition" and "conditions" are the
  exact same deal, but both exists for more natural
  language usage.
  
  Basically, this is a gloryfied if-statement.
  
  Example of usage:
  with each of (obj_object1, instance1, _obj_object2) where conditions (x > 100 && y < 30) {
    // Your code here targeting the list of instances meeting the conditions.
  }
  
  with each condition (x > 100 && y < 30) {
    // Your code here targeting all instances meeting the conditions.
  }
  
  You can also invert the result of the 
  conditions using "not".
  
  // Example
  with each of (obj_object1, instance1, _obj_object2) where not conditions (x > 100 && y < 30) {
    // Your code here targeting the list of instances not meeting the conditions.
  }
*/

#macro where &&
#macro condition macros__each_of__where_conditions
#macro conditions macros__each_of__where_conditions

return argument[0];