Legacy GM can I save a 2D array to an INI file?

camerakid

Member
Hey Guys,

Can I save 2D arrays in INI files without converting it to a ds_list or ds_map?

I can fully save simple arrays but I couldn't figure it out to do with a 2D array.

Code:
for(var i = 0; i  < 100; i ++)
{
ini_write_real("Save", "stat01" + string(i), stat01[i]);
}
How can I save stat02[0,0]=5 , stat02[0,1]=1 , stat02[1,1]=2 etc. (x300) in an INI file?

Thanks.

Attila
 
Last edited:
F

Flappy Dappy Games

Guest
Yep, just like so:
Code:
for(var i = 0; i < array_width; i ++)
for(var j= 0; j< array_height; j++){
     ini_write_real("Save", "stat01"+string(i)+string(j),stat01[i,j]);
}
 

camerakid

Member
Thank you but actually what "array_width" and "array_height" would be?

I know only about array_height_2d

This method not works for me unfortunately.

Maybe anyone with another solution would be awesome! :D
 
Last edited:
F

Flappy Dappy Games

Guest
Thank you but actually what "array_width" and "array_height" would be?

I know only about array_height_2d

This method not works for me unfortunately.

Maybe anyone with another solution would be awesome! :D
They are just descriptive variables, you store whatever you want in them. For example, array_width = 100
 
C

CedSharp

Guest
Thank you but actually what "array_width" and "array_height" would be?

I know only about array_height_2d

This method not works for me unfortunately.

Maybe anyone with another solution would be awesome! :D
You use array_height_2d() to get the size of the first dimension.
You then use array_length_2d(array, index) to get the size of the second dimension.

You can also instead do this:

Code:
var i,j,array,array_width,array_height;
array_width = array_height_2d(myArray);
for(i=0; i<array_width; i++) {
    array = myArray[i];
    array_height = array_length_2d(myArray, i);
    for(j=0; j<array_height; j++) {
        ini_write_real("Save", "stat01["+string(i)+","+string(j)+"]", array[j]);
    }
}
 
Last edited:

camerakid

Member
You use array_height_2d() to get the size of the first dimension.
You then use array_length_2d(array, index) to get the size of the second dimension.

You can also instead do this:

Code:
var i,j,array,array_width,array_height;
array_width = array_height_2d(stat01);
for(i=0; i<array_width; i++) {
    array =stat01[i];
    array_height = array_length_2d(stat01, i);
    for(j=0; j<array_height; j++) {
        ini_write_real("Save", "stat01["+string(i)+","+string(j)+"]", array[j]);
    }
}
Thank you very much for this great method. For some reason I'm getting the "trying to index a variable which is not an array" error...

In the beginning of my code I created the array the following way:

Code:
globalvar stat01;

for (var m=300; m >= 0; m--)
for (var n=300; n >= 0; n--)
{
stat01[m,n]=0;
}
 

Simon Gust

Member
Thank you very much for this great method. For some reason I'm getting the "trying to index a variable which is not an array" error...

In the beginning of my code I created the array the following way:

Code:
globalvar stat01;

for (var m=300; m >= 0; m--)
for (var n=300; n >= 0; n--)
{
stat01[m,n]=0;
}
You shouldn't use globalvar
just make it global
Code:
global.stat01[300, 300] = 0;

for (var m=300; m >= 0; m--)
for (var n=300; n >= 0; n--)
{
    global.stat01[m,n]=0;
}
 

Simon Gust

Member
Might be helpful to state why ;)
1. it steals variables names. If you had global.stat01 you could still have unlimited stat01 variables for any other job.
2. it gets obsolete in later versions.
3. you forget it's scope because it's not color coded where as global. variables tell you in their name.
anything else?
 

camerakid

Member
You shouldn't use globalvar
just make it global
Code:
global.stat01[300, 300] = 0;

