Game Development Community

calling a .lib function from torque script?

by pilotcap232 · in Torque 3D Beginner · 09/26/2012 (2:44 pm) · 9 replies

sup guys and galls,

got a noob question:
i got a some source files that i can compile as lib files and i would like to include the lib file in my build project in torque3d


now my question is:

how do i call a function from my compiled file.lib included in my torque build?


can anyone show me a small Torque script example, or is there something i need to do before i can call functions from torque from the .lib file?


suggestions are welcome, thanks in advanced

#1
09/26/2012 (5:45 pm)
a .lib is a library that will be compiled into the engine, to access it from torque script you need to expose it to the scripting engine.

I'm no c++ expert but when i had to create new console commands, it was somewhere in:
"engine/console/consolefunctions.cpp"

ConsoleFunction(name,returnType,minArgs,maxArgs,usage1)

Example:
ConsoleFunction(MyFunction, bool, 1, 1, "blah blah!")
{
	return false ;
}
#2
09/26/2012 (6:54 pm)
thanks Kyrah,

ok, then i am confused,

so whatever code function you add to the engine, you need to expose it to conlosefunctions so it can be used with torquescript?

so in other words all extra added functions needs to be in consolefunctions.cpp?


#3
09/26/2012 (8:44 pm)
Ok, so let's assume you have a function in myLib.lib under a certain namespace (myNamespace).

What you want to do is create a function declaration that uses myNamespace::myFunction. To get this into the engine, is a piece of cake.

I use a separate file for all of my library defined functions, it looks a bit like this:
//Define T3D Console
#include "platform/platform.h"
#include "console/console.h"
#include "console/consoleInternal.h"
#include "console/engineAPI.h"

//Include External References
using namespace MyNamespace; //use your namespace here, alternatively you can just link your library and call a class declaration. But a namespace will keep things nice and clean.

//Two basic examples:
// Void Call (No Parameters)
DefineEngineFunction(BasisPrintStuff, void, (),, "(void) Print something...") {
   Con::printf("Hello from Engine!!!");
}

//Returned Call (With Parameters)
DefineEngineFunction(MyFunction, const char *, (const char * input),, "Work with parameters!") {
   //Let's assume you have a MyNamespace::AddHello(const char *i);
   std::string myMessage;
   myMessage = MyNamespace::AddHello(input);

   //to properly convert std::string data into const char * output, use the following:
   char *str = Con::getReturnBuffer(myMessage.size() +1);  
   dStrcpy(str, myMessage.c_str());  

   return str;
}

And to answer your other question, you do not need to put your functions into consoleFunctions.cpp. You can have them in any file you want. You just need to expose the file to the T3D console (see the first four #include lines in my example code)

Pretty simple stuff here... Just need to apply what you already know ;)
#4
09/27/2012 (2:09 am)
What's the difference between DefineEngineFunction and ConsoleFunction?
#5
09/27/2012 (4:22 am)
DefineEngineFunction is the newer method of setting up ConsoleFunctions -- as well as other similar macros. It was done to make automatic documentation easier and much more robust. Backwards compatibility was maintained so you have a choice... but the new way is better.
#6
09/27/2012 (4:30 am)
Check out this link to EngineAPI.rtf that Rene wrote and uploaded.
#7
09/27/2012 (10:03 am)
I would recommend you use DefineEngineFunction and DefineEngineMethod whenever you can over the ConsoleXXX versions.

@pilotcap232:
Yes, you have to explicitly expose and functions or methods to script. This allows the game developer to control what is available for modification in TorqueScript, and what is hard coded within the source code.

In addition to using DefineEngineFunction, you could also wrap your library code within a SimObject derived class and expose the methods using DefineEngineMethod. This works well if you're creating a number of instances of something from your library. This lets your SimObject class worry about constructing and destructing you library objects.

- Dave
#8
09/27/2012 (10:29 am)
thanks guys,

ok, i think i am getting the hang and principle of it.

but, since the lib is quite extensive is there any more convenient way to expose the lib methods or functions without having do do all of them manually every single one?

you know, like when you script in python using import to use all the methods from that class etc..
or c# scripting using namespace to make use of all the methods available?

again, c++ is not my strongest and still getting the hang of it.

thanks in advanced




#9
09/28/2012 (11:33 am)
My suggestion would be to either expose the ones you pecifically want to use. Or to write your own objects/methods to make this lib do what you want, and expose just those few objects/methods.

Ideally you want the TorqueScript EngineFunctions to be as "plug n play" as possible, no point in doing "crud" work in TorqueScript.