Saving items from list to file (SOLVED)

Started by Sadistyk, Tue 21/03/2006 03:42:55

Previous topic - Next topic

Sadistyk

Hello. I want to create a database with last names (items in a list box), so I've created a list box in a gui called gEmp (the list script name is lista1). But I have a problem: when I quit the program a re-enter the list is empty. My question is: how can I save the items from the list? (maybe to some .txt file).

Good Bye.

monkey0506

#1
Hmm...perhaps....

Code: ags
// in game_start
File *listfile = File.Open("lista1.lst", eFileRead);
if (listfile) { // restore the list
  lista1.Clear();
  String word = listfile.ReadStringBack();
  while (word) {
    lista1.AddItem(word);
    if (!listfile.EOF) word = listfile.ReadStringBack();
    else word = null;
    }
  listfile.Close();
  }

// outside of all functions
void SaveList() {
  File *listfile = File.Open("lista1.lst", eFileWrite);
  if (listfile) { // save the list
    int i = 0;
    while (i < lista1.ItemCount) {
      listfile.WriteString(lista1.Items[i]);
      i++;
      }
    listfile.Close();
    }
  }


Just add the "in game_start" part to your game_start function, the "outside of all functions" part outside of all functions, and then call the SaveList function (type "SaveList();" (without the quotation marks)) before you exit the game.  Kind of a simple method...but it should work I think (untested though...soo...)


[EDIT:]

Oh, and yes...LST is an arbitrarily decided file extension because nothing I know uses that extension, and it could easily be interpreted as LIST.  Although three letter extensions aren't really necessary any more...o.0

[EDIT:]

[NOTE:]  You MUST fill the list and call the SaveList() function to create the list.lst file.  Creating the file yourself with the text in it will not work.

Sadistyk

#2
It worked great, thanks a lot ;D

If it isn't too much trouble, you could explain to me step by step what you've done, 'cause I don't understand anything of it.

monkey0506

#3
Code: ags
// in game_start
File *listfile = File.Open("lista1.lst", eFileRead);
if (listfile) { // restore the list
  lista1.Clear();
  String word = listfile.ReadStringBack();
  while (word) {
    lista1.AddItem(word);
    if (!listfile.EOF) word = listfile.ReadStringBack();
    else word = null;
    }
  listfile.Close();
  }


At the beginning of the game this opens the file lista1.lst for reading, and stores it in the File* (File pointer) listfile.  If the file does not exist then listfile will hold the value null.

If listfile is not null then lista1 is cleared, and the items are added back to the list.  This is done by starting a seed word (reading back the first word from the file with listfile.ReadStringBack() and storing it into a String variable (word)) and then running a while loop to add all the rest of the items.

The while loop will continue to run until the String word is null, which will be set when the end of the file is reached (listfile.EOF will equal true (note that "!listfile.EOF" means that listfile.EOF equals false)).

Then finally, the file (listfile) is closed.

Code: ags
// outside of all functions
void SaveList() {
  File *listfile = File.Open("lista1.lst", eFileWrite);
  if (listfile) { // save the list
    int i = 0;
    while (i < lista1.ItemCount) {
      listfile.WriteString(lista1.Items[i]);
      i++;
      }
    listfile.Close();
    }
  }


Okay...we open the file back up, this time for writing though (if the file doesn't exist, AGS will attempt to create it for you).  Then we store an integer to hold the index of the list items which we will iterate through to store them.

"while (i < lista1.ItemCount)" means that the while loop will run until i is greater than or equal to lista1.ItemCount (the number of items stored in the list).  We use '<' (less than) instead of '<=' (less than or equal to) because the items are indexed from 0 to ItemCount - 1.

Inside the while loop we write the items in the list to the file (to be read back later), and then increase i by 1 to continue to the next item (or end the loop when i is equal to lista1.ItemCount).

After that, we close the file and throw in our closing braces.



You're very welcome for the code.  Glad I could help.

Sadistyk

Thanks again. If only you could help me with this last thing, 'cause you seem to know a lot about this things. Suppose I misswrote an item and I want to erase it, but it's the third item in the list, and I can't use de RemoveItem command 'cause I need the position where the item is on, and if I put it on 0, will erase two items (that I don'w want to erase) after getting to the third. How can I erase the third without erasing the first two, or how can I tell the computer what is the items location?

monkey0506

I'm not sure I understand what you're asking here.

You say you can't use ListBox.RemoveItem?  You say you need the index, so perhaps you could do it like this:

Code: ags
lista1.RemoveItem(lista1.SelectedIndex - lista1.TopItem);


You could use this by say, adding a button to your GUI that says "Delete" and then putting the code into it's interaction script (set its "On click" to "run script" and then put the line in the script), and then when you find an item that you want to delete, you could select it, then click the delete button.

If this is what you're looking for and you need more details, feel free to ask.  Otherwise, can you please describe in a little more detail what it is you're asking?

Sadistyk

#6
That's exactly what I wanted. You really know of this stuff. Thanks a lot for your help.

P.S.: just curiosity. Is it possible to open a GUI from a list item?

monkey0506

Good.  Glad to have helped get the issue resolved.

And I don't really know so much about ListBoxes in particular (and I really know even less about the File functions), but there is good documentation, and having worked with AGS for two years, I've gotten a feel for how things generally flow.

SMF spam blocked by CleanTalk