Skip to content

Instantly share code, notes, and snippets.

@katsaii
Created July 1, 2020 13:43
Show Gist options
  • Save katsaii/21f9fd53f1772efb54716d42c821f228 to your computer and use it in GitHub Desktop.
Save katsaii/21f9fd53f1772efb54716d42c821f228 to your computer and use it in GitHub Desktop.
Handles the creation of controller objects and parents, offering runtime errors in situations where there could be strange behaviour.
/* Singleton object utilities
* --------------------------
* Kat @Katsaii
*/
#macro __instance_create_layer_internal instance_create_layer
#macro __instance_create_depth_internal instance_create_depth
#macro instance_create_layer instance_create_checked_layer
#macro instance_create_depth instance_create_checked_depth
#macro PREFIX_SINGLETON "the"
#macro PREFIX_ABSTRACT "par"
#macro PREFIX_CONTROLLER "con"
/// @desc Returns the prefix of an asset name.
/// @param {string} str The asset name to find the prefix of.
function asset_get_prefix(_str) {
var pos = string_pos("_", _str);
var prefix = pos == -1 ? "" : string_copy(_str, 1, pos - 1);
return prefix;
}
/// @desc Returns whether a new instance of this object is safe to create.
/// @param {real} obj The object index to check.
function object_is_safe(_obj) {
var name = object_get_name(_obj);
switch (asset_get_prefix(name)) {
case PREFIX_SINGLETON:
if not (instance_exists(_obj)) {
// singleton doesn't exist, so we can create it
break;
}
case PREFIX_ABSTRACT:
// parent objects should not be created at all
return false;
}
return true;
}
/// @desc Throws an error if this object is not safe to create.
/// @param {real} obj The object index to check.
function object_must_be_safe(_obj) {
if not (object_is_safe(_obj)) {
throw "\nit is unsafe to create a new instance of this object! (" + object_get_name(_obj) + ")";
}
}
/// @desc Wrapper for `instance_create_depth` which checks whether the instance is able to be safely created.
/// @param {real} x The x position.
/// @param {real} y The y position.
/// @param {real} depth The depth to create the instance at.
/// @param {real} obj The object index to create an instance of.
function instance_create_checked_depth(_x, _y, _depth, _obj) {
object_must_be_safe(_obj);
return __instance_create_depth_internal(_x, _y, _depth, _obj);
}
/// @desc Wrapper for `instance_create_depth` which checks whether the instance is able to be safely created.
/// @param {real} x The x position.
/// @param {real} y The y position.
/// @param {real} layer_name_or_id The layer to create the instance on.
/// @param {real} obj The object index to create an instance of.
function instance_create_checked_layer(_x, _y, _layer, _obj) {
object_must_be_safe(_obj);
return __instance_create_layer_internal(_x, _y, _layer, _obj);
}
for (var i = 0; object_exists(i); i += 1) {
var name = object_get_name(i);
switch (asset_get_prefix(name)) {
case PREFIX_SINGLETON:
room_instance_add(room_first, 0, 0, i);
case PREFIX_CONTROLLER:
object_set_persistent(i, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment