This only works if the enum being referenced was created before the enum that is using it in an expression, but it will not work for variables or functions, since the enum value must be able to be evaluated as a constant when the project is Compiling. Notice in the above example we use another enum to create an expression for "violet". You can also assign values to the enum variables at the time of creation: The enum entries can only be integer numbers or expression s with previous enums that evaluate to an integer number, and by default are numbered from 0 upwards, so our example given above would default to red = 0, orange = 1, yellow = 2, etc. In the following example, we create an enum for the colours of the rainbow and assign it various constants and default values: EnumsĪn enum is an "enumerator", and it essentially permits you to create your own limited data type with a list of constant values, and they have the following structure: Note that you cannot have any white-space between the colon : and either the config name nor the macro name otherwise you will get an error. #macro iOS:ad_id ""Īs you can see, you give the config name first then a colon : and then the macro name and value. For example, say you have a configuration for Android Ads and another for iOS Ads, then you could define a single macro to hold the required app ID value: One very important feature of macros is that they can be defined for use with specific Configurations (configs), meaning you can have the same macro name but give it different values based on the currently selected config. This is purely cosmetic, in that splitting a macro like this will have no effect over the result of the final macro when used, and is simply to provide support for multi-line text on macros that have longer lines of code. #macro hello show_debug_message("Hello" \ It is worth noting that you can also split macros over multiple lines using the \ character to show where the line breaks. Using this code will make the image blend a different colour every time the macro is used. You would then call this macro something like this: By setting the macro to a function it means that this function will be called every time you use the macro. If you need the value of a macro to change at run-time then you should probably make it a global variable, since these can be changed from code during a game, unless you set the macro to be a runtime function. It will be easier to organise and debug later! You can define a macro anywhere in your code or in a script and it will be pre-compiled and included in your game as if it was there from the start, but we recommend that you create a dedicated script asset and define all your macros in there. Note that you would not be able to change the constant's value, so code like this will cause the game to crash: You could then use this in your code like this: Removing the equal sign and colon will make it a valid macro definition. Doing so will cause your macro definition to become invalid.įor example, this is not the correct way to define a macro: You cannot use an equal sign (like in variables) or put a semicolon at the end (like in a regular statement). The syntax shown above must be used correctly to define macros. The syntax structure for a macro is as follows:įor example, say you define the following macro " total_weapons": Basically, a macro is a named variable that holds an expression. You can define your own macros using the Script Editor and then use them in your code and actions as if they were regular variables, with the one difference being that they can't be changed in the game. While not exactly variables, macros are similar to them in how they are used, i.e.: they are named values that you can use throughout your code to replace hard-coded values. These are outlined on the appropriate pages for the runtime functions that require them in the GML Reference section. NOTE The GameMaker Language also has a number of built-in constant values that are used to identify specific things. Also note that any value that is always the same is classed as a constant, regardless of the data type, for example, a string or the number 3. In the GameMaker Language there are two types of user-defined constant: macros and enums, both of which are explained below. This makes them ideal for holding values that are used throughout the game to identify special data. In fact, constant values cannot be changed after they have been declared. A constant is a type of variable that is set once at the start of the game and then never changes.
0 Comments
Leave a Reply. |