Game Development Community

dev|Pro Game Development Curriculum

Loading entire directories of scripts

by Dave Bacher · 02/09/2002 (8:30 am) · 3 comments

Download Code File

OK, so you're writing your wizbang game, and you want to add your newest weapon. So you have three basic steps:
1. Create the weapon's model
2. Create the weapon's script
3. Hook the weapon's script into the engine.

This is a way to do 3 without modifying any scripts, once it is set up.

This works the same way modern Linux configurations often work. You have your main init.cs file load a bunch of cs/gui files from a directory, and execute them automatically.

For example, instead of having to add a line to MyMod/client/init.cs to load a new GUI screen, you can just save the GUI file. The next time you start, it will automatically be located and loaded.

To accomplish this, you need a folder or directory to hold the files you want to run. In fps/client/init.cs, locate the following code:

// Load up the Game GUIs
exec("./ui/defaultGameProfiles.cs");
exec("./ui/PlayGui.gui");
exec("./ui/ChatHud.gui");
exec("./ui/playerList.gui");

// Load up the shell GUIs
exec("./ui/mainMenuGui.gui");
exec("./ui/aboutDlg.gui");

Remove all of these execs from the UI directory, and replace them with the following lines of code:
exec("MyMod/WildLoad.cs");
ExecDirectory("fps/client/ui", "Initializing GUI");

Remember only replace the execs. What this does is loads and processes ALL of the cs and gui files in the directory. Later, if you save a new gui file to that directory, it'll automatically be located and loaded.

This does not, yet, work on dso files. Exec tries to compile them, unless they are passed without the dso extension. It's easy enough to code, but it's not important to me at the moment.

Note that this allows the popular "just put the file here and it will work" expansion system.

#1
09/18/2002 (7:06 am)
got it working :) - neat.
worth noting for total newbs like me (took me a few hours to get this working properly for particles)

you must use the full path for the exec'ed files like this,

execDirectory("fps/server/scripts/particles", "Initializing Effects");


if you use this it will only do the first file

execDirectory("./particles", "Initializing Effects");
#2
12/21/2002 (11:02 am)
I'll take a look at that sometime if it is still happening -- it is a low priority, but it is one of the scripting functions that will be provided from the Battle Bazaar Common Scripts, when those are ready.
#3
07/10/2007 (6:51 am)
I know it's been sime time for this post, but I just bought the book "3D game programming All-in-One", so I am just starting with Torque! Anyway, here is the solution to the directory load:

function loadDirFiles (%path, %fileSpec)
{
if (%fileSpec $= "*.cs") {
%fileMask = %path @ "/" @ %fileSpec;
for (%file = findFirstFile (%fileMask); %file !$= ""; %file = findNextFile (%fileMask)) {
Compile (%file);
} // endfor
%fileSpec = "*.dso";
} // endif
%fileMask = %path @ "/" @ %fileSpec;
for (%file = findFirstFile (%fileMask); %file !$= ""; %file = findNextFile (%fileMask)) {
if (%fileSpec $= "*.dso") {
%file = getSubStr (%file, 0, strlen (%file) - 4);
} // endif
Exec (%file);
} // endfor
} // End

Usage:

// Load Characters Datablocks & Methods
loadDirFiles ("./Characters", "*.cs");
// Load Weapons Datablocks & Methods
loadDirFiles ("./Weapons", "*.cs");
// Load Vehicules Datablocks & Methods
loadDirFiles ("./Vehicules", "*.cs");