ID:2136548
 
(See the best response by Kaiochao.)
Saving Code (In obj/Player):
Logout()
var/player_sav = "players/[ckey].sav"
var/savefile/F = new(player_sav)
F["version"] << VERSION
F["mob"] << src
F["loc"] << src.loc
del src


Loading Code (In Client):
client/New()
if(usr) return ..() //reconnecting to existing mob
else
var/player_sav = "players/[ckey].sav"
if(length(file(player_sav))) //if player savefile exists
var/savefile/F = new(player_sav) //open it
var/version = ""
F["version"] >> version
if(version==VERSION)
F["mob"] >> usr
F["loc"] >> usr.loc
else
return 0
return ..() //creates a new mob if necessary

Problem description:

Hey guys, working on my own project lately and i have a weird bug i'm having problems with.

When i save the player's mob and load it back up it also saves and loads any objs in the same location as the player. if they are objects set by the map editor they disappear if the player saved on top of them
anyone know how to avoid this?
I assume that they only disappear when the player is loaded back into the square, but i don't have a second account to verify

Another problem i'm having is the player has an object that is supposed to follow them and i have it set to make it's loc the same as the players when the player's mob calls Move() and when the player's mob has Read() called so it should move to where the player is when the player logs in, it does not. It moves to the player as soon as the player moves, but it doesn't appear near the player when the player is read from the file.

Code associated with the object described:
Read()
..()
if(overworld)
EnterOverworld()
action_bar.loc = loc

New()
..()
if(overworld)
EnterOverworld()
action_bar = new(loc);
ActionFill()

Move()
..()
action_bar.loc = loc


I'll post more code if necessary but I feel like i gave enough description to receive an answer.
Best response
Instead of saving the loc (which is the turf that the mob is on, in this case), you should save the coordinates:
// when saving
F["x"] = x
F["y"] = y
F["z"] = z

// when loading
usr.loc = locate(F["x"], F["y"], F["z"])
This solved the obj problem and makes perfect sense. Thank you.

It doesn't however solve the second issue I mention.
I still can't set the action_bar's loc during the Read() called on the Player.

It also doesn't make sense to me why it doesn't work.
I managed to fix it like this
usr.loc = locate(F["x"], F["y"], F["z"])
var/mob/Player/P
P = usr
P.action_bar.loc = locate(F["x"], F["y"], F["z"])


But this seems super dirty and i don't understand why it didn't work as I had it before, I would like that explained to me for my own understanding of how byond works.
In response to Cabledragon
When you load a datum from a savefile, its New() is called (with no arguments), and then its Read() is called.

Instead of saving and loading coordinates in the client's save proc, you could do it in the player's Read() proc. Then, you can set the player's action bar's loc in Read() after the player's loc has been set properly.

Before, you were setting the action bar's loc before the player's loc was set.
No, I was doing it in the read proc before. Setting the action bar's loc, was happening in the Player's Read() in the first place.

It's in the last code box in the first post.

That's why it blows my mind that it won't work.
In response to Cabledragon
You shouldn't be using usr in Read(), which is why I didn't think that was in Read(). You should be using src there.

Instead of setting the action bar's loc to locate(...), you should just be setting it to loc.

Er, wait. Is action bar something that should be a HUD object in client.screen?
Action bar should also be able to be seen by other players so I would assume it should not be a HUD object.

I'm pretty sure I tried using src as well, earlier but I'm a little deep in code into something else at the moment but I'll go back and check how it functions now, with the changes you'd suggested.

Edit: Tested it, using src in Read() did not work as intended.
Okay, I'm officially going to bed because I have NO IDEA WHAT'S GOING ON ANYMORE.

SO, SUDDENLY, when i change the values of some of the variables in my /mob/Player where they are defined... it changes them even if the Player mob is READ FROM A FILE!

Edit: It only seems to do this... if the value of the saved variable was the same as it's defined value when it was saved...

Edit 2: Yes, I've tested it thoroughly and it seems very bizarre to me. If for whatever reason, the variable's value ends up being the same as the initial value and then it is saved. When the game load's next, instead of loading the saved value, it will load whatever the initial value is set to. ):
In response to Cabledragon
Cabledragon wrote:
SO, SUDDENLY, when i change the values of some of the variables in my /mob/Player where they are defined... it changes them even if the Player mob is READ FROM A FILE!

Edit: It only seems to do this... if the value of the saved variable was the same as it's defined value when it was saved...

Yes, this is correct behavior.

When you save a mob, vars that have not changed from their default value are not saved. That's the default; if you want them to save, you have to do it manually.

If you then change the default value, any mobs you load that did not save anything for those values will use the default.