change multiple variables value in a for loop

katharzis

Member
hello everyone.
i'm new to coding in general and i was wondering if you can help me with this problem:
i have this variables

GML:
global.StatLvl1 = 2
global.StatLvl2 = 2
global.StatLvl3 = 2
global.StatLvl4 = 2
global.StatLvl5 = 2
global.StatLvl6 = 2
global.StatLvl7 = 2
global.StatLvl8 = 2
global.StatLvl9 = 2
global.StatLvl10 = 2
as there will be more than 100 of this variables i want to use something like

Code:
for (var i = 1; i < 30; i ++)
    {
    
    global.StatLvl(i) = 2
    }
i know this code is wrong or incomplete
can you please guide me in the right direction?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
You ALMOST have it! :)

You want to use an ARRAY for your values instead of fixed variables. This would be done like this:
GML:
global.StatLvl = array_create(30, 2);
That will create a 30 item array with the value 2 for each item, and you can then access or change the value by doing something like:
GML:
global.StatLvl[4] = 100; // Change the fifth item in the array to 100 (arrays start at 0)
For more information, see these pages of the manual:

 

katharzis

Member
You ALMOST have it! :)

You want to use an ARRAY for your values instead of fixed variables. This would be done like this:
GML:
global.StatLvl = array_create(30, 2);
That will create a 30 item array with the value 2 for each item, and you can then access or change the value by doing something like:
GML:
global.StatLvl[4] = 100; // Change the fifth item in the array to 100 (arrays start at 0)
For more information, see these pages of the manual:

Thank you so much for your fast response !

i have 1 more question... if i have already spread this "global.StatLvl1 = 2" allover my project , i'd say i have to convert them all from this form to an array, manually ?

OHH can i just Search/Replace (global.StatLvl1 = 2) with (global.StatLvl(1) = 2) ?
will this make my life easier?

i have 30 of this global.StatLvl and have diferent values from 0 to 3, and i use them allover my project
 
Last edited:

Nocturne

Friendly Tyrant
Forum Staff
Admin
OHH can i just Search/Replace (global.StatLvl1 = 2) with (global.StatLvl(1) = 2) ?
Yes, you can use search replace, but be careful and ensure you use the right brackets for arrays as well as the right index value! You want to search for "global.StatLvl1" andreplace it with "global.StatLvl[0]". Note the SQUARE brackets and note that 1 becomes 0 (and 2 becomes 1 and 3 becomes 2, etc...) since arrays are indexed from 0, not 1.
 

katharzis

Member
Yes, you can use search replace, but be careful and ensure you use the right brackets for arrays as well as the right index value! You want to search for "global.StatLvl1" andreplace it with "global.StatLvl[0]". Note the SQUARE brackets and note that 1 becomes 0 (and 2 becomes 1 and 3 becomes 2, etc...) since arrays are indexed from 0, not 1.
perfectly understood. i really appreciate your time and help. ty
 
Top