Game Development Community

iTGB Cheat Sheet (updated 2-25)

by Mat Valadez · in iTorque 2D · 01/20/2009 (1:03 pm) · 36 replies

Here's a list of handy things to remember when developing with iTGB
Update(02-11): Added links to code additions at the bottom
Update(02-14): Added important safety tip for multiTouch
Update(02-25): Added important safety tip for PUAP_SCRIPT_CHANGE


Preprocessor commands

PUAP_OPTIMIZE enables the following iPhone optimizations;
t2dSceneObjects now have a setUsesPhysics(bool) function that allows them to use physics or not. All object are false by default, which helps to reduce wasting time processing physics on non-moving objects

also affected:
t2dParticleEffects will stop playing when their world limit is off-screen
t2dSceneObject mCollisionMaxIterations is set to 1 by default


PUAP_SCRIPT_CHANGE activates a few changes to the scripting engine. The big benefit of this is that script execution is much quicker, in particular, variable lookup is faster than without it. When this is defined, iTGB will change the way it reads/compiles scripts, this means that DSOs will be incompatible with previous versions, and old DSOs will be incompatible with iTGB. Because of this, it's best to use scripts that have been compiled with the same binary to ensure compatibility.
Important SafeTy Tip With this flag set, you must initialize your variables or you may get some bogus values. i.e. use this:
function countTo( %input ) {
   %count = "";//or %count == 0;
   while( %count < %input  {
      echo( "current count:" SPC %count );
      %count++;
   }
}
instead of this:
function countTo( %input ) {
   while( %count < %input  {
      echo( "current count:" SPC %count );
      %count++;
   }
}


USE_COMPONENTS adds some functionality to the TGB components system that allow them to receive onUpdate() and onAddToScene() callbacks, like the script behaviors do.

With this undefined, components still work, but will not receive those callbacks, this define is there to improve script->code conversion.





iPhone-specific features

MultiTouch
The first touch is always mapped to the mouse/cursor. If your game does not use multi-touch(example: a port of a PC mouse-driven point 'n click), you wan't have to implement any touch callbacks.
When multiTouch is enabled(using a global variable), touches are handled by script callbacks.

Important Safety Tip: Don't forget to set
$pref::iPhone::enableMultipleTouch = true;
in defaultPrefs.cs


All touches (including the first one) are passed to three functions
oniPhoneTouchUp( %touchCount, %touchX, %touchY )
oniPhoneTouchDown( %touchCount, %touchX, %touchY )
oniPhoneTouchMove( %touchCount, %touchX, %touchY )
The parameters for each function work the same.
%touchCount is the number of touches for this event, %touchX is a list of the touchs X value, %touchy is a list of the touches Y value. Example:
getWord( %touchX, %touchCount );
will give you the X value for the last touch.


Accelerometer
The accelerometer is mapped to joystick0 three axises (xaxis, yaxis and zaxis) and can be mapped as you would a joystick in TGB. Example:
moveMap.bind(joystick0, xaxis, "joystickMoveX" );
      moveMap.bind(joystick0, yaxis, "joystickMoveY" );
      moveMap.bind(joystick0, Zaxis, "joystickMoveZ" );


UPDATE 1-23-09
By default, iTGB will disable all text output to speed things up, to turn that off for debugging, call this function:
enableDebugOutput( true );
This will output all the console text to your XCode GBD output pane. In other words, everything that yo would normally see in the Torsion output and the console.log will be visible through XCode


Images
iTGB supports PVR images. To create a PVR texture from a PNG or another filetype use the

PVR tool. PVRs must be square and power of 2 (e.g. 512x512 pixels). iTGB will look for PVR images first, so, when specifying an image (such as the "bitmap" parameter in a GUI), leave out the extension of the file and iTGB will find the image for you, looking for PVRs first.



$Global Variables

iTGB will force all palleted BMPs to 16 bit if you specify

$pref::iPhone::ForcePalletedBMPsTo16Bit = true

this can save on video memory if you need it.

iTGB will shutdown the network, allowing the game to run faster by not running unused Net code if
$pref::iPhone::UsesNetwork = false in defaultpref.cs


The following need to be set in DefaultPrefs.cs for their changes to load when iTGB does:
$pref::iPhone::StatusBarType ("BlackTransparent"/"BlackOpaque")
$pref::iPhone::StatusBarHidden (true/false)
$pref::iPhone::ScreenOrientation ("Portrait"/"Landscape")
$pref::iPhone::ScreenAutoRotate (true/false)
$pref::iPhone::EnableMultipleTouch (true/false)


General Things To Know
iTGB on Simulator or device will only read DSOs, you don't even need to have the .cs files present on the device. Make sure your scripts are compiled before trying to run.

In order to transfer you game content to you app bundle, you need to include the following in your Xcode project's "Resources" folder;

common folder
game folder
Any other mod folders you are using
main.cs
default.png


Common and game should contain your script DSOs, main.cs is the only file that need not be compiled(main.cs.dso is not loaded, only the cs file)

default.png is the image the iPhone will display while your app(iTGB) is starting up

When you add these items to the XCode project, make sure to select the second option for adding folders(recursively add folders) The icons will be blue in the XCode sideBar if you added them correctly.

XCode will not automatically transfer your changed files if you modfiy them. The best way to make sure that it does is to delete the the .app in your XCode project's build directory, then build. This will not re-compile your project, but it will force XCode to copy all of your resources over.

UPDATE 02-11-09
Code additions from forum members

For those having a problem with iTGB crashing when the home button is pressed: Quit Fix

How to get a Unique identifier for a device in script(Credit to Dave Young) Device ID ConsoleFunction

How to get device real-world location(Credit to Dave Young) Device Location

An implementation of the iPhone keyboard: Keyboard


Updates will come when I think of them. :p
Page«First 1 2 Next»
#21
05/05/2009 (5:16 pm)
Quote:
Unless you have actually written the compiler that is converting your code into low level assembly, you can't say which loop will run faster. down to!

Well you can also benchmark your applications and see the speedup, as I've seen in JavaScript, TorqueScript, and C++. Yes, compiler / browser / parser would affect things. But what I'm saying is I've personally measured gained performance in my chess AI, which evaluates thousands upon thousands of game board states per turn, as well as in neural net code, genetic algorithm code, NIC driver code, and more. It has to do more with the extra conditional check than it does the increment, imo.

Quote:
More importantly the contents of the loop, than anything else.

Yes, this is obvious. But I'm saying that once you've gotten the loop contents down to a minimum, you've unrolled it as best you can, etc, that changing the loop type can have an impact if it is a frequently called loop. I got an extra 4 FPS out of my kart kit simply by changing from a incrementing for-loop to a decrementing do-while loop in its onUpdate section.

Here's one (of many) accounts of speedup in JavaScript.

I recommend you try it out. :)
#22
05/05/2009 (7:21 pm)
Wow, that JS speed test is *old*! I doubt the script engine in IE is even the same anymore, just like the other browsers. Do you have any timer tests we can quickly run ourselves? If not, please make a TGB project we can try in VC++, GCC 4.0, 4.2 and LLVM :)
#23
05/05/2009 (7:32 pm)
@Ronny:

It's really not worth it to me to take time to make example code to prove a point. Nobody has to trust me, I'm just sharing my experience agenda-free. Take it or leave it. :)

