ID:271092
 
What would be the code to save contents to a list to a savefile?

Let's suppose a mob has a list called "attacks", which contains "A","B" and "C".

mob/var/list
attacks = list("A","B","C")


When the player logs out, these contents should be saved.

The next ime they log in, they will have "A"+"B"+"C" in their "attacks" list.

Detailed enough?

Thanks for the help...
I'll take a stab at this one.


Using what you have...

mob
Logout() //
..()
var/savefile/X = new("/[src.key]/file.sav") //if you're wanting individual savefiles for players...
X["list"] += attacks


Though if the attacks list is a mob var, it's probably getting saved already with the player's mob.

But continuing on a simplistic path, you'll need to be sure to load the attacks savefile when players log back in...

mob
Login()
..()
var/savefile/X = new("[src.key]/file.sav")
src.attacks += X["list"]


I'm not sure what you mean, as lists are as easy to save as any other variable. If you haven't worked with savefiles yet, I suggest you read some tutorial on it (DM Guide, DM Ref, DM Demos...etc.). However, if you have played with them before, I wrote this snippet up (and hopefully you can understand it, and especially learn from it).

client
var/list/myList = new // Our target list for saving

verb/Add_to_List(t as text)
if(t)
myList += t

verb/Remove_from_List(t as null|anything in myList)
if(t)
myList -= t

New()
..()
if(fexists("theSave.sav")) // If savefile exists, load it
var/savefile/F = new("theSave.sav")
F["theList"] >> myList

Del()
if(myList) // If myList is not null, save it
var/savefile/F = new("theSave.sav")
F["theList"] << myList
..()

Stat() // Display myList in statpanel
if(statpanel("My List"))
stat(myList)


Hiead
In response to Ariii (#1)
Ariii wrote:
>       var/savefile/X = new("file.sav")
> X["list"] += attacks
>
>

My only problem with your snippet is there; you have to use the "<<" (output) and ">>" (input) operators with savefiles, rather than the arithmetic ones.

Hiead