cannot saving Inventory id

hello everyone i'm a french and i've created a Inventory systmen with ds_list and i assigned some values for ItemID.
My Inventory system have 16 slots.

0 = sword;
1 = key;
2 = red potion
etc...
GML:
///Saving the File
if (file_exists(working_directory + "Me1")) file_delete(working_directory + "Me1");
var SaveFile = file_text_open_write(working_directory + "Me1");

var InventorySlot1 = ds_list_find_value(global.inventory,0);
var InventorySlot2 = ds_list_find_value(global.inventory,1);
var InventorySlot3 = ds_list_find_value(global.inventory,2);
var InventorySlot4 = ds_list_find_value(global.inventory,3);
var InventorySlot5 = ds_list_find_value(global.inventory,4);
var InventorySlot6 = ds_list_find_value(global.inventory,5);
var InventorySlot7 = ds_list_find_value(global.inventory,6);
var InventorySlot8 = ds_list_find_value(global.inventory,7);
var InventorySlot9 = ds_list_find_value(global.inventory,8);
var InventorySlot10 = ds_list_find_value(global.inventory,9);
var InventorySlot11 = ds_list_find_value(global.inventory,10);
var InventorySlot12 = ds_list_find_value(global.inventory,11);
var InventorySlot13 = ds_list_find_value(global.inventory,12);
var InventorySlot14 = ds_list_find_value(global.inventory,13);
var InventorySlot15 = ds_list_find_value(global.inventory,14);
var InventorySlot16 = ds_list_find_value(global.inventory,15);

if !is_undefined(InventorySlot1)
{
file_text_writeln(SaveFile);
file_text_write_real(SaveFile,InventorySlot1);
}
if !is_undefined(InventorySlot2)
{
file_text_writeln(SaveFile);
file_text_write_real(SaveFile,InventorySlot2);
}
if !is_undefined(InventorySlot3)
{
file_text_writeln(SaveFile);
file_text_write_real(SaveFile,InventorySlot3);
}
if !is_undefined(InventorySlot4)
{
file_text_writeln(SaveFile);
file_text_write_real(SaveFile,InventorySlot4);
}
if !is_undefined(InventorySlot5)
{
file_text_writeln(SaveFile);
file_text_write_real(SaveFile,InventorySlot5);
}
if !is_undefined(InventorySlot6)
{
file_text_writeln(SaveFile);
file_text_write_real(SaveFile,InventorySlot6);
}
if !is_undefined(InventorySlot7)
{
file_text_writeln(SaveFile);
file_text_write_real(SaveFile,InventorySlot7);
}
if !is_undefined(InventorySlot8)
{
file_text_writeln(SaveFile);
file_text_write_real(SaveFile,InventorySlot8);
}
if !is_undefined(InventorySlot9)
{
file_text_writeln(SaveFile);
file_text_write_real(SaveFile,InventorySlot9);
}
if !is_undefined(InventorySlot10)
{
file_text_writeln(SaveFile);
file_text_write_real(SaveFile,InventorySlot10);
}
if !is_undefined(InventorySlot11)
{
file_text_writeln(SaveFile);
file_text_write_real(SaveFile,InventorySlot11);
}
if !is_undefined(InventorySlot12)
{
file_text_writeln(SaveFile);
file_text_write_real(SaveFile,InventorySlot12);
}
if !is_undefined(InventorySlot13)
{
file_text_writeln(SaveFile);
file_text_write_real(SaveFile,InventorySlot13);
}
if !is_undefined(InventorySlot14)
{
file_text_writeln(SaveFile);
file_text_write_real(SaveFile,InventorySlot15);
}
if !is_undefined(InventorySlot16)
{
file_text_writeln(SaveFile);
file_text_write_real(SaveFile,InventorySlot16);
}


