ID:138407
 
Some thinking out loud...as I move to trying to use more of the automated save features (so that I can have the next savefile tutorial done about the time BYOND 3.0 comes out), I'm noticing that for certain variables I'm distinguishing between whether this is the very first time this object has been created or whether it's being read in from a savefile.

The current example is a derived value, ammunition. A weapon has ammunition, and it has a max_ammunition value.

Typically I put in New() a call to set the ammunition to max_ammunition.

But once I start dealing with savefiles, that's not a valid approach, because the weapon was saved with only 2 bullets and my New() is setting it back to 6.

I could just set the default value for ammunition for each weapon to the same as the max_ammunition value, like so:

var
ammunition = 6
max_ammunition = 6

But of course being me it bugs me to think that this is a policy-based approach that is very error prone. Too easy to create a new kind of weapon and forget to do this. Also, in reality setting the ammunition does other things, like set the suffix for the object to "([ammunition] bullets)".

So I am ending up with an Initialize() function that is separate from New(), to be called only the very first time the object is ever created (not when it comes from a savefile), which sets initial derived values such as this:

Initialize()
setAmmunition(max_ammunition)

Which leads me to think...it might be interesting for New() to know whether this is a new New() or a New() being called as this object is recreated from a savefile...
On 9/2/00 6:21 pm Deadron wrote:

Typically I put in New() a call to set the ammunition to max_ammunition.

But once I start dealing with savefiles, that's not a valid approach, because the weapon was saved with only 2 bullets and my New() is setting it back to 6.

It shouldn't be doing that. New() is called _before_ the savefile is loaded, so the values of saved vars will take precendence. Example:

mob
verb/save()
var/obj/O = new
O.name = "modified"

fdel("save.sav")
var/savefile/F = new("save.sav")
F << O
usr << "Saving [O]"

verb/load()
var/obj/O
var/savefile/F = new("save.sav")
F >> O
usr << "Loading [O]"

obj/New()
name = "initialized"

Here, we save a "modified" object and then reload it. Since the var Read() is done after the New(), it retains its correct name.

Hope that helps.
In response to Tom H.
Ok -- well I think I'm just confused and frazzled right now from savefile hassles. I'll post a bug about that.

On 9/2/00 7:18 pm Tom H. wrote:
On 9/2/00 6:21 pm Deadron wrote:

Typically I put in New() a call to set the ammunition to max_ammunition.

But once I start dealing with savefiles, that's not a valid approach, because the weapon was saved with only 2 bullets and my New() is setting it back to 6.

It shouldn't be doing that. New() is called _before_ the savefile is loaded, so the values of saved vars will take precendence. Example:

mob
verb/save()
var/obj/O = new
O.name = "modified"

fdel("save.sav")
var/savefile/F = new("save.sav")
F << O
usr << "Saving [O]"

verb/load()
var/obj/O
var/savefile/F = new("save.sav")
F >> O
usr << "Loading [O]"

obj/New()
name = "initialized"

Here, we save a "modified" object and then reload it. Since the var Read() is done after the New(), it retains its correct name.

Hope that helps.