Game Development Community

Adding a function to TGB - how?

by baylor wetzel · in Torque Game Builder · 10/09/2007 (7:49 pm) · 4 replies

I'd like to add a function and perhaps an object type to TGB. But i haven't got a clue where to start

i'm using TGB 1.1.3 for Windows. i have the source code. i have Visual Studio 2005. i'm considering using TGB for a research project that does some semi-intensive math stuff (mostly entropy calculations) and while i might be able to do that in TorqueScript, i'm guessing it's better to write that portion of the code in C. i'd also like to expose hash tables (well, std::map)

#1
10/10/2007 (2:01 am)
When you say "add a function" do you mean to interface between script and the engine?

Have a look in the source for ConsoleMethod and ConsoleFunction, depending on whether the function you wish to expose is part of a class or not.

If you click Documentation/TGE at the top of this page, there's a link to download the source docs. Within that is a little more detail on the engine/script interface - see Reference/console/console/overview.

There are also plenty of examples in the engine to base any new methods.
#2
10/10/2007 (2:31 am)
Make sure you double-check the EULA as well... I know there's some issues for using the product outside the scope of intention, but it may not apply to you.
#3
10/10/2007 (2:02 pm)
For starters, check out tdn.garagegames.com/wiki/Torque_2D/Getting_Started/C_Tutorial

Adding functions to the engine is rather trivial, you just have to add the function definitions, etc etc.

However, when you add a function that you would like to be called from when you execute you must create a ConsoleMethod call too. I cannot remember the exact code off the top of my head, but I recommend that you check out the existing functions within the source before you start writing your own.

I will give you a quick break down of what you must do, however.

Create a normal ol' C++ function, you say you want to do some math functions, so you would write the script here. For example:

F32 MyClass::DoMyMath(F32 param1, F32 param2)
{
    return param1+param2;
}

The next step is to create the ConsoleMethod, this is the function that is called from inside TGB. It is a good idea to use the same function name as the one you created, DoMyMath, with however many arguements you want + 2, i.e. 4 in this case. So, when you call the ConsoleMethod, you then just call *your* function from within it:

ConsoleMethod(MyClass, DoMyMath, F32, 4, 4, "<Class>, <FunctionName>, <ReturnType>, <MinArgs>, <MaxArgs>, <Usage")
{
    return DoMyMath(dAtof(argv[2]), dAtof(argv[3]));
}

Get the idea? Hope this helped a little bit, but I recommend that you check out the existing code files. Good luck!
#4
10/21/2007 (10:25 pm)
Reading the code files was painful - there's so many of them! But you were right, this was easy, once i got past some problems (notably that the TGB 1.5 solution doesn't build; i eventually figured out that the configuration manager is set to not compile glu2d3d.lib; doh!)

i've only done a Hello World class but it's enough to prove to me that this is a viable approach for my project. i thought maybe someone else would benefit from seeing my world's smallest TGB class so i'm going to post that over in the C++ forum (i probably should have posted this question there to begin with)

Thanks for all the help everyone!