for (var m=300; m >= 0; m--)
for (var n=300; n >= 0; n--)
{
    global.stat01[m,n]=0;
}
Thank you for your idea but I have changed to be a global and still getting the same error during writing the ini. :(
 

Bukmand

Member
there isn't but you still shouldn't use it. Don't blame me for trying to help make better use of code.
I use globalvar all the time i dont use global.var takes so much time and globalvar makes it easier a lot
i could write all my vars in one line using globalvar instead of global.var each single line
 

Simon Gust

Member
I use globalvar all the time i dont use global.var takes so much time and globalvar makes it easier a lot
i could write all my vars in one line using globalvar instead of global.var each single line
not reason enough for me to use them. I used to but I started to forget what and wasn't global so I switched.
 

camerakid

Member
save example:
PHP:
for(var i = 0; i  < 100; i ++)
{
   for(var b= 0; b < 2; b++) {
      ini_write_real("Save", string(b)+"stat01" + string(i), stat01[i, b])
   }
}
loading example:

use this script to make things a lot easier

///scrSaveData(name, value)
ini_open("sav.ini")
ini_write_real("stat", argument0, argument1)
ini_close()

save example:
scrSaveData("stats", value)

///scrLoadData(name, value)
ini_open("sav.ini")
var readType=ini_read_real("stat", argument0, argument1)
ini_close()
return readType

loading example:
PHP:
for(var i = 0; i  < 100; i ++)
{
   for(var b= 0; b < 2; b++) {
stat[i, b]=scrLoadData("stats", string(b) + ""+string(i), defaultvalue)
}
}
to save and load amount of value use loop
Thank you for this method too... I don't really understand the saving part though... why do you put string(b) before the stat01's name? " string(b)+"stat01" "

This method generates no errors anymore but for some reason all data that is saved in the INI files are ZERO... Mmmmmm.....
 
C

CedSharp

Guest
I use globalvar all the time i dont use global.var takes so much time and globalvar makes it easier a lot
i could write all my vars in one line using globalvar instead of global.var each single line
I had a very recent hardcore argument about globalvar vs global-prefix and the use of global variables at all.
What people seem to think is that globalvar is bad since it can 'polute' your code, where globalvar doesn't.
I do not agree with that, I think the use of globalvar is very legitimate (as long as used correctly and not abused, obviously).

But it is true that globalvar is now deprecated and chances are in GM3 or even a major update to GM2 it might be removed.

1. it steals variables names. If you had global.stat01 you could still have unlimited stat01 variables for any other job.
2. it gets obsolete in later versions.
3. you forget it's scope because it's not color coded where as global. variables tell you in their name.
anything else?
1. Resources can steal variable names too, and they are global, yet no one complains about it. Use another argument.
2. True, I can't say anything against that lol
3. It is color coded in GM2 and also has the 'global variable' type in the intellisense. Use another argument.
 

camerakid

Member
I had a very recent hardcore argument about globalvar vs global-prefix and the use of global variables at all.
What people seem to think is that globalvar is bad since it can 'polute' your code, where globalvar doesn't.
I do not agree with that, I think the use of globalvar is very legitimate (as long as used correctly and not abused, obviously).

But it is true that globalvar is now deprecated and chances are in GM3 or even a major update to GM2 it might be removed.


1. Resources can steal variable names too, and they are global, yet no one complains about it. Use another argument.
2. True, I can't say anything against that lol
3. It is color coded in GM2 and also has the 'global variable' type in the intellisense. Use another argument.
Thank you. I frequently use GLOBALVAR and global-prefix too. I think both are handy here and there.

Actually do you have any recommendation why your example could have generated the "trying to index a variable which is not an array" error? Thank you :)
 

Bukmand

Member
Thank you for this method too... I don't really understand the saving part though... why do you put string(b) before the stat01's name? " string(b)+"stat01" "

This method generates no errors anymore but for some reason all data that is saved in the INI files are ZERO... Mmmmmm.....
*Solved
 
