ID:141075
 
*Code:
-Digimon Variables:
mob
Digimon
var
hp //Hit points
hpm //Hit points
sp //Skill points
spm //Skill points
saved = 0 //Tried to use this to check if you already saved the Digimon in my save code
exp //Experience
expn //Experience
lvl //Lvl
weig //Weight
fnds //Friendship with the owner
owner // Player ckey
str //Strenght
def //Defense
dlvl // "In-Training" || "Rookie" || "Champion" || "Mega" || "Ultimate"
race // "Agumon" || "Gabumon" || "Gomamon" ...


-Save Code:
mob
proc
Save()
var/savefile/S = new("Saves/[src.ckey]/[src.ckey].sav") //Creates a new save of your last saved character
S["last_x"] << usr.x //stores last X coordinate
S["last_y"] << usr.y //stores last Y coordinate
S["last_z"] << usr.z //stores last Z coordinate
var/savefile/H = new("Saves/[src.ckey]/[src.ckey]Digimon.sav")
for(var/i=1;i<=src.digi;i++)
var/mob/Digimon/E
for(var/mob/Digimon/X in world)
if(X.owner == src.ckey && X.saved == 0)
E = X
X.saved = 1
break
H["D[i]/ICON"] << E.icon
H["D[i]/NAME"] << E.name
H["D[i]/HP"] << E.hp
H["D[i]/HPM"] << E.hpm
H["D[i]/SP"] << E.sp
H["D[i]/SPM"] << E.spm
H["D[i]/EXP"] << E.exp
H["D[i]/EXPN"] << E.expn
H["D[i]/LVL"] << E.lvl
H["D[i]/WEIG"] << E.weig
H["D[i]/FNDS"] << E.fnds
H["D[i]/STR"] << E.str
H["D[i]/DEF"] << E.def
H["D[i]/DLVL"] << E.dlvl
H["D[i]/RACE"] << E.race
H["D[i]/X"] << E.x
H["D[i]/Y"] << E.y
H["D[i]/Z"] << E.z
Write(S) // Writes the stored files into one
Write(H)
Load() //Load procedure
if(fexists("Saves/[src.ckey]/[src.ckey].sav")) // Looks for written save file and if it exists it allows acces to load
var/savefile/S = new("Saves/[src.ckey]/[src.ckey].sav")
var/savefile/H = new("Saves/[src.ckey]/[src.ckey]Digimon.sav")
Read(S)
Read(H)
usr.loc = locate(S["last_x"],S["last_y"],S["last_z"])
for(var/i=1;i<=src.digi;i++)
var/mob/Digimon/E = new()
E.loc = locate(H["D[i]/X"],H["D[i]/Y"],H["D[i]/Z"])
H["D[i]/NAME"] >> E.name
H["D[i]/ICON"] >> E.icon
H["D[i]/HP"] >> E.hp
H["D[i]/HPM"] >> E.hpm
H["D[i]/SP"] >> E.sp
H["D[i]/SPM"] >> E.spm
H["D[i]/EXP"] >> E.exp
H["D[i]/EXPN"] >> E.expn
H["D[i]/LVL"] >> E.lvl
H["D[i]/WEIG"] >> E.weig
H["D[i]/FNDS"] >> E.fnds
H["D[i]/STR"] >> E.str
H["D[i]/DEF"] >> E.def
H["D[i]/DLVL"] >> E.dlvl
H["D[i]/RACE"] >> E.race
E.owner = src.ckey


*Problem description:
I'm working in a digimon game, and the Digimon are going to be like pets, but you won't be able to get the in your inventory. So what I'm trying to do is create a savefile for each Digimon the player has, and charge them once the player loads his past savefile. I've tried EVERYTHING (seriously) but nothing works. I've created this code, but it will only charge the player, it doesn't even create the mob. I'm desperate T-T, any ideas on how to work this out? If you have a total new code, fine for me...
mob
var/list/thingstosave = list()
proc/Save()
var/savefile/F = new(name)
F << src
proc/Load()
var/savefile/F = new(name)
var/mob/M
F >> M
del(src)


Store the objects you want to save in the thingstosave list. If any special processing needs to be done during saving/loading (IE: location will not be saved) put it in the object's Read() or Write() proc.
In response to Garthor
Thanks for trying, but that's not enough U.U, I tried doing what you said, but that's not enough, I made this code:

mob
var
list/digis//This list stores all the digimon the player has.
proc
Save()
var/savefile/S = new("Saves/[src.ckey]/[src.ckey].sav") //Creates a new save of your last saved character
S["last_x"] << src.x //stores last X coordinate
S["last_y"] << src.y //stores last Y coordinate
S["last_z"] << src.z //stores last Z coordinate
S["digis"] << src.digis //Here I saved the list
Write(S) // Writes the stored files into one
Load() //Load procedure
if(fexists("Saves/[src.ckey]/[src.ckey].sav")) // Looks for written save file and if it exists it allows acces to load
var/savefile/S = new("Saves/[src.ckey]/[src.ckey].sav")
Read(S)
src.loc = locate(S["last_x"],S["last_y"],S["last_z"])
for(var/mob/Digimon/E in src.digis)
var/mob/Digimon/D = E
if(D.farm == 0)
D.loc = src.loc
switch(src.dir)
if(NORTH)
D.y-=1
if(SOUTH)
D.y+=1
if(EAST)
D.x-=1
if(WEST)
D.x+=1


