T2D Tutorial C++ Creating a Custom Object
by Matthew Langley · in Torque Game Builder · 05/01/2005 (10:49 pm) · 18 replies
T2D Tutorial C++ Creating a Custom Object
EDIT: sorry for the cross posts, figured it'd be viable in here as well
EDIT: sorry for the cross posts, figured it'd be viable in here as well
About the author
Was a GG Associate and then joined GG in 2005. Lead tool dev for T2D and T3D. In 2011 joined mobile company ngmoco/DeNA and spent about 4 years working game and server tech. 2014 joined startup Merigo Games developing server technology.
#2
I'd say yes and no... this would be a good starting point, but this tutorial is just a starting point, it shows you how to add certain types of functionality, doing something like A* is definately a bit more involved... and taking that and integrating A* into your gameplay is even more involed... everything you say is possible, though I'd split it up between script and C++... they function quite well together... and like Stephen Zepp said before, (I agree completely), that a truly skilled Torque developer knows how to use both well together...
If you go through this tutorial you'll understand the uses of Script and C++ a little better.
05/05/2005 (8:38 pm)
Hmm... well some good questions...Quote:from the information you have provided in this tutorial?
I'd say yes and no... this would be a good starting point, but this tutorial is just a starting point, it shows you how to add certain types of functionality, doing something like A* is definately a bit more involved... and taking that and integrating A* into your gameplay is even more involed... everything you say is possible, though I'd split it up between script and C++... they function quite well together... and like Stephen Zepp said before, (I agree completely), that a truly skilled Torque developer knows how to use both well together...
If you go through this tutorial you'll understand the uses of Script and C++ a little better.
#3
05/05/2005 (9:23 pm)
Thanks Matthew - I guess I'll just stick with your C++ tutorials and TorqueScript for now until TDN gets set up and running!:) Won't waste anymore of your time addressing this any further until the day appropriate engine source docs arrive - you're much too productive to be bothered with this right now!:) Keep up the good stuff - and don't stay up too late! lol
#4
I say go through this tutorial and start thinking of ways you can extend it or ways you can customize your own T2D C++ object off of fxSceneObject2D... maybe look through other objects, like staticSprite... animatedSprite really helped me figure out the use of the "integrateObject" function... check that out...
Also check out John Vanderbeck's A* in TScript resource, thats a great starting point, he did some great work on that. Maybe ask him for some advice... do a little research online if needed... best thing to do is start small... I always tell myself "if all else fails, scale down". lol I find I do that quite often... you usually think of the end result first, the fancy completed compilation, though quite often it takes many little steps to get there, so pick a goal out and go at it one by one. The source code is a great place to look and learn! Melv commented it quite well. Just go at it step by step.
Another suggestion, develop a high level list. I'll give you an example of one of mine
This was the first thing I did. Now it didn't end up like that ( in fact I have a lot still to go ) but when I got lost or couldn't think of things quite clearly I just looked back at this and worked on another funciton/variable. Things like this help prevent you from re-designing the whole objective each time you approach something.
And again ask for help if needed, I'd be more than happy to look things up and figure things out for/with you. I know there are some others here that would too.
05/05/2005 (9:55 pm)
No problem at all, in fact feel free to ask any questions, or e-mail any questions :) I like keeping busy (very busy) though I like having lots of things to do so I can fall back on something if one thing starts getting to me. Plus theres always time to help out a T2D community friend :)I say go through this tutorial and start thinking of ways you can extend it or ways you can customize your own T2D C++ object off of fxSceneObject2D... maybe look through other objects, like staticSprite... animatedSprite really helped me figure out the use of the "integrateObject" function... check that out...
Also check out John Vanderbeck's A* in TScript resource, thats a great starting point, he did some great work on that. Maybe ask him for some advice... do a little research online if needed... best thing to do is start small... I always tell myself "if all else fails, scale down". lol I find I do that quite often... you usually think of the end result first, the fancy completed compilation, though quite often it takes many little steps to get there, so pick a goal out and go at it one by one. The source code is a great place to look and learn! Melv commented it quite well. Just go at it step by step.
Another suggestion, develop a high level list. I'll give you an example of one of mine
- Inventory System - High Level Design C++ Objects: ItemIcon (child of fxSceneObject2D) InventorySet (*possibly* child of tileLayer) Script Objects: Inventory manager (middle man between TorqueDB and script/visual display) Function and DataBreakdown Design C++ Objects: ItemIcon Variables: itemName imageMap frame currentSlot previousSlot InventorySet currentState previousState Functions: pickUp drop returnToPreviousSlot getPreviousSlot getCurrentSlot getWorldPosition getSlotPosition setImageMap setToNewSlot setInventorySet setItemName snapToSlot (callbacks) onPickUp onDrop onReturnToPreviousSlot onSetToNewSlot onSetInventorySet onOutOfSetBounds onSetItemName onSnapToSlot InventorySet Variables: name rows columns slotCount slotContents (two dimensional array) tileLayer Functions: dumpSetToDB (specially made for TorqueDB) getSlotAtWorldPosition getSlotWorldPosition getSlotLocalPosition getTileWorldPosition getTileLocalPosition getTileLayer setSlotIcon setSlotTile setTileLayer (callbacks) onSetSlotIcon onSetSlotTile onSetTileLayer
This was the first thing I did. Now it didn't end up like that ( in fact I have a lot still to go ) but when I got lost or couldn't think of things quite clearly I just looked back at this and worked on another funciton/variable. Things like this help prevent you from re-designing the whole objective each time you approach something.
And again ask for help if needed, I'd be more than happy to look things up and figure things out for/with you. I know there are some others here that would too.
#5
05/05/2005 (10:40 pm)
Thanks for the list and the valuable pointers Matthew - time I stop finishing what I haven't even started yet!lol No, seriously, C++ and T2D are going to take some time to learn and adapt to - so I may as well take your good advice and learn things thoroughly before committing myself to anything in particular - even if that means a delay of a few months or so before committing myself to any large-scale projects of any sort!:) Better now than never I guess!lol
#6
05/10/2005 (12:35 pm)
Do you have a copy of the resource? I cant seem to get it to load.
#7
05/12/2005 (9:24 am)
I've converted this to an HTML file so if anyone needs it post here and drop me an e-mail. Post here so I can confirm your a T2D developer please :)
#8
05/16/2005 (11:55 am)
Thanks, I'd love to take a look at your tutorial, as I'm making engine modifications currently. I've sent you an e-mail.
#9
05/16/2005 (2:03 pm)
Its been sent :) If you don't get it let me know.
#10
tim.doty gmail.com
05/16/2005 (3:34 pm)
Hey Matt, would you mind sending me a copy? I could always stand to read something informative as I slog through the conversion from TorqueScript to C++ objects (most of which has gone well, but there are some design considerations I'm pondering carefully to make sure I implement effectively).tim.doty gmail.com
#12
05/17/2005 (8:35 am)
Sent one both of your ways
#13
I noticed that you didn't change the identifier for the new class and I'm assuming that was an omission? What I'm referring to is:
It looks to me like that is inserting a (presumably) unique ID tag of 2DSS, as in Torque 2D Static Sprite. I'm guessing here not having reviewed the relevant code, but I'd expect that this would have an impact on saving/loading state.
Thanks again for the tutorial!
05/17/2005 (9:10 am)
Thanks. At first I'd thought I might be able to avoid really touching the T2D specific code, but such is not the case and it always helps to have someone's notes on this.I noticed that you didn't change the identifier for the new class and I'm assuming that was an omission? What I'm referring to is:
//-----------------------------------------------------------------------------
// Constructor.
//-----------------------------------------------------------------------------
itemIcon2D::itemIcon2D() : T2D_Stream_HeaderID(makeFourCCTag('2','D','S','S')),It looks to me like that is inserting a (presumably) unique ID tag of 2DSS, as in Torque 2D Static Sprite. I'm guessing here not having reviewed the relevant code, but I'd expect that this would have an impact on saving/loading state.
Thanks again for the tutorial!
#14
I originally did this tutorial in a 6 hour straight session lol... I wanted to do more but scaled it down and missed that. Its very helpful that you caught this.
05/17/2005 (9:51 am)
You are correct. Thank you for catching that :)I originally did this tutorial in a 6 hour straight session lol... I wanted to do more but scaled it down and missed that. Its very helpful that you caught this.
#15
05/17/2005 (9:54 am)
Fortunately no more than 3 minutes ago I submited a Tutorial Pack with all my tutorials, thanks for catching this :) That way I catch it before it goes further out.
#16
05/17/2005 (10:16 am)
You're welcome. I appreciate all the work you put into tutorials. You're making them faster than I have time to read them.
#17
Thanks!
05/17/2005 (10:40 am)
Okay, I made it through the first read on your tutorial. Looks good. The only thing that made me pause (and that was probably because I was reading through rather than working through) was the bit about Con::executef() -- I got it reading through that part a bit more carefully a second time. When I get to subclassing T2D objects I know I'll be referring back to this. I think you hit a good pace for shifting from line-by-line explanations to thats-already-been-covered brevity. There are two things I would suggest: 1) spell check and 2) formatting consistency (particularly tabbed tables). But your content is very good.Thanks!
#18
Con::executef() is insanely usefull... thats how you can call back to the script, definately one to search the T2D source for more examples. Unfortuantely in my tutorial I never have you utilize the callback, lol after 6 hours I must've gotten delerious and forgot I included it. I think I'll extend it by adding fu nction callbacks, maybe that will be in the next tutorial, building off of it.
one of the things I worry about is whether I'm being too thorough in some places and not thorough enough in others, so I sometimes will even go back and re-read through it and reformat it lol... I also try not to waste peoples time, or mine, on things I've repeated 5 times, no use repeating it a sixth (except certain things).
Lol, your definately right there. I really am good at english and sentence structure, just when I'm switching between code and text the wires get crossed lol. I type all of the text used in the tutorials in notepad (since it doesnt take any memory overhead and some of the pcs I use for the tutorials are pretty weak) so I feared putting it into word to get errors on all the formatting tags... I add board formatting tags as I type.. like [ b] [ /b] (without the spaces), etc... but after thinking about it (ok it only took 5 seconds to realize this) I can just add those tags to the dictionary and correct spelling without having to sift through all the tags that will trigger it.
One good things is I always re-create the program as I create the tutorial, so even if I've got a good deal of typos and formatting errors, the code will work :) lol though I always tell myself I need to go through and edit it.
your right, I'm still trying to figure out the best format, I'm closer, but not quite there. Also training myself to use spaces and not tabs (lol its a hard switch, especially at the speed I go through these tutorials).
05/18/2005 (9:21 am)
Thanks for the comments, keep them coming, anything you notice or can suggest just helps :)Con::executef() is insanely usefull... thats how you can call back to the script, definately one to search the T2D source for more examples. Unfortuantely in my tutorial I never have you utilize the callback, lol after 6 hours I must've gotten delerious and forgot I included it. I think I'll extend it by adding fu nction callbacks, maybe that will be in the next tutorial, building off of it.
Quote:I think you hit a good pace for shifting from line-by-line explanations to thats-already-been-covered brevity
one of the things I worry about is whether I'm being too thorough in some places and not thorough enough in others, so I sometimes will even go back and re-read through it and reformat it lol... I also try not to waste peoples time, or mine, on things I've repeated 5 times, no use repeating it a sixth (except certain things).
Quote:1) spell check and
Lol, your definately right there. I really am good at english and sentence structure, just when I'm switching between code and text the wires get crossed lol. I type all of the text used in the tutorials in notepad (since it doesnt take any memory overhead and some of the pcs I use for the tutorials are pretty weak) so I feared putting it into word to get errors on all the formatting tags... I add board formatting tags as I type.. like [ b] [ /b] (without the spaces), etc... but after thinking about it (ok it only took 5 seconds to realize this) I can just add those tags to the dictionary and correct spelling without having to sift through all the tags that will trigger it.
One good things is I always re-create the program as I create the tutorial, so even if I've got a good deal of typos and formatting errors, the code will work :) lol though I always tell myself I need to go through and edit it.
Quote:2) formatting consistency (particularly tabbed tables). But your content is very good.
your right, I'm still trying to figure out the best format, I'm closer, but not quite there. Also training myself to use spaces and not tabs (lol its a hard switch, especially at the speed I go through these tutorials).
Torque Owner Gabor Borbely