Jump to content

How can i add 3rd party library into torquescript?


Recommended Posts

Adding a library is pretty simple, if your familiar with compiling torque. In CMAKE, it is relatively easy, just look at how BulletPhysics is added. You just add the paths to the libraries. In VS it is even more simple I believe, its a matter of rigjtclicking tour solution window and linking your libs. The real question is how these libraries will be integrated into torque. This I believe will take a bit of understanding of c++ and how to debug.

Link to comment
Share on other sites

Torque3d uses a marshalling (?) thing to comunicate between torquescript and C++, I think.

What you want is, probably, to use "DefineConsoleFunction".

I don't understand much the C++ base to really help you directly, sorry, but what I do is to check the source code for examples.

If you know what function you want to check you could do a global search for all the source code for key words with things like notepad++.

For reference, the definition of the macro thing is in


and some examples could be found for example in


Link to comment
Share on other sites

Sorry guys! Halloween was busy for me!

Ok, so yeah the just of what everyone is saying is correct.

If you want to add a new library and then have it be accessable in script, you have a few steps to do:

1) add it to the engine via a library in CMAKE, and then compile it into the engine. As Jason said, you can look at the existing libraries for how to do that(though a wiki page should get made about it). But you'll have a file for the library in cmake, and then add a reference to that via addLibrary(myLibrary) in the torque3d.cmake file so it gets actually included on generation.

2) Once it's in and compiling, you'll want to have a way to actually call it to do things. This is where what @irei1as said comes in.

You have 2 main macro functions you can add to a file in the engine so that you can call stuff from script:

DefineEngineMethod and DefineConsoleFunction.

DefineEngineMethod basically attaches a script function to the namespace/object class in question.

So if you made a new object derived from ScriptObject to do whatever your library does, you could then add something like:


DefineEngineMethod(MyNewObject, myFunction, void, (Point3F position), (Point3F::Zero), "This is a test function that calls a function in myNewObject")


As you can see from above, if we have our new ScriptObject derived class in the engine - MyNewObject in this case, this will add/expose a script-side function 'myFunction' to any object of MyNewObject type.

So in script, you could then do:


%myObject = new MyNewObject();


And it will go 'ok, so %myObject is of the class MyNewObject, and yep, there's a script function myFunction, so we'll pass %aPosition in and that gets turned into the 'position' argument.

This then calls object->myEngineSideFunction (object in this case being the object you're calling this script function on, and myEngineSideFunction being whatever function in that class you're trying to call) and we pass our position argument into it.

If you're not making a new object class and just want to call something, like say, a math function, you do DefineConsoleFunction, which would look like this:


DefineConsoleFunction(myFunction, Point3F, (Point3F position), (Point3F::Zero), "This is a test function")
    Point3F magicNumber = Point3F(10,9,100);
   return magicNumber + position;


As you can see, it's similar, but not identical. We don't have the MyNewObject bit in there, because this doesn't associate to any given class or namespace, it exists in the global namespace.

We also have a different return type after the function name, instead of void like in the prior example, this one will return a Point3F vector. We similarly pass in a vector argument.

So in script, you would do:


%result = myFunction(%aPosition);


We don't have a namespace, so we call the function directly, passing in our argument and it'll return a result.

In practice, you would have your function there call into your library's functions to do whatever you're trying to accomplish, but the setup is the same.

Hopefully that explains it! Let me know if you have any other questions or something doesn't make sense :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...