How can i add 3rd party library into torquescript?

Expanding and utilizing the engine via C++.
7 posts Page 1 of 1
Posts: 11
Joined: Thu Jun 18, 2015 5:02 am
by Zarosath » Fri Oct 27, 2017 2:19 am
Hi, i would like to utilize OpenNN into my torque3d project but i'm a newb on c++.. is it easy at all to include the lib into script?
Jason Campbell
Posts: 332
Joined: Fri Feb 13, 2015 2:51 am
by Jason Campbell » Sun Oct 29, 2017 6:42 pm
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.
Posts: 11
Joined: Thu Jun 18, 2015 5:02 am
by Zarosath » Tue Oct 31, 2017 1:28 am
Can you tell me Whether the library may be accessible via function pointers in TorqueScript?
Jason Campbell
Posts: 332
Joined: Fri Feb 13, 2015 2:51 am
by Jason Campbell » Wed Nov 01, 2017 2:24 am
I wish Azaezel or Timmy would pop in and answer because I am no expert. I feel that with a bit of work you could integrate OpenNN into Torque but as I said it would most likely involve a grasp of C++.
Posts: 84
Joined: Fri Feb 27, 2015 5:13 pm
by irei1as » Thu Nov 02, 2017 11:40 am
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
Steering Committee
Steering Committee
Posts: 937
Joined: Tue Feb 03, 2015 9:49 pm
by JeffR » Fri Nov 03, 2017 3:26 pm
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 :)
Posts: 11
Joined: Thu Jun 18, 2015 5:02 am
by Zarosath » Thu Nov 09, 2017 11:02 pm
I think that will be sufficient, Excellent information dandyman, It requires a certain understanding level of c++ but otherwise quite straightforward..
7 posts Page 1 of 1

Who is online

Users browsing this forum: No registered users and 15 guests