ID:155516
 
Hi, I'm relatively new to Byond. I can't seem to quite figure out savefiles.
What I want to do is check to see if a player has a savefile, and if he does, use the information in it to put him back where he left off, including his location.
If he doesn't have a savefile, I want to prompt him for the information to make one.

Here is what I have:

    Login()
var/savefile/F = new("players.sav")
var/safename = ckey(usr.key)
if (!loc) //new player
usr.class = input("What class?") in list ("Human")
if (usr.class == "Human")
usr.icon = 'Human.dmi'
usr.loc = locate(11,20,1)
usr.icon_state = input("What gender?") in list ("male","female")
F["[safename]"] << usr
else
F["[safename]"] >> usr
world << "[usr] has joined, say hi!"

I realize that it's pointless to ask for class when there is only one option, I'll be adding more.
fexists (file exists) sounds like what your looking for.

mob/Login()
if(fexists("savefiles/[src.ckey].sav"))
// do loading, deleting, etc
else
// start a new game


In almost all of my games, I start out with ^^^ because I build off of procs. Probably not the best way to go about it, but it gets the job done.

Saving and loading is another easy option.
mob/proc/save()
var/savefile/sf = new("savefiles/[src.ckey].sav") // locate the file in the dir
sf["name"] << src.name // save their name
sf["loc"] << src.loc // and their location


Loads would look exactly the same, with the << being >> instead, for input.

Normally I'd recommend you a DM Reference page, but they seem to be more complicated than they really need to tbh, so I'll save you the brain hurt. Good luck learning how to code.
In response to Vector2
Looks perfect, I'll give it a try!
You can try this:
mob
Login()
var
initial="[copytext(usr.ckey,1,2)]"
file="players/[initial]/[lowertext(usr.ckey)].sav"
X=11
Y=20
Z=1
safename = ckey(usr.key)
if(fexists(file))
src<<"loadFile([file])"
var/savefile/F=new/savefile(file)
F["safename"] >> usr
F["class"] >> usr.class
F["X"]>>X
F["Y"]>>Y
F["Z"]>>Z
else
var
savefile/F=new/savefile(file)
usr.class = input("What class?") in list ("Human")
usr.icon_state = input("What gender?") in list ("male","female")
F["safename"] << usr
F["class"] << usr.class
if (usr.class == "Human")
usr.icon = 'Human.dmi'
usr.loc = locate(X,Y,Z)
..()

In response to Vector2
Just to add on to this but saving a mob's loc probably isn't a good idea, because it saves a reference to the turf it was on, including the turf's contents. When you load, anything that was on the turf along with the mob will be loaded into the world.
In response to Vector2
You're going to break his game. Can you stop helping around on the forums without actually -testing- your own code?

That's going to leave him with people trying to load, but being unable to (and probably repeating the Login()).
In response to Hello2u
Don't use that code, you'll end up getting owned by some error that you could've sworn you didn't program in.

Something like Login() occuring multiple times (or any time you Load())

You have to load in the x, y, and z variables of the mob individually, NOT the loc. If you save an instance other than the mob itself into a savefile, you'll probably get an error.

I loaded the location into a savefile once, and I litterally wound up with a savefile with the whole map in it.
In response to Ill Im
At least he's trying.
In response to TGAGC
Trying doesn't really count for anything if your help is actually the opposite of helpful. I don't mean to be an ass about it, as a moderator of these forums, but we need to focus on helping.

Which also means not moderating on my behalf, Ill Im.

What I would've preferred here was "I'm not 100% sure about your issue specifically, but here's what I do to get myself started in general: [code]". If you're not sure, it's important to tell the poster you're not sure. Saves them hassle, saves you hassle.