ID:262521
 
Problem description:
It should be only saving objs, but when I load, its loads mobs. All the players that were in my game when I saved it, are loaded.
Code:
proc
SaveObjects()
world << "Saving Map..please wait!"
var/savefile/F = new ("objects.sav")
var/list/L = new
for(var/obj/O in world)
O.saved_x = O.x
O.saved_y = O.y
O.saved_z = O.z
L += O
F[""] << L
world << "MAP SAVED!"


proc/LoadObjects()
world << "Loading Map..please wait!"
sleep(1)
var/savefile/F = new ("objects.sav")
var/list/L = new
F[""] >> L
if(!L) return
for(var/obj/O in world) if(O.loc||O.z!=1) del(O)
for(var/obj/O in L)
O.loc = locate(O.saved_x,O.saved_y,O.saved_z)
for(var/mob/Player/M in world)
if(!M.client)
del(M)
world << "MAP LOADED!"



Make sure you exclude all things not an object. You could use isobj(O) to make sure that whatever is getting saved and/or loaded is an obj.

P_S
I believe this is a result of references in those /objs pointing to /mobs. Let's say you have a 'sword' /obj whose loc is a certain player. This sword will be saved with a reference to that player. Therefore, when you load your sword, the player will be loaded as well. You can fix this by nulling out any such references, or skipping objects that are owned by mobs.
//Null references
obj
proc/save()
if(ismob(loc))
loc = null
.=..()
//Skip objects
proc/saveworld()
for(var/obj/O in world)
if(ismob(O.loc))
continue
Do_saving_stuff(O)


It's been a while since I worked on save files, so I might be wrong about this.
In response to IainPeregrine
If this is a building game, then I think it's more likely that the owner var is being saved..
Is it for creature world 2?
In response to DarkCampainger
Yes.

BTW, the owner var is set to there key.
In response to Sniper Joe
That's a bad thing to do, you should never set an owner to a key otherwise the point of an owner var will be...slightly pointless.
In response to Ol' Yeller
How so...
In response to IainPeregrine
IainPeregrine wrote:
I believe this is a result of references in those /objs pointing to /mobs. Let's say you have a 'sword' /obj whose loc is a certain player.

Actually this is incorrect. The loc var does not save, and in any case is not an actual reference.

However, true references will save in this manner, provided the var isn't a /tmp. If for example the sword had an owner var pointing to a mob (instead of a key as it should), that would save. Another place this fails is when you have a party system and point to another mob or to the party. (The party really shouldn't be saved, but if it must, it should save a list of keys and not mobs.)

Lummox JR
In response to Ol' Yeller
Ol' Yeller wrote:
That's a bad thing to do, you should never set an owner to a key otherwise the point of an owner var will be...slightly pointless.

It's great that you're trying to help, but you really have no idea what you're talking about, so it kinda ruins it. I implore you to please think through each issue before actually posting code help. That applies to a lot of other threads of course, and even goes back long before you had this key.

Yes, the owner var should be set to the key. Usually it's no trouble to find the actual matching mob if need be, but with most items that's not often needed. Most comparisons on an owner var are usually the sort of thing that can be done with keys. (This is quite different of course from an equipment system in which it's better to know which item you're holding than whether a particular item is being held.)

The reasons an owner var should be a key or ckey are several:

  • An owned item may well save separately of the mob who originally owned it.
  • If an owned item saves outside of a mob's inventory, it must save the key in place of the mob via a manual Write(), then it must find the mob in place of the key when loading, which may not even be possible if that mob no longer exists or hasn't loaded.
  • To verify ownership all a mob has to do is compare their key (if any) to the owner var.

    On the other hand, there's not one compelling reason to make the owner var a mob.

    Now I'll grant that most of these reasons are not immediately obvious and a novice programmer would not be expected to know those. However by thinking through the use of an owner var even a little bit, it should be clear that only a key comparison is really needed for that sort of thing. And where a key is just as easy (or easier) to use as a mob, it should be. Even if that part is not apparent, it should at least be easy to see that the key is just as good for anything you'd use an owner var for, and there's no point telling someone to go the other way; using the key is in fact better in every way.

    Lummox JR