Game Development Community

Save / Load from disk?

by Kevin James · in Torque Game Builder · 04/20/2005 (11:37 am) · 39 replies

Does anyone have a script example of how to do this?

I have an array I want to save to disk when the player quits T2D. And when it starts again, I want it to load this file and populate the array.

I know my object has a "save()" method and yet there is not a "load()" method?

There's nothing in the Reference doc on Save()

I searched the forums.. holy crap was that an exercise in frustration... my search terms are just too generic.

If I figure it out, I'll post my example here. I can't be the first or the last noobie to smash his head against this brick wall.

About the author

Computer security, digital forensics, and platform jumper enthusiast. shells.myw3b.net/~syreal/

Page «Previous 1 2
#1
04/20/2005 (11:44 am)
My Torque DB has already defined functions to save out the whole DB in either plain text, very simply encryption, or HTML (for easy viewing)... as well as a scripted parser to reload the infromation (from plain text or very simple encrypted)


though its probably overkill lol :)
#2
04/20/2005 (11:55 am)
Yeah.. overkill is an understatement! lol

But beggers can't be choosers, so I'll dig through the source and find the 6 lines that do what I need, hehe. I'm sure I'll learn something along the way.

Thanks again!
#3
04/20/2005 (12:05 pm)
I have to head out from work, later I can give you some simple commands... If I remember correctly

%file = new FileObject();

%file.openForWrite();

%file.writeLine("line");

%file.close();
or %file.openForRead();
and
%file.readLine();

look in the file.cs under the database folder of the Torque DB and search for those :)
#4
04/20/2005 (12:16 pm)
Cool!

On 2nd thought, it will be worth the pain to learn how to implement your TorqueDB because I know I will need it on future projects.

You are the Man, King Tut!
#5
04/20/2005 (12:17 pm)
Btw, if I don't use the Journal, do I still have to recompile the engine?
#6
04/20/2005 (12:22 pm)
Nope, it just allows for selection and highlighting in the journal :)... just include two exec lines - after you drop it in -(as listed in the resource) and thats it
#7
04/20/2005 (12:24 pm)
Awesome
#8
04/20/2005 (1:47 pm)
If you use the .save() method on an Object in Torque.. the way to load() it is to exec() the file you specified for the save.
#9
04/20/2005 (2:11 pm)
For what your doing your probably better off just doing as Harold says... I'll create a thread on using my Torque DB though
#10
04/20/2005 (2:21 pm)
In the short term, yeah, but in the long term, I want experience with your DB system. I'm waiting with baited breath for your script examples!
#11
04/20/2005 (3:08 pm)
@Harold: How do you use exec() to load it back into the object?
#12
04/20/2005 (3:31 pm)
If I am not mistaken Kevin, Save() requires a path to a file. then you would just do a exec(./path/file); to load the file. Save will save it in a way that T2D can read when you load that file.

The thing to be careful here is to not overwrite the saved files. Example saving $somevar to a file and then executing that file. then a couple of lines down doing $somvevar=whatever.

Note: seeing this used in a example would be nice as there isn't much out there in ways to see how it is implemented
#13
04/20/2005 (4:30 pm)
Ok here is an example.

If you have done the shooter demo, start it up.

open the display window and type:
echo($player.firelinkpoint);

you will see that the value is 0

ok in the command window put:
$player.firelinkpoint=12
You can check it, it will be 12

Next in the command window type
$player.save("./saveinfo.txt");
then close the app

Then go to your folder and you will see that the file has been saved there.

Then open saveinfo.txt and you will see that firelinkpoint=12

now start Torque again and do the echo again
echo($player.firelinkpoint);

You will see that it is still 0.

Close the app again. Now either open your client.cs file or any other file that you may use to keep your functions. I'm going to use client.cs file.

now at the end of the file, create a function called ReadSaveValues like so.
function ReadSaveValues()
{
exec("./saveinfo.txt");
}

now go to the spot after you called all of your player and enemy functions and put ReadSaveValues(); there like so:
Setupimage();
CreatePlayer();
CreateEnemy();
CreateTileMap();	
ReadSaveValues();
Reopen the app and go to the command window and type
echo($player.firelinkpoint);


you will see that the firelinkpoint is now 12

So basically that is how you use the save() function. I hope that helps in some way or another for anyone interested to know

