Game Development Community

Components... how to?

by Craig Fortune · in iTorque 2D · 05/11/2009 (3:47 pm) · 4 replies

Right, heres the story:

I prototyped my game out in TGB to the extent I was happy to continue creating it for the iPhone. I then set up my dev environment and got iTGB successfully deploying to my iPhone, then I quickly discovered I wanted to move my scripts over to C++ components. This is where the fun starts/ends :)

After a little bit playing about I had very simple component up and running (just sets an angular velocity on an object). I had to do this by a little trial and error and reading through example code, because there seems to be no docs on this? (Someone feel free to point me to them if I've missed them!)

My next step was to make this object spin in the direction that corresponded to the xaxis tilt on the iPhone. Earlier, in script, I did this by utilising a script global, $Gravity, as per the examples with iTGB.

I'm not quite sure the way to approach this (in a performance orientated manner - this is why I'm bothering in the first place) for components. Do I do something like:

function SpinBehavior::onBehaviorAdd( %this )
{	
	// Create the Component
	%this.Component = new SpinComponent();
   
	// Copy all of the behavior information to the component
	copyBehaviorToComponent(%this, %this.Component);
	
	new ActionMap(moveMap);
		moveMap.bindObj(joystick0, xaxis, "joyFunc", %this.Component);
	moveMap.push();

	// Add the component
	if (!%this.Owner.addComponents(%this.Component))
	{
		error ("SpinBehavior::onBehaviorAdd - Failed to register Spin Component");
		%this.Component.safeDelete();
		return;
	}  
}

ie, I bind the joystick axis to a method in the component? (Accessing it via a ConsoleMethod?) This doesn't feel right to me, plus I'll be having to call a ConsoleMethod which is crossing the script/code divide which I want to avoid for speed.

Any suggestions? Or am I missing the idea of how to use components in Torque completely? I feel I probably am, but without some decent docs on the issue I'm simply guessing currently.

#1
05/11/2009 (4:39 pm)
I haven't seen any documentation. The best resources I've found are reading the BehaviorShooter_components sample behaviors and playing with those. Takes a while to get the hang of it but it works great. Bridging the divide for functions like onBehaviorAdd etc are fine. You just don't want to be doing something that's called a lot like onUpdate etc. I also prefer not to do any script for items that are going to be created after the game is started because it can cause noticeable jitter in the framerate when you all of a sudden throw in some script.

You can't get rid of all script, the components are glued into the editor/level loading using script. But I do everything in c++ pretty much and love it. Even if TScript was as fast as c++ I'd still use c++, you can't beat the compile time checks and awesome xcode debugger/profiler.
#2
05/12/2009 (10:52 am)
I tried as I mentioned above and achieved what I was after. The Xcode debugger et al is certainly taking me a while to acclimate to after being a Windows guy for so long :D
#3
05/12/2009 (10:58 am)
Absolutely! I love the mac and have replaced all my windows machines in the house with Mac only (who would of known iphone development would of exposed me to MAC after resisting for 17 years..) but there are some things that still really irritate me about the Mac interface. Xcode certainly is taking a bit longer than usual to get use to.
#4
05/12/2009 (11:02 am)
Good to know someone else is experiencing the same and it's not just me being a bit dense :D