TGB/Tutorials/Fill Battle/The Emperors New Clothes

From TDN

This page is a Work In Progress.

Contents

The Emperor's New Clothes

Open up FillBattle\engine\source. Add a new directory there called FillBattle.

Create 2 files in the FillBattle directory: fillGridModel.h and fillGridModel.cc.

Contents of fillGridModel.h:

#ifndef _FILLGRIDMODEL_H_
#define _FILLGRIDMODEL_H_

#ifndef _SCRIPTOBJECTS_H_
#include "console/scriptObjects.h"
#endif

class FillGridModel : public ScriptObject
{
   typedef ScriptObject Parent;

public:
   FillGridModel();
   bool onAdd();
   void onRemove();

   DECLARE_CONOBJECT(FillGridModel);
};

#endif

Contents of fillGridModel.cc:

#include "platform/platform.h"
#include "console/simBase.h"
#include "console/consoleTypes.h"
#include "console/simBase.h"

#include "FillBattle/fillGridModel.h"

IMPLEMENT_CONOBJECT(FillGridModel);

FillGridModel::FillGridModel()
: ScriptObject()
{
}

bool FillGridModel::onAdd()
{
  if (!Parent::onAdd())
    return false;

  return true;
}

void FillGridModel::onRemove()
{
  Parent::onRemove();
}

In Visual C++, right click on TGBGame -> Source Files in the "Solution Explorer" and click Add -> New Filter. A folder called "NewFilter1" will appear. Rename this to "FillBattle".

Right click on TGBGame -> Source Files -> FillBattle and select Add -> Existing Item... Browse to engine/source/FillBattle, select both files (fillGridModel.{cc|h}), and hit "Add".

Repeat the last 2 paragraphs but under the TorqueGameBuilder folder (so, TorqueGameBuilder -> Source Files, and so on…)

Why add it to both? Well, it's not too important right now. But eventually, you might add all new types of scene objects and you'll want the game builder to know about those. It's just a good habit to get in... if you add it to TGBGame, add it to TorqueGameBuilder.

Build -> Build Solution.

Success!

This class does absolutely nothing. So while I'm trying to give new clothes to your script, I've given you nothing. Let's go over what does happen here.

First, we created a new directory for your stuff. When a new Torque2D 1.7.x comes out, you'll have this nice separate folder that you can freely drop in to a new project.

I Do Declare!

Next, we created the declaration file. It does a few things:

#ifndef _FILLGRIDMODEL_H_
#define _FILLGRIDMODEL_H_

If you make a new class in the future, make sure you rename these to whatever your new class name is. It's required so that you never re-declare the same class twice, a big no-no in C++. In short, it says that if the string _FILLGRIDMODEL_H_ isn't defined, then do everything to the #ENDIF (down below). Microsoft allows you to just put in "#pragma once" at the top of the file, but this is safer for Mac.

#ifndef _SCRIPTOBJECTS_H_
#include "console/scriptObjects.h"
#endif

Here we're just getting the declaratration for the ScriptObject class which we want to use as the base of our class. The "#ifndef/#endif" is totally unnecessary, but it makes compiles go 1 millisecond faster (cue eyes rolling).

class FillGridModel : public ScriptObject
{
   typedef ScriptObject Parent;

public:
   FillGridModel();
   bool onAdd();
   void onRemove();

   DECLARE_CONOBJECT(FillGridModel);
};

#endif

We are a ScriptObject which in turn is a SimObject. SimObject does a few things for us, namely giving the class a unique number when it is new'd up, and allows us to use "class=..." and "superclass=..." in our scripting. ScriptObject tells SimObject that we want the "class/superclass" functionality, plus it gives us the "onAdd" and "onRemove" callbacks that you see so often.

We also choose to use the "onAdd" and "onRemove" functionality, but the reason will be explained in the next lesson.

The "typedef" and "DECLARE_CONOBJECT" are needed for some of the magic of linking C++ to TorqueScript.

Clearly Defined

#include "platform/platform.h"
#include "console/simBase.h"
#include "console/consoleTypes.h"
#include "console/simBase.h"

These includes give us a lot of good stuff: messages to the console, the ability to call into the scripts, and the ability to be platform independent.

#include "FillBattle/fillGridModel.h"

Of course, we need to know what we are trying to define.

IMPLEMENT_CONOBJECT(FillGridModel);

More scripting magic.

FillGridModel::FillGridModel()
: ScriptObject()
{
}

Your standard constructor, called whenever you make the object in C++ or TorqueScript. The line ": ScriptObject()" isn't necessary, but I can sometimes be a purist when it comes to C++... I like to see my base classes constructed.

bool FillGridModel::onAdd()
{
  if (!Parent::onAdd())
    return false;

  return true;
}

Here, we just let our Parent (remember the typedef?) handle the "onAdd". If you go to console/ScriptObjects.cc, you'll see it calls into TorqueScript with "::onAdd".

void FillGridModel::onRemove()
{
  Parent::onRemove();
}

Same as above, but with the "onRemove" callback.

Is It Alive?

Go to your game directory (FillBattle\games\FillBattle). Run the game and open up the console (simply press ~). Type the following in:

$a = new FillGridModel();
$a.dump();
$a.delete();

There you go! No functionality, but no error message. TorqueScript knows about your class and is ready to get started!

Complete!

Return to the Fill Battle Home!