[SOLVED] C++ instead of TorqueScript
by Tomas Lazaro · in Torque Game Builder · 12/21/2010 (2:56 am) · 12 replies
Hi everyone, this is my first post here.
I'm a professional game developer/programmer used to code in C++ so I licensed Torque2D Pro to get the source code as well.
Compiling TorqueGameBuilder was really simple to build as the projects are already setup properly (at least for Visual C++ 2008).
But I'm having trouble finding documentation regarding integration with C++.
1. I want to create a new C++ project for my game and I'm wondering how to proceed. I'm assuming cloning the TorqueGameBuilder project is the best bet since it is like a game.
2. How does the TGB interact with my C++ code? Can I expose functionality like 'Classes' to be linked in the editor made in C++ instead of TorqueScript? Can I do the same from C++ as from TorqueScript?
Has anyone done that before and has a few tips or can point to some documentation that describes how to do that?
Thanks
Tomás
I'm a professional game developer/programmer used to code in C++ so I licensed Torque2D Pro to get the source code as well.
Compiling TorqueGameBuilder was really simple to build as the projects are already setup properly (at least for Visual C++ 2008).
But I'm having trouble finding documentation regarding integration with C++.
1. I want to create a new C++ project for my game and I'm wondering how to proceed. I'm assuming cloning the TorqueGameBuilder project is the best bet since it is like a game.
2. How does the TGB interact with my C++ code? Can I expose functionality like 'Classes' to be linked in the editor made in C++ instead of TorqueScript? Can I do the same from C++ as from TorqueScript?
Has anyone done that before and has a few tips or can point to some documentation that describes how to do that?
Thanks
Tomás
About the author
Professional Game Developer/Programmer
#2
Here is what I've got so far:
I placed myFish.cc and myFish.h in "..\engine\source\T2D"
I created a myFish class like this, not sure it was necessary but it should work I guess:
myFish.h:
... header stuff ...
class myFish : public t2dAnimatedSprite {
typedef t2dAnimatedSprite Parent;
public:
myFish();
virtual ~myFish();
/// Declare Console Object.
DECLARE_CONOBJECT(myFish);
};
Then on the myFish.cc:
...header stuff...
IMPLEMENT_CONOBJECT(myFish);
myFish::myFish() : t2dAnimatedSprite(){}
myFish::~myFish(){}
ConsoleMethod(myFish, workIt, void, 0, 1, "nothing") {
Con::printf("c++ worked!");
}
Then on a script I added this:
function myFish::onLevelLoaded(%this, %scenegraph) {
workIt();
}
When I run the game the "workIt" function is not found.
The in-game console says:
"[...] Unable to find function workIt"
Any ideas?
Thanks!
12/28/2010 (1:04 am)
I've been playing around with this stuff but I was not able to make it work :(Here is what I've got so far:
I placed myFish.cc and myFish.h in "..\engine\source\T2D"
I created a myFish class like this, not sure it was necessary but it should work I guess:
myFish.h:
... header stuff ...
class myFish : public t2dAnimatedSprite {
typedef t2dAnimatedSprite Parent;
public:
myFish();
virtual ~myFish();
/// Declare Console Object.
DECLARE_CONOBJECT(myFish);
};
Then on the myFish.cc:
...header stuff...
IMPLEMENT_CONOBJECT(myFish);
myFish::myFish() : t2dAnimatedSprite(){}
myFish::~myFish(){}
ConsoleMethod(myFish, workIt, void, 0, 1, "nothing") {
Con::printf("c++ worked!");
}
Then on a script I added this:
function myFish::onLevelLoaded(%this, %scenegraph) {
workIt();
}
When I run the game the "workIt" function is not found.
The in-game console says:
"[...] Unable to find function workIt"
Any ideas?
Thanks!
#3
12/28/2010 (1:53 pm)
The C++ looks right. Try this in script:function myFish::onLevelLoaded(%this, %scenegraph)
{
%this.workIt();
}
#4
"[...] Unknown command workIt."
"Object myFish(1338) myFish -> t2dAnimatedSprite -> t2dSceneObject -> [...]"
I tried using a static sprite instead of an animated one, same result except the above hierarchy.
I also tried using Name, Class or SuperClass fields in the Scripting tab in the editor to declare the object "myFish" and didn't work either.
Do I need to register the 'myFish' class in the C++ code somehow? How is it discovered?
I even tried adding to myFish:
static void initPersistFields();
as explained in consoleObject.h
In the script I'm trying:
%this.workIt();
workIt();
myFish::workIt();
myFish.workIt();
I also tried simplifying the myFish code, removed constructors, tried inheriting from ConsoleObject directly but couldn't create a ConsoleMethod without it being a SimObject so I inherited from SimObject.
I run out of ideas :(
What do you think?
12/28/2010 (10:07 pm)
It still fails, it says:"[...] Unknown command workIt."
"Object myFish(1338) myFish -> t2dAnimatedSprite -> t2dSceneObject -> [...]"
I tried using a static sprite instead of an animated one, same result except the above hierarchy.
I also tried using Name, Class or SuperClass fields in the Scripting tab in the editor to declare the object "myFish" and didn't work either.
Do I need to register the 'myFish' class in the C++ code somehow? How is it discovered?
I even tried adding to myFish:
static void initPersistFields();
as explained in consoleObject.h
In the script I'm trying:
%this.workIt();
workIt();
myFish::workIt();
myFish.workIt();
I also tried simplifying the myFish code, removed constructors, tried inheriting from ConsoleObject directly but couldn't create a ConsoleMethod without it being a SimObject so I inherited from SimObject.
I run out of ideas :(
What do you think?
#5
new myFish();
I get the following error:
"... unable to instantiate non-conobject clas myFish."
So the C++ code isn't available at all right now...
12/29/2010 (1:27 am)
I even tried in TorqueScript:new myFish();
I get the following error:
"... unable to instantiate non-conobject clas myFish."
So the C++ code isn't available at all right now...
#6
I'm honestly not a C++ expert but I don't see anything wrong with what you wrote and if you successfully build a class with a ConsoleMethod in it, it should expose to script.
12/29/2010 (3:58 am)
Are you sure that your game is running the newly built exe?I'm honestly not a C++ expert but I don't see anything wrong with what you wrote and if you successfully build a class with a ConsoleMethod in it, it should expose to script.
#7
2) If TGB is open the build fails because it can't access the exe.
I'm pretty sure I'm running the right exe but perhaps there is something else I'm missing :/
I will triple check when I get home about the exe but that I don't think that's it.
12/29/2010 (2:15 pm)
1) Whenever I build TGB (in Release) and run it my firewall pops up and warns me the exe has been modified.2) If TGB is open the build fails because it can't access the exe.
I'm pretty sure I'm running the right exe but perhaps there is something else I'm missing :/
I will triple check when I get home about the exe but that I don't think that's it.
#8
Here is something I was provided regarding consoleMethods:
12/29/2010 (5:01 pm)
Looking more closely, I think your parameters are messed up in your console method as well.Here is something I was provided regarding consoleMethods:
Quote:
The minimum/maximum number of parameters is always "plus 2". Argument 0 is the object, Argument 1 is the function name, Argument 2 is the first argument, Argument 3 is the second argument, and so on.
What a ConsoleMethod gives you in return is access to the object created in script through the "object" variable. We can then call functions inside of FillGridModel using "object".
Everything comes into the ConsoleMethod as a string. That's why we are calling dAtoi a lot, it converts a string into an integer. You'll also see the use of dSscanf. That scans a string for the format specified and puts the result into the passed in "x" and "y" variables. (If your C/C++ is rusty, the "&" before those mean that we passed the variables in as pointers so that the dSscanf function could modify the values.)
#9
EDIT:
- Do I have to "import" or "include" something when in the script perhaps?
- Is "myFish" perhaps in some namespace I need to access properly?
- Do I have to make a reference somewhere to myFish.h somewhere else in the code base? (ConsoleObjects should be registered in their constructors if I understood correctly so I shouldn't do anything special)
12/29/2010 (5:10 pm)
I think I tried playing around with the min/max args in the console method, it still doesn't explain "new myFish();" failing.EDIT:
- Do I have to "import" or "include" something when in the script perhaps?
- Is "myFish" perhaps in some namespace I need to access properly?
- Do I have to make a reference somewhere to myFish.h somewhere else in the code base? (ConsoleObjects should be registered in their constructors if I understood correctly so I shouldn't do anything special)
#10
12/30/2010 (1:48 pm)
Quote:No, the code just needs to be in a script file that has been exec'd. If you're getting that error, then the compiler is reaching it.
- Do I have to "import" or "include" something when in the script perhaps?
Quote:Don't think so...
- Is "myFish" perhaps in some namespace I need to access properly?
Quote:Don't think so...
- Do I have to make a reference somewhere to myFish.h somewhere else in the code base?
Quote:I think in order to 'new' something in TorqueScript the object in C++ must extend a ScriptObject. Not sure there though. Where is "new myFish();" failing
...it still doesn't explain "new myFish();" failing.
#11
There were 2 problems:
1) The C++ code myFish.cc and myFish.h were added to the TorqueGameBuilder project. Regardless of file location it only gets included in the TGB executable but not in the game. The files must be added as part of the TGBGame project so they end up inside the game's exe!
2) My game project was created with the flag "Copy game executable to game path". (I don't know why I did that...). That means that even if I recompile the game's exe it won't affect the game unless I manually overwrite it with a new one. I removed that by deleting the specific exe and editing the file Game.torsion and changing the executable name to: <Executable>TGBGame.exe</Executable> that way it always uses the newly compiled one.
Everything else worked as expected, "%this.workIt();" was the correct usage. I tested declaring functions and static methods, instantiating the class via code or having an animated sprite inheriting from it, it all worked :).
Thanks Pattrick for your help! It's really nice to see that people care and help out quickly.
Cheers
12/30/2010 (9:44 pm)
Wow OMG fixed it :)There were 2 problems:
1) The C++ code myFish.cc and myFish.h were added to the TorqueGameBuilder project. Regardless of file location it only gets included in the TGB executable but not in the game. The files must be added as part of the TGBGame project so they end up inside the game's exe!
2) My game project was created with the flag "Copy game executable to game path". (I don't know why I did that...). That means that even if I recompile the game's exe it won't affect the game unless I manually overwrite it with a new one. I removed that by deleting the specific exe and editing the file Game.torsion and changing the executable name to: <Executable>TGBGame.exe</Executable> that way it always uses the newly compiled one.
Everything else worked as expected, "%this.workIt();" was the correct usage. I tested declaring functions and static methods, instantiating the class via code or having an animated sprite inheriting from it, it all worked :).
Thanks Pattrick for your help! It's really nice to see that people care and help out quickly.
Cheers
#12
12/31/2010 (5:54 pm)
Glad you got it working! Now on to game making and away from Visual Studio configuration. :)
Torque 3D Owner Ronny Bangsund
Torque Cheerleaders
You can do anything you can do in TorquesScript with C++, although some things that are easy in script require a wee bit more typing in some cases. Other times the C++ code might be easier.
TDN and these forums are your first step, but the resources section is guaranteed to have some gems too.