The speedup still works, for JS, however. A friend of mine did some profile tests on script I'd written. Sure enough, big speedups still in the latest Firefox when using do-while over for. And, of course, my iTGB experience is super recent. In JS, you notice the speedup even for a single loop call. In C++, it takes a huge amount of calls to see it (like the chess AI).

I *think* the original source of the tip came from one of my graduate classes in computer architecture. But I honestly can't remember. It's served me well though! :)
#24
05/06/2009 (5:49 am)
while loops are generally faster in script based languages... here are some benchmarks from php: phpbench.com/
#25
05/22/2009 (9:16 am)
Hi Mat,

Thank you for posting this information. Will you please explain how/where to turn on the preprocessor commands?
#26
05/22/2009 (10:13 am)
In XCode, right-click on the target you want to edit, select "Get Info".
On the target info, click the "Build" tab and scroll down to "Preprocessor Macros" under GCC 4.0 Preprocessing.
#27
05/28/2009 (9:48 pm)
Here's another handy thing that hopefully won't stir controversy... vertical iTGB logo with proper resolution.


www.xenoclone.com/images/Default.png

I've seen a couple poor crops used. This works well for portrait games. Haven't seen any official GG source of the logo formatted this way.
#28
07/27/2009 (3:10 am)
Something that is kinda handy in order to get your game working right with the SCRIPT/SIMULATION(Physics) modes is to dump specific objects that don't seem to work properly out using the %obj.dump() command then make sure it is 'saved'.

This specifically applies to dynamically created objects. I found this out when I was trying to get my game to the simulator today and was trying to figure out why it wasn't working on different optimizations.
#29
08/02/2009 (12:57 am)
I'm new in tgbscript... in wich .cs file I must add the function and how I call it?

function countTo( %input ) {
%count = "";//or %count == 0;
while( %count < %input {
echo( "current count:" SPC %count );
%count++;
}
}
#30
12/02/2009 (3:22 pm)
Great resource!! Thanks a bunch!

One minor thing to note is the "getWord" function is zero-indexed based, and as such, the technically correct way to get the last touch position would be:

%xPos = getWord(%touchX, %count-1);
.

#31
12/06/2009 (12:47 am)
Hey, great resource, this has definitely been coming in handy. One change though, for the accelerometer input, I was following what you said and that wasn't working. Instead of:
moveMap.bind(joystick0, xaxis, "joystickMoveX");

It should be...
moveMap.bind(joystick, "xaxis", joystickMoveX);

I had been having problems with that and this worked for me.
#32
04/14/2010 (3:34 am)
Please help !

(sorry im a noob ^^) i set my sprites with auto rotation in torque game builder this doesn't work on iphone because setUsesPhysics is false? what exactly change in t2dSceneObjects for setUsesPhysics to be true?

Thanks
#33
04/14/2010 (4:14 am)
Hi Alexandre

Add UsesPhysics = "1" to your t2dSceneObject
#34
04/14/2010 (4:30 am)
i add UsesPhysics = "1" in t2dSceneObject.cc but i have 1 error :/

'UsesPhysics' was not declared in this scope








#35
04/14/2010 (4:36 am)
Are you coding in C++ or TorqueScript? You shouldn't be needing to modify the t2dSceneObject C++ class.
#36
04/14/2010 (4:48 am)
sorry for my mistake, i changed my torque script it work ^^ thanks !
Page«First 1 2 Next»