ID:140475
 
This is my first coding attempt at... well, anything. I wanted to make a game with my friend and got stumped on the very first task, saving and loading.
This system uses actual skin buttons to navigate(rather than pop up windows). All the verbs correctly correspond to the buttons an do execute the verb, besides I see no way of posting those.
Code:
world
mob = /mob/Guest

mob/Guest
icon = 'guest.dmi'
var/tmp/LS1,LS2,LS3,Sel
Login()
savable = 0
loadlist()
..()
proc
loadlist()
Sel=null
winset(src,"select1","text=Free Slot") //sets the character load button to say Free Slot
winset(src,"select2","text=Free Slot")
winset(src,"select3","text=Free Slot")
var/I = 1
for(var/C in flist("Players/[src.ckey]/")) //assigns the character names to each button
winset(src,"select[I]","text=[C]")
if(I == 1) LS1 = C
if(I == 2) LS2 = C
if(I == 3) LS3 = C
I++
verb
newbutton() //called when the player clicks a button to create a new character
if(LS3) //a max of 3 characters
return
var/tmp/N = winget(usr,"namein","text") //retrieves the name of the new character
if(!N)
return
winset(src,"window1","is-visible=false") //closes the login window
var/tmp/mob/M
M = new/mob/PC() //creates a new mob for the player
M.name = N
M.loc = locate(10,10,1)
M.icon = 'player.dmi'
M.savable = 1
world << "[N] has logged in for the first time."
src.client.mob = M //transfers the player to the new mob
del(usr)
return
loadbutton() //called when the player clicks the Load button on the login window
if(!Sel) //ignores the click if no character is selected
return
winset(src,"window1","is-visible=false")
if(Sel==1)
src.client.loadcharacter(LS1) //calls the loadcharacter proc with the currently selected character
if(Sel==2)
src.client.loadcharacter(LS2)
if(Sel==3)
src.client.loadcharacter(LS3)

select1() //called when the player selects the first character slot
if(!LS1)
return
Sel=1
select2() //second
if(!LS2)
return
Sel=2
select3() //third
if(!LS3)
return
Sel=3
client/proc
loadcharacter(A) //proc that loads the character
var/savefile/S = new ("Players/[src.ckey]/[A]")
S["mob"] >> src.mob
S["x"] >> src.mob.x
S["y"] >> src.mob.y
S["z"] >> src.mob.z
world << "[src.mob.name] has logged in."
autosave() //Saves the character
if(!src.mob.savable)
return
var/savefile/S = new ("Players/[src.ckey]/[src.mob.name]")
S["mob"] << src.mob
S["x"] << src.mob.x
S["y"] << src.mob.y
S["z"] << src.mob.z
src << "Character Autosaved..."
spawn(600) src.autosave()


Problem description:
Loading a character completely resets the character(no items and default health), as well as not setting the location(black screen).
The original /mob/Guest doesn't disappear ether. It's as if the file I'm loading did not save correctly or I'm not even calling the correct file. =/

For make the Mob/Guest Dissapear and if its a player use this:

mob/Guest/Logout()
del src


And in the problem of Saving Try That

client/proc
loadcharacter(A) //proc that loads the character
var/savefile/S = new ("Players/[src.ckey].sav")
S["mob"] >> src.mob
S["x"] >> src.mob.x
S["y"] >> src.mob.y
S["z"] >> src.mob.z
S["items"] >> src.items
S["health"] >> src.health
world << "[src.mob.name] has logged in."
autosave() //Saves the character
if(!src.mob.savable)
return

var/savefile/S = new ("Players/[src.ckey].sav")
S["mob"] << src.mob
S["x"] << src.mob.x
S["y"] << src.mob.y
S["z"] << src.mob.z
S["items"] << src.items
S["health"] << src.health
src << "Character Autosaved..."
spawn(600) src.autosave()
In response to Revolution Gohan
I had originally thought that the items and variables are kept when I save the mob.
client/proc
loadcharacter(A) //proc that loads the character
var/savefile/S = new ("Players/[src.ckey].sav")
S["mob"] >> src.mob
S["x"] >> src.mob.x
S["y"] >> src.mob.y
S["z"] >> src.mob.z
S["items"] >> src.items
S["health"] >> src.health

The S["mob"] >> src.mob should already have the items and mob variables(health) in it.
This is a rather odd workaround to something that should already be happening.
In response to Cybersnake15
The real change is that A is no longer being used (meaning you're only saving one character). It looks like somewhere in the mix your reference to the mob is getting messed up (thus, it's not finding a savefile to load). I don't know enough about the newer interface stuff still to make a demo to play with, but if you output the character reference at several different stages you should be able to find out where the problem occurs. For instance, send a message to the world in loadcharacter() stating what A is.

client/proc
loadcharacter(A) //proc that loads the character
world << "A is [A] at the beginning of loadcharacter()." // Troubleshooting
var/savefile/S = new ("Players/[src.ckey].sav")
S["mob"] >> src.mob
S["x"] >> src.mob.x
S["y"] >> src.mob.y
S["z"] >> src.mob.z
S["items"] >> src.items
S["health"] >> src.health
In response to YMIHere
I'm not to sure what you mean by that, but I did happen to notice that the S["mob"] >> src.mob was triggering the Login() proc(as it should). I did not post that section of my code, thought it was irrelevant. But noob me decided to have the autosave() proc called there... saving my current location(0,0,0) to the save file before the loadcharacter() proc could get the old values out...
In response to Cybersnake15
You should be using spawn() to call the autosave in its own thread anyway, which will be scheduling it to occur as soon as the current thread sleeps, which it shouldn't do until after you finish loading.

Also, you should generally handle any special saving and loading in mob/Read() and Write(), so that in your save proc all you need is F<<mob and in your load proc F>>mob. It's much more organized and easier to work with, that way.