Using fileIO for RPG character data
by Derek Traver · in Torque Game Builder · 12/14/2009 (12:37 pm) · 9 replies
I'm assuming the most efficient way to do an RPG game with an inventory, some basic statistics, and experiences points/leveling up would be to use fileIO.
I've seen and dissected the High Score tutorial to the best of my ability ( http://tdn.garagegames.com/wiki/TGB/ScriptTutorials/ScoreTutorial ), but am still having a difficult time wrapping my head around modifying it for these purposes.
Has anyone done it before that could shed some light on it? Or are there any tutorials or source codes for games out there that do this?
I've seen and dissected the High Score tutorial to the best of my ability ( http://tdn.garagegames.com/wiki/TGB/ScriptTutorials/ScoreTutorial ), but am still having a difficult time wrapping my head around modifying it for these purposes.
Has anyone done it before that could shed some light on it? Or are there any tutorials or source codes for games out there that do this?
About the author
Recent Threads
#2
Hehehe... I'm not sure where to start exactly. How about:
I have a simple game set up with a player object set up with a controls behavior and class "player".
How would I use fileIO to:
1) prompt the user to either create a new game, or load a game
2) if the user chooses to create a new game, prompts the user to enter a character name
3) upon character creation, create a text file storing the character name, level and max health. and then how would I access those values in my behavior, and subsequently save them to the file?
12/14/2009 (3:34 pm)
If I could have your source? :)Hehehe... I'm not sure where to start exactly. How about:
I have a simple game set up with a player object set up with a controls behavior and class "player".
How would I use fileIO to:
1) prompt the user to either create a new game, or load a game
2) if the user chooses to create a new game, prompts the user to enter a character name
3) upon character creation, create a text file storing the character name, level and max health. and then how would I access those values in my behavior, and subsequently save them to the file?
#3
12/15/2009 (2:00 pm)
When I get home later today I will post a sample of my save game and load game process to give you an idea of how I'm doing it.
#4
12/15/2009 (4:15 pm)
That would be awesome... thanks!
#5
function SaveCore()
{
%fo = new fileobject();
%file = "~/save" @ $saveid @ ".sav";
%fo.OpenForWrite(%file);
%fo.WriteLine("saveid " @ $saveid);
%fo.WriteLine("clock " @ $clock);
%fo.WriteLine("mastertime " @ $mastertime);
%fo.WriteLine("savename " @ $savename);
%fo.WriteLine("morale " @ $morale);
%fo.WriteLine("disease " @ $disease);
%fo.WriteLine("diseasetime " @ $diseasetime);
%fo.WriteLine("diseasecheck " @ $diseasecheck);
%fo.WriteLine("upgradepoints " @ $UpgradePoints);
%fo.WriteLine("capturedcities " @ $CapturedCities);
%fo.WriteLine("location " @ player.GetPosition());
%fo.WriteLine("difficulty " @ $Difficulty);
%fo.WriteLine("starving " @ $starving);
%fo.WriteLine("foodcontime " @ $checkFoodConTime);
%fo.WriteLine("lastcombat " @ $lastCombat);
%fo.close();
}
And here is I load these same variables back into the game -
function LoadCore()
{
%fo = new fileobject();
%file = "~/save" @ $saveid @ ".sav";
%ret = %fo.OpenForRead(%file);
if(%ret == -1)
return;
for(%line = %fo.ReadLine();%line !$= "";%line = %fo.ReadLine())
{
%word = GetWord(%line, 0);
switch$ (%word)
{
case "clock":
$clock = restWords(%line);
case "mastertime":
$mastertime = restWords(%line);
case "morale":
$morale = restWords(%line);
case "disease":
$disease = restWords(%line);
case "diseasetime":
$diseasetime = restWords(%line);
case "diseasecheck":
$diseasecheck = restWords(%line);
case "upgradepoints":
$UpgradePoints = restWords(%line);
case "capturedcities":
$CapturedCities = restWords(%line);
case "location":
%position = restWords(%line);
case "difficulty":
$Difficulty = restWords(%line);
case "starving":
$starving = restWords(%line);
case "foodcontime":
$checkFoodConTime = restWords(%line);
case "lastcombat":
$lastCombat = restWords(%line);
}
}
}
12/15/2009 (6:04 pm)
Ok, I save my game data in different text files to organize them better, here is a single function to save the game variables. This should hopefully give you a pretty good idea on how to save variables to files for save games, etc.function SaveCore()
{
%fo = new fileobject();
%file = "~/save" @ $saveid @ ".sav";
%fo.OpenForWrite(%file);
%fo.WriteLine("saveid " @ $saveid);
%fo.WriteLine("clock " @ $clock);
%fo.WriteLine("mastertime " @ $mastertime);
%fo.WriteLine("savename " @ $savename);
%fo.WriteLine("morale " @ $morale);
%fo.WriteLine("disease " @ $disease);
%fo.WriteLine("diseasetime " @ $diseasetime);
%fo.WriteLine("diseasecheck " @ $diseasecheck);
%fo.WriteLine("upgradepoints " @ $UpgradePoints);
%fo.WriteLine("capturedcities " @ $CapturedCities);
%fo.WriteLine("location " @ player.GetPosition());
%fo.WriteLine("difficulty " @ $Difficulty);
%fo.WriteLine("starving " @ $starving);
%fo.WriteLine("foodcontime " @ $checkFoodConTime);
%fo.WriteLine("lastcombat " @ $lastCombat);
%fo.close();
}
And here is I load these same variables back into the game -
function LoadCore()
{
%fo = new fileobject();
%file = "~/save" @ $saveid @ ".sav";
%ret = %fo.OpenForRead(%file);
if(%ret == -1)
return;
for(%line = %fo.ReadLine();%line !$= "";%line = %fo.ReadLine())
{
%word = GetWord(%line, 0);
switch$ (%word)
{
case "clock":
$clock = restWords(%line);
case "mastertime":
$mastertime = restWords(%line);
case "morale":
$morale = restWords(%line);
case "disease":
$disease = restWords(%line);
case "diseasetime":
$diseasetime = restWords(%line);
case "diseasecheck":
$diseasecheck = restWords(%line);
case "upgradepoints":
$UpgradePoints = restWords(%line);
case "capturedcities":
$CapturedCities = restWords(%line);
case "location":
%position = restWords(%line);
case "difficulty":
$Difficulty = restWords(%line);
case "starving":
$starving = restWords(%line);
case "foodcontime":
$checkFoodConTime = restWords(%line);
case "lastcombat":
$lastCombat = restWords(%line);
}
}
}
#6
12/16/2009 (8:52 pm)
You know what, you can save each line as TorqueScript, then just Exec the file to run the script. I've found that to be the simplest method of saving to a file; all the work is in the save, not the load.
#7
My file is set at: ("~/game/save.dat")
It can write. It echoes out the correct stuff when I read the file using script upon different executions of the program. However, when I go to the actual folder, the file is not there. I even did a "save.dat" search in Windows and it came up with no results.
02/09/2010 (12:10 pm)
I am having a weird problem. The code works, but...My file is set at: ("~/game/save.dat")
It can write. It echoes out the correct stuff when I read the file using script upon different executions of the program. However, when I go to the actual folder, the file is not there. I even did a "save.dat" search in Windows and it came up with no results.
#8
02/09/2010 (1:53 pm)
@Ki: Have you tried to make sure that Windows is not hiding any files? A .dat file might be considered a protected OS file, and thus hidden by Windows (the registry is a .dat file, after all).
#9
05/07/2010 (7:45 am)
@ Steve D -> ty man, this was very usefull for me :)
Torque Owner Steve D