ID:170208
 
I'd like to manually save/load all the variables for my mobs using:

savefile["entry"] << variable
savefile["entry"] >> variable

The problem I'm having is that, since I am going to have a ton of variables at some point, I'd like to create a list of them and use that list to reference them dynamically. This way all the variables are in one place (the list), and I can modify that list, instead of having to go into each and every proc that saves or loads, and edit all the variables from there. Here is what I've tried, and I know why it doesn't work, but I'd like any suggestions, please:

var/tmp
loading_choice

#define CHAR_VARS list(name=name,gender=gender,lastname=lastname)

player
proc
SaveChar()
var/savefile/playerfile = new(PLAYER_FILE)
var/cname = ckey(name)
playerfile.cd = cname
var/char_var
for(char_var in CHAR_VARS)
playerfile[char_var] << CHAR_VARS[char_var]
src << "[name] saved!"
LoadChar()
var/savefile/playerfile = new(PLAYER_FILE)
playerfile.cd = "/[loading_choice]"
src << "Choice:[loading_choice]"
src << "Dir:[playerfile.cd]"
var/char_var
for(char_var in CHAR_VARS)
playerfile[char_var] >> //What do I put here??
loading_choice = null
src << "[name] loaded!"

---------------
The SaveChar() proc works like a charm, but I can't figure out how in the world to get the LoadChar() proc to dynamically >> into all the /player vars. There doesn't seem to be any way to reference a variable in Byond. I can't output into CHAR_VARS, because it's a definition. Honestly, I have no idea why the SaveChar even works in the first place, but it does.

=$= Big J Money =$=</<></<>
Use the DM tag before and after your code: <DM> code </DM>

There is an easier way:

var
list
save_vars = list(/*what you want to save*/)
mob
proc
SaveChar()
var/savefile/savefile = new(PLAYER_FILE)
for(var/v in global.save_vars)
savefile[v] << src.vars[v]
LoadChar()
var/savefile/savefile = new(PLAYER_FILE)
for(var/v in savefile.dir)
savefile[v] >> src.vars[v]


This way, you can actually add variables to the save_vars list between versions and not have any errors loading old-version characters, seeing as it never saved, so it will just default to an initial value.

Hope this helps.
In response to Ter13
just a note, save_vars would be a list of strings.
In response to OneFishDown
Did it really need to be said, though? I mean, it seemed obvious to me... I guess you are right, I should have mentioned it --even though it goes without saying.
In response to Ter13
So, basically what you are doing different is using a mob variable called vars, which, when given the name of a variable, will actually reference the variable itself? I haven't heard of vars yet, so I guess that's my missing link. Was there a reason you used a global variable versus my #definition?

While on the subject, I'm curious. I decided to go on this holy crusade to manually create my own saving method so I can easily keep track of what goes in and out of savefiles. However, is it really necessary? Is there that much benefit to doing this over ["player"] >> player ? It just seems safer to me.

Thanks a ton,

=$=
In response to BigJMoney
Yes, there is a benefit, speed and size.
In response to Ter13
I'm hoping you'll remark on that one question I had...

"Was there a reason you used a global variable versus my #definition?"

=$=
In response to BigJMoney
Probably because he's just used to that method. The only difference between the two is, your CHAR_VARS is replaced in the code durring the compile, while his method references the data from a variable durring runtime. For this purpose, I don't see much difference between the two. The #define method might be a little faster, but I doubt it'd be noticable.

~X
In response to BigJMoney
BigJMoney wrote:
So, basically what you are doing different is using a mob variable called vars, which, when given the name of a variable, will actually reference the variable itself? I haven't heard of vars yet, so I guess that's my missing link.

That's pretty much it. The vars var is a built-in associative list that contians all the variables and thier names. Associative lists are like hashes, or key=>value pairs. list("gender" = gender, "desc" = desc, "name" = name)

You can retrieve the value of a var if you know the variable's name. You can also do a reverse lookup and check which variables contain certain values.

var/value = vars["name"] // returns the name of the mob

var/check_value = 8 // the value we're looking for
for(var/v in vars) // for each variable name in the vars list
if(check_value == vars[v]) // check the value of the variable
return vars[vars[v]] // return the name of the variable if it matches our value


~X
In response to Xooxer
What?!? I never knew that! Man, I had no idea #define went through the code and did all the work at compile-time. I bet that's a huge difference when it comes to things that are called a lot in real-time. I probably first learned about #define back when I didn't know enough to care about that kind of significance.

Just when you think you know something.

=$=