Last edited:

camerakid

Member
Thank you guys... I suspect I should go back to the roots as I am maybe using this array completely wrong.

I have a GRID of 50x50. I use mostly DS_GRID and MP_GRID for movements. Very complex game.

I have added an extra feature and found to be handy and fast to have a 2D array to save a few more data to the system. For example if the players interacts with an object on x5 and y5 grid it would save that interaction data to the array. A value like '3' meaning something happened with the object on GRID X5 Y5 that is stored in the 2D ARRAY.

I would like to save this data. The 2D array works perfect with this method.The only point I am missing here still how to save this 2D array to an INI file. :) I need to SAVE and LOAD that the ARRAY [5,5]'s value is '3'.

Is it better to use another DS_GRID for this too? I just don't want to make the game more CPU or MEMORY eater as I am using 3 50x50 GRIDS already for objects/tiles/walls... so a forth DS_GRID might be too much to check for.
 

Simon Gust

Member
1. Resources can steal variable names too, and they are global, yet no one complains about it. Use another argument.
2. True, I can't say anything against that lol
3. It is color coded in GM2 and also has the 'global variable' type in the intellisense. Use another argument.
1. but that is what naming convensions are for. If you call all your object random names like "tile" instead of obj_tile that's your own fault. I don't name a variable "obj_tile", I'd name it "tile". If you do it right you shouldn't worry about the lack of variable names. You just have to be smart and make your own rules. global.vars are free of course, I write my enums in CAPITAL letters. temporary variables have their own list of availiable names like inst, wdt, hgt, lft, top, rgt, bot, i, j, tile, spr, obj, ind, xx, yy, x1, y1, x2, y2, xsc, ysc, type, this... (for data structures list, map, buffer, array, grid, queue, stack, vertex and so on) I don't use macros if I already use enums and any resource has a prefix. Stop blaming others for having smart naming conventions.
3. Pretty sure globalvar is obsolete in GMS 2. And let's be honest, who uses GMS 2, like come on.
 
C

CedSharp

Guest
1. but that is what naming convensions are for. If you call all your object random names like "tile" instead of obj_tile that's your own fault. I don't name a variable "obj_tile", I'd name it "tile". If you do it right you shouldn't worry about the lack of variable names. You just have to be smart and make your own rules. global.vars are free of course, I write my enums in CAPITAL letters. temporary variables have their own list of availiable names like inst, wdt, hgt, lft, top, rgt, bot, i, j, tile, spr, obj, ind, xx, yy, x1, y1, x2, y2, xsc, ysc, type, this... (for data structures list, map, buffer, array, grid, queue, stack, vertex and so on) I don't use macros if I already use enums and any resource has a prefix. Stop blaming others for having smart naming conventions.
3. Pretty sure globalvar is obsolete in GMS 2. And let's be honest, who uses GMS 2, like come on.
1. But then that logic applies to globalvar as well. If you can't differienciate your own globals than your normal variables, that's your own fault. Conventions exist exactly for that. I'm just using your argument against you, twice.
3. GM2 is the new awesome beast, I use it, may other use it, it's awesome. That's not a valuable argument. I never said it wasn't deprecated, I just said it had color and intellisense.
 

Simon Gust

Member
1. But then that logic applies to globalvar as well. If you can't differienciate your own globals than your normal variables, that's your own fault. Conventions exist exactly for that. I'm just using your argument against you, twice.
3. GM2 is the new awesome beast, I use it, may other use it, it's awesome. That's not a valuable argument. I never said it wasn't deprecated, I just said it had color and intellisense.
1. I'm not countering your argument to use globalvar, I'm countering your argument of why no one blames resources from stealing variable names and being global. It's not my fault for missnaming globalvars if I don't use them to begin with what kind of logic is that. If I use global.var I don't have use any convention I can choose whatever I want. having to name globalvars isn't an argument of mine.
3. Speak for yourself and 85% of all of the community.
 
C

CedSharp

Guest
It'll be a great argument when you have to redo all your globals next fall.
Where is the info that globalvar will get removed next fall ? If that's the case then I actually have to change how I use globals indeed. I was under the assumption that since it just got deprecated it would be around for at least another couple years. GameMaker Studio 2 has legacy support for scripts that don't even exists right now, so I'm sure there will at the bare minimum be something similar that converts all globalvars to global.vars.

1. I'm not countering your argument to use globalvar, I'm countering your argument of why no one blames resources from stealing variable names and being global. It's not my fault for missnaming globalvars if I don't use them to begin with what kind of logic is that. If I use global.var I don't have use any convention I can choose whatever I want. having to name globalvars isn't an argument of mine.
3. Speak for yourself and 85% of all of the community.
Something being a resource, a variable or a global variable doesn't change anything. The naming of it should be differenciated for everyone's sanity. Some people (like you I assume at this point) rather prepend something in front of it, or even forget the use of globals whatsoever and instead create a 'manager' or a 'stats' object which holds all the values and is persistent.

there are many ways to handle things, globalvar being one which I will continue to say is valid. It doesn't bring any kind of issue whatsoever other by itself, it's the use that others make of it that is wrong.
Using global. instead of globalvar brings nothing more or less. From my point of vue you pollute your code with 'global.' everywhere and make an object access for something that really just should be a variable.

But that's my point of vue. You're allowed to say you don't like or don't want to use globalvar, but saying that someone else shouldn't is another matter.
Maybe saying you don't recommend it and listing points would've been better.
 

hogwater

Member
There is no info, but if something is deprecated, using it puts you at risk. I know I hate doing things twice, so I'd rather not spend my time creating a future disaster.
 

Simon Gust

Member
Something being a resource, a variable or a global variable doesn't change anything. The naming of it should be differenciated for everyone's sanity. Some people (like you I assume at this point) rather prepend something in front of it, or even forget the use of globals whatsoever and instead create a 'manager' or a 'stats' object which holds all the values and is persistent.

there are many ways to handle things, globalvar being one which I will continue to say is valid. It doesn't bring any kind of issue whatsoever other by itself, it's the use that others make of it that is wrong.
Using global. instead of globalvar brings nothing more or less. From my point of vue you pollute your code with 'global.' everywhere and make an object access for something that really just should be a variable.

But that's my point of vue. You're allowed to say you don't like or don't want to use globalvar, but saying that someone else shouldn't is another matter.
Maybe saying you don't recommend it and listing points would've been better.
I do have a persistent objects will all the important variables for the game's flow. The reason I "pollute" my code with globals in the first place is because they are faster to access than instance variables and it gives them sort of weight I want to see in my code. Everytime I see, read or write a global variable it imprints in my head and I start remembering it.
 
C

CedSharp

Guest
I do have a persistent objects will all the important variables for the game's flow. The reason I "pollute" my code with globals in the first place is because they are faster to access than instance variables and it gives them sort of weight I want to see in my code. Everytime I see, read or write a global variable it imprints in my head and I start remembering it.
I can understand the weight behind 'global.' in the code, the very reason I don't like it is the reason you like about it. That's quite fair.
But it is not true that accessing global.variablename is faster than object.varaiblename. Why? Because global is an actual instance in gamemaker. Treated differently, true, but it's basically just a persistent instance that you can't change any of the event's code.

At least that's what I've been told when debating globalvar in other posts of this forum.
I thought global. was more like a macro that would be replaced at compile time, but it seems not.
 

camerakid

Member
Actually meanwhile I have changed my code so created another DS_GRID for my project. That seemed to be easier to save than these 2D arrays.

Btw, interestingly I usually use global prefix version for like money/health etc. variables but I use globalvar for arrays... I think right now both are great to be used :)
 
Top