I realize that it is overkill but it explains how to go about calling the file after declaring objects so you don't overwrite the save file values and it also show how to call the saved file in code.
#14
04/20/2005 (6:14 pm)
Thanks Charlie, I tried that but I can't get it to work.

I can get the save function to write to the file (i checked it, its full of my array values), but the exec function is where I fall on my face. In your example:

function ReadSaveValues()
{
   exec("./saveinfo.txt");
}

How does it know to put that value into $player.firelinkpoint?
#15
04/20/2005 (6:22 pm)
Give your object a name...

%obj = new ScriptObject(player);
player.save();

When you exec the file it will create the object named "player"
#16
04/20/2005 (6:35 pm)
@Harold:

$RankList.save("T2D/client/ranklist.dat");

But when I look in ranklist.dat, there's no reference to my $RankList object:

//--- OBJECT WRITE BEGIN ---
new ScriptObject() {
      rank2_2 = "8";
      rank0_2 = "4";
      rank3_0 = "10";
      rank1_0 = "7";
      rank3_5 = "6";
      rank1_5 = "6";
      rank2_3 = "2";
      rank0_3 = "5";
      rank3_1 = "4";
      rank1_1 = "5";
      rank3_6 = "11";
      rank1_6 = "8";
      rank2_4 = "5";
      rank0_4 = "7";
      rank3_2 = "8";
      rank1_2 = "4";
      rank2_0 = "8";
      rank0_0 = "10";
      rank2_5 = "6";
      rank0_5 = "6";
      rank3_3 = "1";
      rank1_3 = "7";
      rank2_1 = "3";
      rank0_1 = "7";
      rank2_6 = "9";
      rank0_6 = "3";
      rank3_4 = "10";
      rank1_4 = "10";
};
//--- OBJECT WRITE END ---
#17
04/20/2005 (10:58 pm)
But those are the ranklist values correct?

If I am not mistaken it doesnt need to know the name of the object specifically. as long as the objects values are different , there should be no difference. It can search by what type of object it is, in this case a script object, then it would find the rank members and asign the appropriate values.

I don't know why my code didn't work for you. It worked fine for me although I did change some stuff at the end that I didn't test first. Also, if you look at my code txt file, you will see that it is the same thing. there is no name.

Basically as long as the object exists then it should know where to asign the values. that's why I put that function call at the end of all the other function calls that did things like create the player object.

If i was to do that first, then it wouldn't save any information, because the object didn't exist yet.
#18
04/20/2005 (11:16 pm)
I am also thinking that since it is a global value that It wouldn't need all the name information since it can't repeat itself anywhere else.

Try doing it with a local variable and see if it gives you more information. The reason being that since it is local, it can be named and used elsewhere. So replace the $ with % and see what happens.

I'm at work so I can't check right now
#19
04/21/2005 (5:16 am)
Obviously, I'm experiencing a major mental block. Extremely frustrated. Ready to chuck this whole save(), exec() approach onto the junk heap and blast away at it with an AK47

%RankList = new ScriptObject();
    exec("T2D/client/ranklist.dat");
    %RankList.dump();

resultant console log section:
Compiling T2D/client/ranklist.dat...
Loading compiled script T2D/client/ranklist.dat.
Member Fields:
Tagged Fields:
Methods:
  delete() - obj.delete()
  dump() - obj.dump()
  getClassName() - obj.getClassName()
  getGroup() - obj.getGroup()
  getId() - obj.getId()
  getName() - obj.getName()
  getType() - obj.getType()
  save() - obj.save(fileName, <selectedOnly>)
  schedule() - object.schedule(time, command, <arg1...argN>);
  setName() - obj.setName(newName)

The ranklist.dat is full of the array values. I still can't comprehend how the engine is supposed to magically fill in my scriptobject when there's no reference to it in the file. So I'm gonna work on alternatives. If anyone out there is shaking their head at my stupidity and is just itching to set me straight on what I'm doing wrong here, please do.
#20
04/21/2005 (5:23 am)
I'm with Kevin here to be quite frank--based on the content of the file generated with .save, and the useage of exec people are talking about, I don't see how it works either!

In any case, Matt's Torque DB tutorial/resource gives you a whole slew of ways to read and write file information--might be better to just jump into that!
Page «Previous 1 2