Skip to content

Instantly share code, notes, and snippets.

@katsaii
Created September 14, 2019 00:24
Show Gist options
  • Save katsaii/50eade77baafe32186f3985136eebcb6 to your computer and use it in GitHub Desktop.
Save katsaii/50eade77baafe32186f3985136eebcb6 to your computer and use it in GitHub Desktop.
A utility for enforcing some singleton prefix on objects in GameMaker. The default prefix is "the", i.e. `the_singletonName`.
gml_pragma("global", @'
// search for singletons
global.singletons = ds_list_create();
for (var i = 0; object_exists(i); i++) {
if (object_prefix(i) == SINGLETON_PREFIX) {
ds_list_add(global.singletons, i);
// automatically insert singleton into the first room
object_set_persistent(i, true);
room_instance_add(room_first, 0, 0, i);
}
}');
#macro SINGLETON_PREFIX "the"
/// @desc Returns whether an object is a singleton
/// @param obj {Integer} The id of the object to check.
/// @author Kat @katsaii
if (is_real(argument0) && !object_exists(argument0) &&
instance_exists(argument0)) then argument0 = argument0.object_index;
return ds_list_find_index(global.singletons, argument0) != -1;
/// @desc Returns the object prefix
/// @param obj {Integer} The id of the object to check.
/// @author Kat @katsaii
var obj = argument0;
if (is_real(obj) && !object_exists(obj) &&
instance_exists(obj)) then obj = obj.object_index;
var name = object_get_name(obj);
var pos = string_pos("_", name);
return pos == -1 ? "" : string_copy(name, 1, pos - 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment