ID:913167
 
(See the best response by Neimo.)
Code:
The Save and Load proc
client
proc
Save()
if(src.mob.save_file_1==1)
var/savefile/save
save = new ("Player Saves/[mob.ckey]/Save File 1.sav")
save["mob"] << src.mob
save["x"] << src.mob.x
save["y"] << src.mob.y
save["z"] << src.mob.z
if(src.mob.save_file_2==1)
var/savefile/save
save = new ("Player Saves/[mob.ckey]/Save File 2.sav")
save["mob"] << src.mob
save["x"] << src.mob.x
save["y"] << src.mob.y
save["z"] << src.mob.z
if(src.mob.save_file_3==1)
var/savefile/save
save["mob"] << src.mob
save = new ("Player Saves/[mob.ckey]/Save File 3.sav")
save["x"] << src.mob.x
save["y"] << src.mob.y
save["y"] << src.mob.z


Load()
if(src.mob.save_file_1==1)
var/savefile/load
load = new ("Player Saves/[mob.ckey]/Save File 1.sav")
load["mob"] >> src.mob
load["x"] >> src.mob.x
load["y"] >> src.mob.y
load["z"] >> src.mob.z
if(src.mob.save_file_2==1)
var/savefile/load
load = new ("Player Saves/[mob.ckey]/Save File 2.sav")
load["mob"] >> src.mob
load["x"] >> src.mob.x
load["y"] >> src.mob.y
load["z"] >> src.mob.z
if(src.mob.save_file_3==1)
var/savefile/load
load = new ("Player Saves/[mob.ckey]/Save File 3.sav")
load["mob"] >> src.mob
load["x"] >> src.mob.x
load["y"] >> src.mob.y
load["z"] >> src.mob.z


mob
proc
Auto_Save()
set background = 1
src.client.Save()
spawn(10) src.Auto_Save()
if(prob(40))
usr <<"\white Character Saved."


Creating a New Character:
turf/newchar
density = 1
layer = FLY_LAYER
Click()
if(istype(usr, /mob/Guest))
switch(alert(usr, "Which slot do you want to use?", "Slots", "Slot 1", "Slot 2", "Slot 3"))
if("Slot 1")
if(fexists("Player Saves/[usr.client.ckey]/Save File 1.sav"))
switch(alert(usr, "Are you sure that you want to overwrite your old Character?", "Character Creation", "Yes", "No"))
if("Yes")
sleep(0)
fdel("Player Saves/[usr.client.ckey]/Save File 1.sav")
Create()
if("No")
return
else
usr.save_file_1 = 1
Create()

mob/Guest
Login()
src.loc = locate(9,9,1)
proc
Create()
var/mob/newmob
var/newname = null
newname = input("What will your name be?", "Name", usr.key) as text
if(!usr)
return
if(length(newname) < 4)
alert("Your name must be at least four letters long!")
Create()

else
newname = html_encode(newname)
newmob = new /mob/PC()
newmob.icon = 'base.dmi'

if(usr.save_file_1)
newmob.save_file_1 = 1
if(usr.save_file_2)
newmob.save_file_2 = 1
if(usr.save_file_3)
newmob.save_file_3 = 1


newmob.loc = locate(44,186,1)
newmob.name = newname
usr.client.mob = newmob

del(usr)


Loading
turf/load
density = 1
layer = FLY_LAYER
Click()
if(istype(usr, /mob/Guest))
switch(alert(usr, "Which slot do you want to use?", "Slots", "Slot 1", "Slot 2", "Slot 3"))
if("Slot 1")
if(fexists("Player Saves/[usr.client.ckey]/Save File 1.sav"))
usr.save_file_1 = 1
usr.client.Load()
else
alert("No Saved File Found!")
sleep(5)
return


Note: I only use File 1 right now for testing purposes. Once I get the first savefile, I'll be able to do the rest easily.

Problem description:Okay, so the character creation works and all. Saving also seems to save into the Folder that I specified. But whenever I try to Load the game, A black screen pops up, with no character at all. Also, sometimes when I remake my character for testing I get this:

runtime error: bad savefile or list
proc name: Save (/client/proc/Save)
source file: Saving and Loading.dm, 7
usr: Test (/mob/PC)
src: Chiwy8 (/client)
call stack:
Chiwy8 (/client): Save()
Test (/mob/PC) Auto Save()
Test (/mob/PC): Login()

In case you need the Login procs that I have, here:

mob/Guest
Login()
src.loc = locate(9,9,1)

mob/PC/Login()
AdminLoad()
Auto_Save() //This I added as a way to fix the problem with the Loading Black Screen. Doesn't seem to be changing anything
..()


The Auto Save spawn time is low for testing purposes as well. I've spent hours trying to figure this out on my own, and using the notes I have for Saving and Loading. Any help will be greatly appreciated.

~Chiwy8
In Save() you're using the savefile before creating it for mob.save_file_3.
Oh I see it. But I havent even done anything to the third saved file. Does that still affect it though?
Oh, also. You can't set coordinates one-by-one from 0. If any is 0, all are 0. To solve this, you need to load the coordinates and set loc using locate().

Sorry, I just read the runtime error and solved that first. To clarify that one, yes, you need to actually create the object before using it as an object that exists.
Wait, back up a bit. Where did you get the 0 from in the location?

