Game Development Community

How to create an user profile(load, save game data) for a game

by mohanmartin · in Torque Game Builder · 03/14/2008 (4:58 am) · 1 replies

Hi i am game artisit cum programmer, now creating a Hidden Objects game in TGB it contains more levels. but i dont know how to create user profile for it.
Anyone help how to create an user profile for load and save game datas?


Thanks and Regards
MOHAN.M

#1
03/15/2008 (10:06 pm)
There's a trick I read about on the forums some time ago. Create a global player profile object:

$PlayerProfile = new ScriptObject();

...and set dynamic fields for all of the player data you need like:

$PlayerProfile.Name = "Alice";

...and then to save the player profiles data:

function SavePlayerProfile()
{
//open the file
//seriously, I haven't done this since 1.1.3 so use whatever code is appropriate

//write all of the dynamic fields to the file
for(%index = 0; %index < $PlayerProfile.GetDynamicFieldCount(); %index++)
{
%FieldName = GetField($PlayerProfile.GetDynamicField(%index), 0);
%FieldValue = GetField($PlayerProfile.GetDynamicField(%index), 1);

File.Write("$PlayerProfile." @ %FieldName @ " = " @ %FieldValue @ ";");
}

//close the file
}

...which will put everything into the file as a line of code which makes loading the profile ridiculously easy:

function LoadPlayerProfile()
{
exec(/*insert file name here*/);
}

...and that's everything you need.

The biggest variable is which version of TGB you're using and how to write the path and file name.

The key is that by writing the player profile as a series of script statements, loading the profile becomes very simple. It also means that when you change how a profile saves, you don't need to also change how the profile loads.