The list thing worked awesomly, and thanx for the tip, but once the list is loaded, I can't place the digimon in the world again, I tried creating secondary x, y, and z variable that updated themselves each time the digimon moved, and then when the player logged out, storing the digimon in the list, but that didn't work, so now I add the digimon to the list when it is created. Still, once I try to place it, it just won't appear, but I know the digimon did saved because I can see it in the Digimon panel, wich is a list of all the Digimon mobs in world who's owner is the player. Please help me out with this one ^^'.
In response to Gaboswsttj
That is not what I wrote at all. And you can't grab data out of a savefile with just S[key], you have to actually use the >> operator to read it.

Anyway, here's what I'm saying you should do:

mob
var/list/othermobs
proc
Save()
var/savefile/F = new(name)
F << src
Load()
var/savefile/F = new(name)
var/mob/M
//reading the savefile creates a new mob, so we have to get rid of the old one:
F >> M
del(src)

Write(var/savefile/F)
..()
F["x"] << x
F["y"] << y
F["z"] << z
Read(var/savefile/F)
var/nx, ny, nz
F["x"] >> nx
F["y"] >> ny
F["z"] >> nz
var/turf/T = locate(nx, ny, nz)
if(T) loc = T


That's it, aside from any fexists() type stuff you may need. If you don't want to save the locations of the mobs in the othermobs list, but rather just load them into the same tile, you'd again put that in Read():

mob
Read(var/savefile/F)
..()
//stuff from before
for(var/mob/M in othermobs)
M.loc = loc


The fact that there is simply a variable pointing to other mobs is enough to save them. COROLLARY: variables pointing to other mobs is enough to save them even if you don't want to save them. So, for example, if you have mob/var/target, you would be saving and loading your target (as in, a new one would be created upon load) with your character. In such cases, you want to make sure those variable are declared as var/tmp, so mob/var/tmp/target.
In response to Garthor
Garthor wrote:
you can't grab data out of a savefile with just S[key], you have to actually use the >> operator to read it.

Wrong; this is undocumented IIRC, but it works. I dunno how it behaves with objects though, I'm guessing it'd fail in such a case.
In response to Kaioken
Even though I agree with Kaioken, because I've done that before to save verbs, I see your point now, im going to try this right away and I'll let you know if it worked, thanks guys!!
In response to Gaboswsttj
OMG It worked!! Thank you SOOO much for all of your help... This was the final code

mob
var/list/digis //list of digimon the player has
proc
Save()
var/savefile/S = new("Saves/[src.ckey]/[src.ckey].sav") //Creates a new save of your last saved character
S << src
for(var/mob/Digimon/N in digis)
N.x2 = N.x
N.z2 = N.z
N.y2 = N.y
Write(S)
Load() //Load procedure
if(fexists("Saves/[src.ckey]/[src.ckey].sav"))
var/savefile/F = new("Saves/[src.ckey]/[src.ckey].sav")
Read(F)
for(var/mob/Digimon/D in world)
if(D.owner == ckey)
D.flw = 0
D.loc = locate(D.x2,D.y2,D.z2)
Write(var/savefile/S)
..()
S["x"] << x
S["y"] << y
S["z"] << z
Read(var/savefile/F)
..()
var/nx, ny, nz
F["x"] >> nx
F["y"] >> ny
F["z"] >> nz
src.loc = locate(nx, ny, nz)
for(var/mob/Digimon/M in digis)
M.loc = M.loc
Logout()
if(src.p == 1)
src.Save()
del src


THANKS FOR EVERYTHING
In response to Gaboswsttj
I don't know why you refuse to believe me when I say "that's all you need". Here's what you've written with all the redundant crap removed (and a small fix):

mob
var/list/digis //list of digimon the player has
proc
Save()
var/savefile/S = new("Saves/[src.ckey]/[src.ckey].sav") //Creates a new save of your last saved character
S << src
Load() //Load procedure
if(fexists("Saves/[src.ckey]/[src.ckey].sav"))
var/savefile/F = new("Saves/[src.ckey]/[src.ckey].sav")
//fix in order to use Read() on a savefile that was saved with <<
F.cd = "./.0"
Read(F)
Write(var/savefile/S)
..()
S["x"] << x
S["y"] << y
S["z"] << z
Read(var/savefile/F)
..()
var/nx, ny, nz
F["x"] >> nx
F["y"] >> ny
F["z"] >> nz
src.loc = locate(nx, ny, nz)
Logout()
if(src.p == 1)
src.Save()
del src


Still debating with myself over whether that small fix was preferable to what I wrote earlier ( F >> M; del(src) ). I suppose it is, because you put crap in mob/Logout() that belongs in client/Del(). Oh well.