And as for the runtime error, that didnt fix it :(
Now I got this runtime error Just now:


runtime error: bad savefile or list
proc name: Load (/client/proc/Load)
source file: Saving and Loading.dm,31
usr: Chiwy8 (/mob/Guest)
src: Chiwy8 (/client)
call stack:
Chiwy8 (/client): Load()
the load (6,4,1) (/turf/load): Click(the load (6,4,1) (/turf/load), "mapwindow.map", "icon-x=19;icon-y=2;left=1;scre...")
Anyone around that could help me?
In response to Chiwy8
Kaiochao wrote:
Oh, also. You can't set coordinates one-by-one from 0. If any is 0, all are 0. To solve this, you need to load the coordinates and set loc using locate().

Did you fix that yet? Instead of loading the x,y,z directly into the mob's coordinates, you should load them into temporary local variables, locate the turf at those coordinates, and set the mob's loc to that turf.
To be honest, I am confused to what you guys mean by that. Any tips on how I would go around to do that?
client
proc
Save()
if(src.mob.save_file_1==1)
var/savefile/save
save = new ("Player Saves/[mob.ckey]/Save File 1.sav")
save["mob"] << src.mob
save["temp_x"] << src.mob.x
save["temp_y"] << src.mob.y
save["temp_z"] << src.mob.z
if(src.mob.save_file_2==1)
var/savefile/save
save = new ("Player Saves/[mob.ckey]/Save File 2.sav")
save["mob"] << src.mob
save["temp_x"] << src.mob.x
save["temp_y"] << src.mob.y
save["temp_z"] << src.mob.z
if(src.mob.save_file_3==1)
var/savefile/save
save["mob"] << src.mob
save = new ("Player Saves/[mob.ckey]/Save File 3.sav")
save["temp_x"] << src.mob.x
save["temp_y"] << src.mob.y
save["temp_z"] << src.mob.z


Load()
if(src.mob.save_file_1==1)
var/savefile/load
load = new ("Player Saves/[mob.ckey]/Save File 1.sav")
load["mob"] >> src.mob
loc = locate(load["temp_x"], load["temp_y"], load["temp_z"])
if(src.mob.save_file_2==1)
var/savefile/load
load = new ("Player Saves/[mob.ckey]/Save File 2.sav")
load["mob"] >> src.mob
loc = locate(load["temp_x"], load["temp_y"], load["temp_z"])
if(src.mob.save_file_3==1)
var/savefile/load
load = new ("Player Saves/[mob.ckey]/Save File 3.sav")
load["mob"] >> src.mob
loc = locate(load["temp_x"], load["temp_y"], load["temp_z"])


You need to set the coordinates all at once, since the default mob connected to the client has coordinates of 0,0,0. Setting one at a time wont work, since it just sets it back to 0 (since others are 0, theres no such location as 1,0,0; 0,1,0; or 0,0,1)
Alright, I understand now what you meant. I tried doing that ^ and originally got the undefined variables in the loc. So I wrote it with src.mob.loc, so that's how I tested that out. I went into the game, and I moved around. When I tried to relog, I loaded the game "No Saved File Found". I did the same thing again. This time it found a saved file, but again, took me to (0,0,0) in the game. So that didn't work.
        Load()
if(src.mob.save_file_1)
var/savefile/load
load = new ("Player Saves/[mob.ckey]/Save File 1.sav")
load["mob"] >> src.mob
var/newx
var/newy
var/newz
load["x"] >> newx
load["y"] >> newy
load["z"] >> newz
src.mob.loc = locate(newx,newy,newz)


In game, it still loads to a (0,0,0) location
I think it may be that you aren't saving any coordinates to begin with, the variables are probably null.

try outputting the variables and see what you get?
I thought that this:

                save["x"] << src.mob.x
save["y"] << src.mob.y
save["z"] << src.mob.z


was a way to save and output the variables. Since I am now seeing otherwise, how else could I output the variable and save it to the client?

I apologize, I'm fresh out of the DM guide and some tutorials.
Best response
Here's an example of loading/saving the x, y, and z variables then locating them to the correct location.

mob
Read(var/savefile/f)
..()
// this will get the x, y, and z from the savefile and send them there
loc = locate(f["x"], f["y"], f["z"])

Write(var/savefile/f)
..()
// store the x, y, z
f["x"] << x
f["y"] << y
f["z"] << z
// remove any icon data (up to you) or any unnecessary data
f.dir.Remove("icon", "overlays", "underlays", "contents", "verbs")

client/proc
save()
var/savefile/f = new("[src.key].sav")
// store the mob here, it saves all variables that have been modified
f["mob"] << src.mob

load()
if(fexists("[src.key].sav"))
var/savefile/f = new("[src.key].sav")
f["mob"] >> src.mob
I see.

How is it that a lot of tutorials and guides don't do this? Most of them do it the way I was doing it (from what I've seen)
In response to Chiwy8
Chiwy8 wrote:
I see.

How is it that a lot of tutorials and guides don't do this? Most of them do it the way I was doing it (from what I've seen)

Some people go overboard, some people don't know what they're doing and some people don't know the correct way to save and load. I for one think that this is the way people should be saving and loading.
That makes sense. I guess I should start practicing and doing it your way than. Thanks!

I mentioned this above earlier but, is this related to my problem?


runtime error: bad savefile or list
proc name: Load (/client/proc/Load)
source file: Saving and Loading.dm,42
usr: Chiwy8 (/mob/Guest)
src: Chiwy8 (/client)
call stack:
Chiwy8 (/client): Load()
the load (7,3,1) (/turf/load): Click(the load (7,3,1) (/turf/load), "mapwindow.map", "icon-x=14;icon-y=21;left=1;scr...")


It happens when I click the load sometimes.