Pathfinder written in C++ - arguments
by kimerafusion · in Torque 2D Beginner · 04/13/2014 (1:43 pm) · 3 replies
Hey there,
I'm here because I need to use a pathfinder in my game. I have already written this pathfinder in C++, which is a class that requires a grid (basically a 2*2 table) in its constructor, and returns a "Path" object, which is slightly more than a table of vectors. The problem here is that I have absolutely no idea how I could add this class to my Torque project, and how to use it properly. The problem remains in how can I pass arguments to my C++ class? Do I have to transform everything into strings?
I'm here because I need to use a pathfinder in my game. I have already written this pathfinder in C++, which is a class that requires a grid (basically a 2*2 table) in its constructor, and returns a "Path" object, which is slightly more than a table of vectors. The problem here is that I have absolutely no idea how I could add this class to my Torque project, and how to use it properly. The problem remains in how can I pass arguments to my C++ class? Do I have to transform everything into strings?
#2
Moreover, I don't get how to declare a new class. I'm currently working on an RPG, I would like to store the equipment of the characters using an array of objects that aren't really objects people will see when the inventory won't be opened, but I don't understand how to create this class, to say to the game something like "the class Item will contain this this and this variable".
04/16/2014 (2:54 pm)
Thank you for your answer, however I don't really get how to add these C++ classes to my own game. Where do I have to put my class files (.h, .cpp) so the game will know where is the class I'm supposed to create?Moreover, I don't get how to declare a new class. I'm currently working on an RPG, I would like to store the equipment of the characters using an array of objects that aren't really objects people will see when the inventory won't be opened, but I don't understand how to create this class, to say to the game something like "the class Item will contain this this and this variable".
#3
The key to plugging them into your game comes from defining what existing class it is derived from. The example in the link I gave is from SimObject, which is the most basic type in the engine. ScriptObject or SceneObject are other good classes to derive from, depending on what type of custom object you are making.
I don't want to totally confuse you, but if you are up for it - the old, commercial engine had an interesting tutorial that covers implementing a custom C++ class. You won't be able to get the code in the tutorial to work with T2D MIT as written, but it could help in understanding the general concept of custom code additions.
tdn.garagegames.com/wiki/TGB/Tutorials/Fill_Battle (have to sign in to TDN to see)
Also consider prototyping your game in script as much as possible before writing custom C++ code. Maybe I didn't completely understand what you want to accomplish with your inventory example but my first thought would be to simply use ScriptObjects and TAML to store and persist equipment data.
04/18/2014 (6:42 am)
You can put your C++ files anywhere you want in the source folder - it is simply a matter of adding them to your compiler's project/solution.The key to plugging them into your game comes from defining what existing class it is derived from. The example in the link I gave is from SimObject, which is the most basic type in the engine. ScriptObject or SceneObject are other good classes to derive from, depending on what type of custom object you are making.
I don't want to totally confuse you, but if you are up for it - the old, commercial engine had an interesting tutorial that covers implementing a custom C++ class. You won't be able to get the code in the tutorial to work with T2D MIT as written, but it could help in understanding the general concept of custom code additions.
tdn.garagegames.com/wiki/TGB/Tutorials/Fill_Battle (have to sign in to TDN to see)
Also consider prototyping your game in script as much as possible before writing custom C++ code. Maybe I didn't completely understand what you want to accomplish with your inventory example but my first thought would be to simply use ScriptObjects and TAML to store and persist equipment data.
Associate Mike Lilligreen
Retired T2Der
www.garagegames.com/community/forums/viewthread/133395
In TorqueScript, everything is a string but this does not mean you have to pass everything to C++ as a string as well. There are functions that convert a string to an int, float, bool, etc. There are plenty of good examples to look through in the engine/source/2d folder - all methods exposed to TorqueScript are in their own ScriptBinding header file. Here's an example:
ConsoleMethodWithDocs(ImageAsset, getExplicitCellHeight, ConsoleInt, 3, 3, (cell)) { S32 cellIndex; // Was it a number or a string? if (!dIsalpha(*argv[2])) { // Using cell index. cellIndex = dAtoi(argv[2]); return object->getExplicitCellHeight(cellIndex); } else { // Using cell name. ImageAsset::FrameArea& frameRegion = object->getCellByName(argv[2]); return frameRegion.mPixelArea.mPixelHeight; } }