Game Development Community

How to make player profiles

by Andrew H · in Torque Game Builder · 08/03/2011 (5:42 pm) · 4 replies

I am going to integrate save files into my game. How would I do this? I need to save an inventory, attacks, creatures, story progress, and location (level and X/Y coordinates).

#1
08/04/2011 (1:50 am)
You'll most likely use SimXMLDocument object.
#2
08/04/2011 (8:31 am)
@Rpahut, Okay. Thanks. I might be able to figure it out from here. But maybe not. I'll think of something.
#3
08/04/2011 (6:49 pm)
I'm in the middle of this problem myself. My current experiment is to save my data out in a format that is genuine TorqueScript code. Then use exec() to read the items back in. Not sure how well it will go but so far so good.

For instance, let's suppose you've saved your player data in a SimObject (a little weird, I know) like so:

new SimObject(PlayerProfile);
playerProfile.nickname = "doodaddy";
playerProfile.level = 20;
playerProfile.otherScalar = 30.5;

// lalala.  using my playerProfile like God intended!

In other words, I've used dynamic fields of a SimObject to act like a struct. Now it's time to save the profile, like so:

%file = new FileObject();
// open and error check
...

%file.writeObject(playerProfile);

%file.close();
%file.delete();

This is what the file will look like (approximately, I'm just typing this all by heart):

new SimObject(playerProfile) {
   canSaveDynamicFields = "1";
     nickname = "doodaddy";
     level = 20;
     otherScalar = 30.5;
};

To read it back in do:

playerProfile.delete();  // trash the old one
exec(<your file name>);

if (!isObject(playerProfile))
   // go problems!
#4
08/04/2011 (8:20 pm)
@Charlie, alright. I figured out how to read from an XML object and store the value in a variable - using what Rpahut suggested - a SimXMLObject. You push the element containing your data and make %variable = %profile.getData()

Example:

//Given this XML:
//<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
//<player>
//  <coins>100</coins>
//  <currentHP>50</currentHP>
//  <maxHP>74</maxHP>
//</player>
//
//Then to read it, you would do this:

//Loading XML file
%file = new SimXMLDocument();
%file.loadFile("player.xml");

//Pushing player, then coins
%file.pushChildElement("player");
%file.pushChildElement("coins");

//Add the coins XML to variable
%coins = %file.getData();

//Continue on until all data has been added to variables.

But I'll be sure to try yours as well! Thanks! Although - being an amateur in Torque, I found it slightly difficult to understand. But I think I kinda get it. Seems like it might be more efficient.