Skip to content

Instantly share code, notes, and snippets.

@katsaii
Last active September 20, 2019 19:45
Show Gist options
  • Save katsaii/7b4785d68e2689e5e66741681b7df083 to your computer and use it in GitHub Desktop.
Save katsaii/7b4785d68e2689e5e66741681b7df083 to your computer and use it in GitHub Desktop.
Pseudo properties using macros in GameMaker
#macro VAR "foo"
#macro GETTER variable_global_get(VAR)
#macro SETTER \
for (var __value = GETTER;; { \
if (is_real(__value) && __value > 0) { \
variable_global_set(VAR, __value); \
} else { \
show_error("Global property '" + VAR + "' must be a positive number", false); \
} \
break; \
}) __value
@katsaii
Copy link
Author

katsaii commented Sep 16, 2019

Examples

Getting and setting:

SETTER = 12;
show_message(GETTER); // prints: '12'

Setting an invalid value:

SETTER = 1.0;    // success
SETTER = "five"; // error

Using the addition assignment operator on the property:

SETTER = 2;      // value: 2
SETTER += 5;     // value: 7

@katsaii
Copy link
Author

katsaii commented Sep 16, 2019

You may also be able to create a getter-setter using the macro:

#macro GETSET GETTER; SETTER

with the limitation of not being able to use it 'inline' like the original getter.

In other words, the following is invalid code:

GETSET = 3;
show_message(GETSET);         // invalid token

since the macro GETSET will be transformed into:

GETTER; SETTER = 3;
show_message(GETTER; SETTER); // invalid token

which is obviously invalid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment