P
ph101
Guest
Hi guys
Please can a more experienced GMLer roll up their sleeves and get their hands dirty with my 2 scripts for saving and loading a 1D array into an already open ini?
They are very simple -
Save loops through a 1D array and populates a temp ds_list before writing as a string with ds_list_write.
Load reads said string using ds_list_read into a temp list then loops through the length populating a 1D array.
Save works as intended, I can see a string has been made in the ini. Problem is, load is not appearing to work and I wondered if anyone could say why? I realise I should just not use arrays and purely rely on lists. I may have to if I can't fix this, but arrays are already embedded in a fair amount of my code, and really this should work...
Plus The scripts may help others.. if they worked of course...
So I am saving an array called global.have_room thusly
And the load script, which fails to populate my array.
I'm calling the load scr to populat the array thusly
I tried using pointers to fill the array directly in load also to no avail so i'm really stumped. Would really appreciate some help, thank you
Please can a more experienced GMLer roll up their sleeves and get their hands dirty with my 2 scripts for saving and loading a 1D array into an already open ini?
They are very simple -
Save loops through a 1D array and populates a temp ds_list before writing as a string with ds_list_write.
Load reads said string using ds_list_read into a temp list then loops through the length populating a 1D array.
Save works as intended, I can see a string has been made in the ini. Problem is, load is not appearing to work and I wondered if anyone could say why? I realise I should just not use arrays and purely rely on lists. I may have to if I can't fix this, but arrays are already embedded in a fair amount of my code, and really this should work...
Plus The scripts may help others.. if they worked of course...
Code:
///scr_save_1d_array(array_name, section, array_key_str)
//converts a 1d array into a list, saves the list in the open ini as a string
//assumes ini is already opened
var array_name = argument0; //name of our array to save
var array_section = argument1;
var array_key_str = argument2; //str to use as key to save a retriue3v
var length; //length of array
var temp_list_name; //temp list name
var i; //loop
var temp_list_str; //stores the list string
length = array_length_1d(array_name); //get length of 1d array
temp_list = ds_list_create(); //temp list to store array so we can write it
//output the array to a list
for (i = 0; i < length; i += 1) //global.gridWidth
{
ds_list_add(temp_list, array_name[i]); //add each array value to list in turn up to array length
}
//convert the list to a str for saving in ini
temp_list_str = ds_list_write(temp_list);
//save the string as per arguments - note the INI MUST ALREADY BE OPEN
ini_write_string(array_section, array_key_str, temp_list_str);
ds_list_destroy(temp_list); //we are done with the list now, destroy
Code:
scr_save_1d_array(global.have_room, 'general', '_1d_array_have_room');
Code:
///scr_load_1d_array(array_name, section, array_key_str)
//load a previously saved str of a list and populates a 1d array with each value
//assumes ini is already opened
var new_array;
var array_section = argument0;
var array_key_str = argument1; //str to use as key to save a retriue3v
var length; //length of array
var temp_list_name; //temp list name
var i; //loop
var temp_list_str; //stores the list string
//create temp list
temp_list = ds_list_create();
//load in the encoded saved string - NOTE THE INIT IS ALREADY OPEN and is closed elsewhere
temp_list_str = ini_read_string(array_section, array_key_str, 0);
//load the str into the temp list
ds_list_read(temp_list,temp_list_str);
//get lenth of the list now it has valuesin
length = ds_list_size(temp_list);
//output the list into the stated array
for (i = 0; i < length; i += 1) //global.gridWidth
{
new_array[i] = ds_list_find_value(temp_list, i); //grab each value in turn and put into array
}
ds_list_destroy(temp_list); //we are done with the list now, destroy
return new_array; //return the whole array
Code:
global.have_room = scr_load_1d_array('general', '_1d_array_have_room');
Last edited by a moderator: