Game Development Community

How to organize game code?

by Kostiantyn Teterin · in Torque Game Builder · 07/12/2007 (10:14 am) · 5 replies

I am working on a game, where every level have different gameplay (something like Azada from Big Fish Games). I need to make a different code for different levels. I tried to put my code into the level script, but it seems to be pregenerated and after I save my level again - all my code was lost.

What is the best way to organize Torque game? I mean, where to put global gameplay code, where to put global menu code, where to put separate code for each level? In C++, I used different classes (or children of a base class) and also #include directive. But I don't know how to make it on Torque :(

Sorry if my questions seems too dumb to you.

#1
07/12/2007 (11:06 am)
You 'might' be able to add scripts to your level file if you wrap the editor generated code with this

//--- OBJECT WRITE BEGIN ---
//--- OBJECT WRITE END ---

This is how you can add code to your .gui files also (note. some people prefer to make a separate .cs file with the same name as the .gui file for each gui to contain scripts specific to it). This works with mission files in TGE but youll just have to test it for level files with TGB

Usually script files go in game/gamescripts so, within there, you could have a separate script file for each level or even a separate folder of scripts for each level. The organization is really entirely up to you because the engine doesnt care, as long as the script file is executed first you can use its scripts from anywhere.

You can't define classes in script but you could make a scriptobject and give it functions like, for someone used to OOP that might be a good organization for you.

new ScriptObject(level1);
new ScriptObject(level2);
function level1::begin()
{}
function level2::begin()
{}

You can similarly add script functions to other types of torque objects, like a gui (which you would probably put in the .gui file after // object write end)
#2
07/12/2007 (11:12 am)
James:

Thanks for so detailed answer! I'll try that immediately.
#3
07/12/2007 (12:18 pm)
James:

I've wrapped the generated code as you said, but my code was gone along with the marks when I've saved a slightly changed level :( Seems like the best idea is to keep the code separately.
#4
07/12/2007 (12:31 pm)
Kostya: How about this: Place invisible scene objects in your level and assign them a class / behaviour to load custom, level specific code. Make the objects delete themselves after that. That way, when you run your level, your scripts get loaded automatically.

Another possibility would be to name your level specific scripts like your level file and just load the same filename plus a different extension. You might also want to wrap your level specific code into packages and un-/load them on level transitions.
#5
07/12/2007 (6:09 pm)
Oliver:

Thanks for the great idea about behaviors! I have implemented this idea to make level loading more independent. And, by the way, I never realized how great the behaviors are! Just too perfect to be true :)