Hey guys. I have a save command for my Player which saves all of my inventory, all of my stats, my name and my location... The problem I've been having is how to save other things in the game. The two examples I want to learn about is saving my Cat's name (I can rename him but don't know how to save his new name). and saving the objects I put into a chest (I can deposit and withdraw but it's empty on every new game)...
I have tried to put a variable name on my cats name and the chest and save it as a variable through my save file, but I haven't had any luck getting it to work.
This is what I have for code for my savefile and code for the 2 examples I want to make savefiles for.
Code:
Savefile:
mob
// Attempts to load the player. If it can't, spawns them like normal. Replace ..() with what you
// would like to happen when there's no save file to load.
Login()
if(!loadPlayer())
..()
// Saves the player to a file such as "Savefiles/alexpeterson.sav"
Logout()
savePlayer()
del src
// The processes themselves you'll want to use
proc
loadPlayer()
if(fexists("Savefiles/[ckey].sav"))
var/savefile/F = new("Savefiles/[ckey].sav")
Read(F)
return 1
else
return 0
savePlayer()
var/savefile/F = new("Savefiles/[ckey].sav")
Write(F)
return 1
// Here's an edit to Read and Save allowing them to save the player's coordinate variables.
Read(var/savefile/F)
..()
Move(locate(F["x"],F["y"],F["z"]))
Write(var/savefile/F)
..()
F["x"] << x
F["y"] << y
F["z"] << z
F["name"] << name
Cat Rename Code:
mob
Cat
verb
Pet()
set src in view(1)
usr << "<i><font color= \"#00aa00\">You pet your cat.</font></i>"
usr << "<i><font color= \"#881100\">[src] purrs excitedly.</font></i>"
Rename(name as text)
set src in view (1)
src.name = name
usr << "You rename your cat [name]"
And My Chest Code:
obj/Chest/verb
Open()
set src in oview(1)
icon_state = "Open"
Useable = 1
Close()
set src in oview (1)
icon_state = "Closed"
Useable = 0
//Close, it compiles but has a stack error when I try to store.)
Store()
set src in view (1)
if(Useable)
var/atom/movable/object = input("What item would you like to store?","Chest") as null|anything in usr
if(object)
object.Move(src)
usr << "You store [object] in the chest."
else
usr << "The chest is closed."
Withdraw()
set src in view (1)
if(Useable)
var/atom/movable/object = input("What would you like to take out?","Chest") as null|anything in src
if(object)
object.Move(usr)
usr << "You take the [object] out of the chest."
else
usr << "The chest is closed."
I know it's better if you already have a theory on how the code is supposed to work here, but I really don't know how to save world variables.
I hope someone can help me with this, and I look forward to hearing from you.
The reason the pet keeps track of the owner's key instead of the owner is to avoid circular references. You can look up the owner by key; at least unless NPCs can have pets, in which case you'd need to rethink it.
If your pet is kept in a pet var, and that var is not /tmp, then the pet will save when you save your mob.
One thing you should be aware of is that savefiles can accidentally use this same behavior against you. If you have an obj in your inventory, and it has a non-/tmp var that points to some other player's mob, that player will save with you. If you have a var pointing to a turf, the turf and anything on it will save with you. So you should be aware of those possibilities.