• 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!

Basic string/value question with macros and for loops

D

danzibr

Guest
I'm storing player stats in an array using macros. Like PC0 has the value 0, PC1 has the value 1 and so on, then like NAME has the value 10, CLASS has the value 11, and so on.

Now, I'm trying to set the default names as '??????'. I *could* just type
Code:
pc_stats[PC0,NAME] = '??????';
pc_stats[PC1,NAME] = '??????';
pc_stats[PC2,NAME] = '??????';
pc_stats[PC3,NAME] = '??????';
pc_stats[PC4,NAME] = '??????';
But this seems like the perfect time to use a for loop. I've tried various things like
Code:
var i;
for (i=0;i<5;i++) pc_stats[PC+i,NAME] = '??????';
But... that doesn't work. I tried other things, like throwing string(<stuff>) and 'PC'+i in there, but I can't get it to work. I know it's super basic, but for the life of me I can't get it to work (sorry, I'm still a super novice).

Thanks!
 

Alice

Darts addict
Forum Staff
Moderator
Actually, you can't retrieve constants/macros by their name; that's because they're replaced at compile time by the code they represent.

If you know that PC1 is the same as PC0 + 1, and PC2 is the same as PC0 + 2, you might use:
Code:
var i;
for (i=0;i<5;i++) pc_stats[PC0+i,NAME] = '??????';
However, it only works for that specific case, and will fail when PC0/PC1/PC2 etc. aren't arranged in a nice sequence. You might be better off using the constants explicitly outside a loop, or maybe if you need to loop through PCn variables often, make an array/list which you would loop through.
(there is a tricky thing about array/lists, however - nested and chained arrays and data structures access is not supported in GM right now)
 
D

danzibr

Guest
Great! Not the exact solution I was looking for (which, as you said, doesn't exist), but it certainly gets the job done! Thanks :)
 
Top