Game Development Community

FileIO Tutorial (Revision 1.0)

by Kevin James · in Torque Game Builder · 04/01/2010 (7:49 am) · 5 replies

Its been several years for me so I'm going through the tutorials from the beginning to learn the new version and refresh myself. I'm okay up until the FileIO Tutorial. Btw, these tutorials are great as they hold my hand through the most mundane tasks. That is, up until this incredibly vague statement in the FileIO guide: "Make sure that you add an exec call in the game.cs file so that your new code will be loaded."

I would expect a hotlink to one of those mind-numbingly short and sweet little docs, like "create a new level by selecting New Level in the file menu". Since we have nothing like that I looked at the demos to see how it is done. Apparently, exec calls should be placed in the main.cs file. Here is what initializeProject looks like with my line added:

function initializeProject()
{
// Load up the in game gui.
exec("~/gui/mainScreen.gui");

// Exec game scripts.
exec("./gameScripts/game.cs");
exec("./gameScripts/FileIO.cs");

// This is where the game starts. Right now, we are just starting the first level. You will
// want to expand this to load up a splash screen followed by a main menu depending on the
// specific needs of your game. Most likely, a menu button will start the actual game, which
// is where startGame should be called from.
startGame( expandFilename($Game::DefaultScene) );
}

The console output looks nothing like what is shown in the tutorial. Here is the end of it:

Torque Game Builder (v1.7.4) initialized...
Loading compiled script C:/Program Files/GarageGames/TorqueGameBuilder-1.7.4/games/TutorialBase/resources/starterArt/resourceDatabase.cs.
% Game Resources : Loaded Resource starterArt
Loading compiled script C:/Program Files/GarageGames/TorqueGameBuilder-1.7.4/games/TutorialBase/game/managed/datablocks.cs.
Loading compiled script C:/Program Files/GarageGames/TorqueGameBuilder-1.7.4/games/TutorialBase/game/managed/persistent.cs.
Loading compiled script C:/Program Files/GarageGames/TorqueGameBuilder-1.7.4/games/TutorialBase/game/managed/brushes.cs.
Loading compiled script C:/Program Files/GarageGames/TorqueGameBuilder-1.7.4/games/TutorialBase/game/gameScripts/datablocks.cs.
Loading compiled script C:/Program Files/GarageGames/TorqueGameBuilder-1.7.4/games/TutorialBase/game/gameScripts/guiProfiles.cs.
Loading compiled script C:/Program Files/GarageGames/TorqueGameBuilder-1.7.4/games/TutorialBase/game/gui/mainScreen.gui.
Loading compiled script C:/Program Files/GarageGames/TorqueGameBuilder-1.7.4/games/TutorialBase/game/gameScripts/game.cs.
Loading compiled script C:/Program Files/GarageGames/TorqueGameBuilder-1.7.4/games/TutorialBase/game/gameScripts/FileIO.cs.
Activating DirectInput...
DirectInput joystick failed to enable!
Loading compiled script C:/Program Files/GarageGames/TorqueGameBuilder-1.7.4/games/TutorialBase/game/data/levels/fileIO.t2d.
DirectInput deactivated.
Shutting down the OpenGL display device...
Making the GL rendering context not current...
Deleting the GL rendering context...
Releasing the device context...

Why does the code not execute? FileIO.cs code here:

function writeFile()

{
echo("Here");

%file = new FileObject();
if(%file.openForWrite("./game/data/test.txt"))
{
%file.writeLine("Hello World!");

echo("File Written");
}
else
{
error("File is not open for writing");
}
%file.close();
%file.delete();
}

About the author

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


#1
04/01/2010 (8:07 am)
When you exec the FileIO.cs file, all it does is define the writeFile function, you still need to call the function if you want it to execute.

Put a call to writeFile after you exec FileIO.cs, and the example code should run.
#2
04/01/2010 (8:16 am)
Thanks!
#3
08/16/2011 (6:18 am)
Sorry to dredge this up after so long, but I'm far more clueless than the person who started this. How do I call to writeFile? I get the feeling we could use a bit more in the way of simple tutorials on how the scripting/coding works for this engine.
#4
08/16/2011 (3:08 pm)
Simply call the function "writeFile()" in your code whenever you want to write to a file.

Define:

function writeFile() {

Code here that does what you want
}

Execute:

writeFile();
#5
08/18/2011 (9:31 pm)
Thank you!