• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Macro Arrays Question

D

Daniel Cook

Guest
Hey everyone,

I am working on an item system in a game and have run into a bit of a snag.
The jist of it is this, i have created an Enum to hold a Int value for every item within my game:
enum ItemIndex{
gun = 0,
sword = 1,
healthPotion = 10,
ect...​
}

Then i held specifice data abount my items within an array. then by using there Enum value i can get the relivent data from the array.

stackCapacity[ItemIndex.gun] = 1;
stackCapacity[ItemIndex.sword] = 1;
stackCapacity[ItemIndex.healthPotion] = 5;

So that works just fine, however I recently descoved Macros, seeing as how there value will never need to change I figured it would be better to have them stored as a constant value. But i can't seem to make a constant Array with this code.

#macro stackCapacity[1] 1

Am i doing something wronge? or can you not create marco arrays? and if not what is the best way to create a constant array?

thanks for reading.
 

jo-thijs

Member
Hey everyone,

I am working on an item system in a game and have run into a bit of a snag.
The jist of it is this, i have created an Enum to hold a Int value for every item within my game:
enum ItemIndex{
gun = 0,
sword = 1,
healthPotion = 10,
ect...​
}

Then i held specifice data abount my items within an array. then by using there Enum value i can get the relivent data from the array.

stackCapacity[ItemIndex.gun] = 1;
stackCapacity[ItemIndex.sword] = 1;
stackCapacity[ItemIndex.healthPotion] = 5;

So that works just fine, however I recently descoved Macros, seeing as how there value will never need to change I figured it would be better to have them stored as a constant value. But i can't seem to make a constant Array with this code.

#macro stackCapacity[1] 1

Am i doing something wronge? or can you not create marco arrays? and if not what is the best way to create a constant array?

thanks for reading.
Hi and welcome to the GMC!

Macro names have to be formed like variable names, the [ and ] characters are not valid.

Your macro will either be invalid syntax or will interprete the macro "stackCapacity" as "[1] 1".
 
G

Guest

Guest
Is the difference between using a macro or an enum for something like this just stylistic? They're both unchangeable and offer a slight performance boost over mutable variables, correct?

Also, is this usage---constant -> array index---what is meant by the term lookup table?
 
D

Daniel Cook

Guest
Hi and welcome to the GMC!

Macro names have to be formed like variable names, the [ and ] characters are not valid.

Your macro will either be invalid syntax or will interprete the macro "stackCapacity" as "[1] 1".
Thanks for the wellcome ^^

At the monent I have about 7 different arrays holding different data for the item in the game. These array are variables within an object call "ItemData_Obj".
I then simply made this instance's ID a global var and reference it when I want to get some data from the arrays.
Although this works just fine, I was was just curiouse about alternative way to hold this data.

So Macros and Array are a no. Is there no way then to make an Constant Array of Values?

Thanks.
 
D

Daniel Cook

Guest
Is the difference between using a macro or an enum for something like this just stylistic? They're both unchangeable and offer a slight performance boost over mutable variables, correct?

Also, is this usage---constant -> array index---what is meant by the term lookup table?
Yer, I prity much from what I understand. Having a Constant variable inprove performance.
I'm not sure what you mean by your quiestion "what is meant by the term lookup table?".
 
Here is something I have done recently. It is a collection of enums and macros to perform a similar function.

Code:
//Get days of the week
// This enum must begin with 0 due to the way GML calculates days of the week! Sunday = 0
    enum DAYS_OF_THE_WEEK
    {
        Sunday,
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday
    }
    //Get #macro constants for names of days of the week
    #macro    SUNDAY        "Sunday"
    #macro    MONDAY        "Monday"
    #macro    TUESDAY        "Tuesday"
    #macro    WEDNESDAY    "Wednesday"
    #macro    THURSDAY    "Thursday"
    #macro    FRIDAY        "Friday"
    #macro    SATURDAY    "Saturday"
Sadly, as explained above, there is no way in GML to create a #macro Array[]. The closest thing would be to:
  1. Create an enum with the terms you wish to use at their respective int values,
  2. Create a series of #macros with string values or other values you wish to use,
  3. Create something like global.ListOfItems = array_create(enumName.length(or last value in enum))-1, "[some value]")
  4. iterate through the array in a for-loop and input values as needed based on your #macros
Code:
for (var i = 0; i < array_length_1d(global.ListOfItems); i++)
{
     switch (i)
     {
           case ItemIndex.gun:
           {
                 stackCapacity[i] = Macro_LOCKED_AND_LOADED;
           }
           break;
     }
}
I don't know...

The only problem with this is that the array storing the data CAN change, whereas the enum and the #macros will not.
 
Very strange necrobump. Still, I wanted to share my personal solution to this. With the way macros work in GMS2, you can sort of create a macro array.
Code:
#macro COLOR obj_palette.palette_color

// -- obj_palette
palette_color[0] = c_white;
palette_color[1] = c_red;
palette_color[2] = c_magenta;
palette_color[...] = ...
The macro gets replaced with whatever it represents. In this case, the following two lines are both valid and functionally identical.
Code:
draw_set_color(COLOR[2]);
draw_set_color(obj_palette.palette_color[2]);
You can even change the value of the macro at runtime because it's essentially a pointer to a variable. This line is also valid:
Code:
COLOR[1] = c_blue;
 
Top