### How can i add 3rd party library into torquescript?

Expanding and utilizing the engine via C++.

#### How can i add 3rd party library into torquescript?

Zarosath
Posts: 10
Joined: Thu Jun 18, 2015 5:02 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?

#### Re: How can i add 3rd party library into torquescript?

Jason Campbell
Posts: 271
Joined: Fri Feb 13, 2015 2:51 am

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.

#### Re: How can i add 3rd party library into torquescript?

Zarosath
Posts: 10
Joined: Thu Jun 18, 2015 5:02 am
Can you tell me Whether the library may be accessible via function pointers in TorqueScript?

#### Re: How can i add 3rd party library into torquescript?

Jason Campbell
Posts: 271
Joined: Fri Feb 13, 2015 2:51 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++.

#### Re: How can i add 3rd party library into torquescript?

irei1as
Posts: 76
Joined: Fri Feb 27, 2015 5:13 pm
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
source\console\engineAPI.h
and some examples could be found for example in
source\console\consoleFunctions.cpp

#### Re: How can i add 3rd party library into torquescript?

JeffR
Steering Committee
Posts: 840
Joined: Tue Feb 03, 2015 9:49 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")
{
object->myEngineSideFunction(position);
}
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();
%myObject.myFunction(%aPosition);
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

#### Re: How can i add 3rd party library into torquescript?

Zarosath
Posts: 10
Joined: Thu Jun 18, 2015 5:02 am
I think that will be sufficient, Excellent information dandyman, It requires a certain understanding level of c++ but otherwise quite straightforward..

#### Who is online

Users browsing this forum: No registered users and 1 guest