Game Development Community

Beginner ? on how the Level Builder works with .cs files

by Sonja · in Torque Game Builder · 04/12/2006 (12:01 pm) · 13 replies

I am brand new to this whole realm of computer games (not to mention posting questions to a forum!), so please bear with me if I sound naive.

I have gone through both the Basic Tutorial and Level Builder Getting Started tutorial(got both working), and read through the Torque Script primer and the Informal Technical Overview (although I didn't understand most of this last item). What I am wondering is how the game scripts (e.g., game.cs) and the levels (those .t2d files that the Level Builder generates) fit together? I assume that you can build a game using both a script like we generated in the Basic Tutorial and the Level Builder, but I can't get them to work together. It looks like there are multiple hierarchized .cs files that T2D uses, and I can't figure out what should go where. Do you separate out the stuff you write script for in one file, and the stuff that comes out of the Level Builder is in a different file? Do you just put the script into the .t2d files? Can anyone give me some suggestions on how to get them working together? or at least where to look for guidance.

(Thinking about this leads me to the more fundamental conceptual question of what are these "levels" anyway?)

Thanks!

About the author

Recent Threads


#1
04/12/2006 (1:38 pm)
The best way to link them together is setting an object in the Level Builder to a "ScriptClass"... you can do this by setting its "class" or "superclass" field in the Level Builder... for example if you set a static or animated sprite's class to "Player"... and then have this function in a script that gets exec'd your player will begin to move right.

function Player::onLevelLoaded(%this, %scenegraph)
{
   %this.setLinearVelocityX(10);
}
#2
04/12/2006 (1:58 pm)
Don't worry about having to ask questions, we were all beginners with T2D/TGB at some point.

Without spending hours typing up a reply (which probably needs to be added as an update to the TDN article on how TGB starts up), let's look at a bit of code.

Here is part of the main.cs file which is found in games/T2D (or in the name of the project folder you created):
//---------------------------------------------------------------------------------------------
// initializeProject
// Perform game initialization here.
//---------------------------------------------------------------------------------------------
function initializeProject()
{
   // Load up the in game gui.
   exec("~/gui/mainScreen.gui");
   
   // Exec game scripts.
   exec("./gameScripts/game.cs");
   
   // Remove the following four lines if you would like to start the game without running the
   // level builder.
   if ($runWithEditors)
   {
      toggleLevelEditor();
      return;
   }
   
   // 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("~/data/levels/baseLevel.tgb");
}
Since you already did the basic tutorial, hopefully the script stuff makes sense. Anyway, it's pretty well commented. Basically after we exec our gui/scripts, the if statement is true in a default build and the level editor loads up. It also returns out of the function so startGame is not run.

Instead, startGame gets called when you press the play button or via Launch->Run Game in the editor.

//---------------------------------------------------------------------------------------------
// startGame
// All game logic should be set up here. This will be called by the level builder when you
// select "Run Game" or by the startup process of your game to load the first level.
//---------------------------------------------------------------------------------------------
function startGame(%level)
{
   // Set The GUI.
   Canvas.setContent(mainScreenGui);
   Canvas.setCursor(DefaultCursor);
   
   moveMap.push();
   sceneWindow2D.loadLevel(%level);
}
Here is the default startGame function. Say you want to load levels like it is done in the level editor and then have a custom script set up the camera. Your game.cs file might end up looking like this:
//---------------------------------------------------------------------------------------------
// startGame
// All game logic should be set up here. This will be called by the level builder when you
// select "Run Game" or by the startup process of your game to load the first level.
//---------------------------------------------------------------------------------------------
function startGame(%level)
{
   // Set The GUI.
   Canvas.setContent(mainScreenGui);
   Canvas.setCursor(DefaultCursor);
   
   moveMap.push();
   sceneWindow2D.loadLevel(%level);
   createCamera();
}

//---------------------------------------------------------------------------------------------
// endGame
// Game cleanup should be done here.
//---------------------------------------------------------------------------------------------
function endGame()
{
   sceneWindow2D.endLevel();
   moveMap.pop();
}

function createCamera()
{
   echo("Camera created!");
   $camera = new ScriptObject()
   {
      class = Camera;
   };
}
See how I added the createCamera and called it within the startGame function? That's a basic way to add custom functions/scripts to what you create in the level editor. Of course testing out what I just did is not going to give much in the way of a visual result (except for the echo which would display in the console). Still, I hope the concept is clear enough. If not let me know.
#3
04/22/2006 (7:08 am)
Thanks Mike, that helped me a lot!

I think a tutorial that links the level editor with scripting should be high on the priority list. Because it has taken me many days to subdue this sense of panic and frustration that never the twain shall meet. Actually, it would be best for the shooter tuturial to be totally re-written with all of the tools in mind. I know.. easier said than done.. but for newbies that would be an invaluable tutorial.
#4
04/22/2006 (7:14 am)
I have written code and a tutorial as a TGB Bounty submission for the Memory Game.

I'm waiting on some feedback from GG as to what they think of it, but it does cover dragging sprites into a level, setting some properties in the Level Builder, and then doing stuff to them in script.

Hopefully it won't be too long before I hear something and then it can be released :)
#5
04/22/2006 (7:35 am)
Glad that helped you Kevin.

I imagine all of the existing tutorials will be redone taking into account the level editor. Some of them have been placed as bounties for the community to update. Others, like the spacescroller, GG will update internally at some point before we are out of beta, I guess.
#6
04/22/2006 (3:30 pm)
Matthew and Mike,

Thanks to both of you for your replies. Between your explanations, lots of time spent reading through the forums, and even more time spent over the last week and a half playing around with T2D, I feel like I am making at least some progress. It's slow, but then I never thought it was going to be easy.

Thanks again. I am sure I will be back with more questions.
#7
04/23/2006 (2:59 am)
When beta3 is out and with it the bounty mini games with tutorials a lot of things will be cleared up. Right now there are some bugs in the level builder that make it hard use.
#8
04/23/2006 (6:12 pm)
I have a related question that may seem silly. Has anyone tried to put together the graphics for GUI using the level editor? If so, how did you get the graphics to actually function as buttons and menu items?

Mike and Matt, thanks for the tie-in. I've been looking at TGB and wondering how things were connected myself. Since the level call is built-into the main script, now I know all I have to do is call my individual script modules from the level call. :)
#9
04/24/2006 (2:01 pm)
Thanks for the tips! After a week of failed experimentation, I finally have my player object in the level editor reacting to code in the player.cs file, as per Matthew's instructions above. However, I still can't seem to get basic keybindings to work. Any suggestions?
#10
04/24/2006 (2:44 pm)
function Player::onLevelLoaded(%this, %scenegraph)
{
   $Player = %this;
}

then in your keybind functions manipulate the $player object's velocity :)
#11
04/24/2006 (5:56 pm)
Matthew and Mike,

Thanks to both of you for your replies. Between your explanations, lots of time spent reading through the forums, and even more time spent over the last week and a half playing around with T2D, I feel like I am making at least some progress. It's slow, but then I never thought it was going to be easy.

Thanks again. I am sure I will be back with more questions.
#12
04/24/2006 (6:23 pm)
That worked like a charm. It seems silly, but making the GarageGames logo bounce across my screen constitutes a major breakthrough in figuring out how to create my game, especially after several days of confused hacking. Thanks for the help!
#13
04/25/2006 (8:45 am)
Glad it worked!

In the next release there is a full on tutorial in making a fish mini-game with the Level Builder... in fact I posted the movement section in the TGB Mini-Game Bounty thread here... feel free to check it out.