file_text_close(SaveFile);
GML:
if file_exists(working_directory + "Me1")
{
  var LoadFile = file_text_open_read(working_directory + "Me1")
  var loadedInvSlot1 = file_text_read_real(LoadFile);
  var loadedInvSlot2 = file_text_read_real(LoadFile);
  var loadedInvSlot3 = file_text_read_real(LoadFile);
  var loadedInvSlot4 = file_text_read_real(LoadFile);
  var loadedInvSlot5 = file_text_read_real(LoadFile);
  var loadedInvSlot6 = file_text_read_real(LoadFile);
  var loadedInvSlot7 = file_text_read_real(LoadFile);
  var loadedInvSlot8 = file_text_read_real(LoadFile);
  var loadedInvSlot9 = file_text_read_real(LoadFile);
  var loadedInvSlot10 = file_text_read_real(LoadFile);
  var loadedInvSlot11 = file_text_read_real(LoadFile);
  var loadedInvSlot12 = file_text_read_real(LoadFile);
  var loadedInvSlot13 = file_text_read_real(LoadFile);
  var loadedInvSlot14 = file_text_read_real(LoadFile);
  var loadedInvSlot15 = file_text_read_real(LoadFile);
  var loadedInvSlot16 = file_text_read_real(LoadFile);

  if is_undefined(loadedInvSlot1)
  {
   ds_list_add(global.inventory,loadedInvSlot1 -1);
  }
 
    if is_undefined(loadedInvSlot1)
  {
   ds_list_add(global.inventory,loadedInvSlot2 -1);
  }
 
    if is_undefined(loadedInvSlot2)
  {
   ds_list_add(global.inventory,loadedInvSlot2 -1);
  }
 
    if is_undefined(loadedInvSlot3)
  {
   ds_list_add(global.inventory,loadedInvSlot3 -1);
  }
 
    if is_undefined(loadedInvSlot4)
  {
   ds_list_add(global.inventory,loadedInvSlot4 -1);
  }
 
    if is_undefined(loadedInvSlot5)
  {
   ds_list_add(global.inventory,loadedInvSlot5 -1);
  }
 
    if is_undefined(loadedInvSlot6)
  {
   ds_list_add(global.inventory,loadedInvSlot6 -1);
  }
 
    if is_undefined(loadedInvSlot7)
  {
   ds_list_add(global.inventory,loadedInvSlot7 -1);
  }
 
    if is_undefined(loadedInvSlot8)
  {
   ds_list_add(global.inventory,loadedInvSlot8 -1);
  }
 
    if is_undefined(loadedInvSlot9)
  {
   ds_list_add(global.inventory,loadedInvSlot9 -1);
  }
 
    if is_undefined(loadedInvSlot10)
  {
   ds_list_add(global.inventory,loadedInvSlot10 -1);
  }
 
    if is_undefined(loadedInvSlot11)
  {
   ds_list_add(global.inventory,loadedInvSlot11 -1);
  }
 
    if is_undefined(loadedInvSlot12)
  {
   ds_list_add(global.inventory,loadedInvSlot12 -1);
  }
 
    if is_undefined(loadedInvSlot13)
  {
   ds_list_add(global.inventory,loadedInvSlot13 -1);
  }
 
    if is_undefined(loadedInvSlot14)
  {
   ds_list_add(global.inventory,loadedInvSlot14 -1);
  }
 
    if is_undefined(loadedInvSlot15)
  {
   ds_list_add(global.inventory,loadedInvSlot15 -1);
  }
 
    if is_undefined(loadedInvSlot16)
  {
   ds_list_add(global.inventory,loadedInvSlot16 -1);
  }
}
else
{
//do nothing
}
when i want to save Inventory that saving the value in the File but that don't load the value.
When i load the game with my script that assigned for the undefined value the value 0 which is the sword.
HELP ME !!! (and sorry for the bad english hope you can understand me).
 

gnysek

Member
I recommend to move to arrays first. You gonna save 90% lines of code, and it will be easier to search for bugs.

Edit: in fact, you can just make a for-loop over ds_list items without creating any "var". That would still benefit in less code, and will be easier to find where bug is.
 

TsukaYuriko

☄️
Forum Staff
Moderator
First things first: Save yourself the trouble of copying the same code over and over by using an array rather than numbered variables.

That aside, your load code reads reals, but does not move to the next line after doing so. You have to "read lines" just as much as you have to "write lines".
 
I recommend to move to arrays first. You gonna save 90% lines of code, and it will be easier to search for bugs.

Edit: in fact, you can just make a for-loop over ds_list items without creating any "var". That would still benefit in less code, and will be easier to find where bug is.
I fixed the scrSaveFile with for loops but how to do with scrLoadFile ?
GML:
if (file_exists(working_directory + "Me1")) file_delete(working_directory + "Me1");
var SaveFile = file_text_open_write(working_directory + "Me1");
var SavedInventory = ds_list_size(global.inventory);

file_text_write_real(SaveFile,SavedInventory);

   for (var i = 0;i < SavedInventory;i++)
   {
      file_text_writeln(SaveFile);
      file_text_write_real(SaveFile,ds_list_find_value(global.inventory,i));
   }
file_text_close(SaveFile);
 

TsukaYuriko

☄️
Forum Staff
Moderator
Have you tried? If you figured out how to do the save portion, chances are it is within your capabilities to figure out the load portion. :)

If you're facing issues, post what you tried and we'll see what we can do.
 
yes that's it work !

GML:
if file_exists(working_directory + "Me1")
{
  var LoadFile = file_text_open_read(working_directory + "Me1")
  var loadedInventory = file_text_read_real(LoadFile);
  for (var i =0;i < loadedInventory;i++)
  {
    var loadedInvslot = file_text_read_real(LoadFile);
    ds_list_add(global.inventory,loadedInvslot);
  }
  file_text_close(LoadFile);
 
Top