Game Development Community

Community Tip Thread

by practicing01 · in Torque 2D Beginner · 05/04/2013 (9:33 pm) · 25 replies

Post information you feel is useful to others about the engine, algorithms, art and game-dev in general.

Tip:

As you populate a child GuiListBoxCtrl of a GuiScrollCtrl, they will both resize automatically. A child GuiDynamicCtrlArrayControl of a GuiScrollCtrl will not do this, however. A method for dealing with this is keeping a counter variable that will represent the Y extent of the GuiDynamicCtrlArrayControl. Increase the counter with each item that you add to the list. When all of the items are added, change the Y extent of the GuiDynamicCtrlArrayControl and call computeSizes() on the parent GuiScrollCtrl. This will scale the scroll bars properly.
Page «Previous 1 2
#1
05/17/2013 (11:17 pm)
Line 148 of the point force controller toy has this:
SandboxScene.Controllers.add( %controller );

Controllers is not a custom field. It is an undocumented simset that you are required to add your controllers to if you want them to work.
#2
05/18/2013 (2:29 am)
If you are making a function whose entire function is to create a Sprite (or other, similar object), it can be VERY useful to throw this at the end:

function thisIsAFunctionName()
{
...code to make a sprite or something...

return %objectname;
}

This way you can reference and alter the object within other functions as so:

%object1 = thisIsAFunctionName;
%object2 = thisIsAFunctionName;

%object1.Size = %object2.Size;

Just a rough example. It took me a day to figure out why the below down wasn't working before I discovered how to return a value.
#3
05/19/2013 (2:56 am)
Make a chart of some sort to keep track of your SceneLayer, SceneGroup, CollisionLayers, and CollisionGroups. Otherwise, you WILL get confused.
#4
05/20/2013 (5:55 pm)
This might sound obvious but if your program is going slow, the only way to speed it up is to have the computer do LESS actions. You can't actually speed your computer up but you can remove redundant or unnecessary code.
1. When you are developing a chunk of code it is sometimes useful to have it spread out as long as possible so you can easily read it and debug. But when you are done, try to smoosh appropriate lines together if possible. DO NOT over do it or you risk making it nigh-impossible to debug the code later if need be.

2. For optimization (and ease of reading), try replacing often used functions and variables with a easy to read variable.
For example,
if(%this.position.Y - %this.getHeight()/2 > %this.wallLeftID.position.y - %this.wallLeftID.getHeight()/2
&& %this.position.X - %this.getWidth()/2 < %this.wallLeftID.position.X + %this.wallLeftID.getWidth()/2)
   %this.setGravityScale(0);
Can be organized cleanly like this:
%halfHeight = %this.getHeight()/2;
%halfWidth  = %this.getWidth()/2;
%LeftWallX  = %this.wallLeftID.position.x;
%LeftWallY  = %this.wallLeftID.position.y;
%thisX      = %this.position.X;
%thisY      = %this.position.Y;
%LeftWallWidth  = %this.wallLeftID.getWidth();
%LeftWallHeight = %this.wallLeftID.getHeight();
	 					
if(%thisY - %halfHeight > %LeftWallY - %LeftWallHeight /2
&& %thisX - %halfWidth  < %LeftWallX + %LeftWallWidth  /2)
   %this.setGravityScale(0);

Any further code needing those same variables will be substantially easing to write and read.
#5
06/13/2013 (3:59 am)
MoveTo has four parameters: Location, Speed, AutoStop, WarpToTarget. Setting Autostop to True and WarpToTarget to False can get rid of unwanted skipping. Example:

MyModule.ThePlayerObject.MoveTo( %worldPosition, 50 , true, false);
#6
06/30/2013 (12:18 am)
If you can't clone() a guicontrol object for whatever reason, there is a work-around.

Create your guicontrol object as you normally would with Name fields etc. Load it as you normally would with TamlRead().

Step 1: Rename the loaded guicontrol. Assuming you set the Name field to "guicontrol_original".
//have to use setName cus the objectName member is protected
//and the onNameChange callback is empty
guicontrol_original.setName("guicontrol_clone");
%cloned_gui=guicontrol_clone;//save to variable, notice it's quoteless

Step 2: Since we're going to load the original again, button Name conflicts will occur unless we rename them.
for (%x=0;%x<%cloned_gui.getCount();%x++)
{
%butt=%cloned_gui.getObject(%x);//go through child guicontrols
//hardcoded names and you have to use getName()
if (%butt.getName()$="movebutt")
{
//change the command and any other things if you need to
%butt.command="banana(0);";
}
//set the name to an empty string so there aren't any conflicts
%butt.setName("");
}

Step 3: Repeat

Edit: If you don't want to load the taml from file every time, you can load it once and call save() on the guiobject. That'll give you the code to create the guiobject from script, which you can turn into a function.
#7
07/28/2013 (12:40 am)
You can set an assets AssetCategory field to something meaningful for searching and do so like this:

//list to hold the results
%assquery=new AssetQuery();

//populate list, search for AssetCategory="floortile"
AssetDatabase.findAssetCategory(%assquery,"floortile",false);

//loop through list if found any
for (%x=0;%x<%assquery.getCount();%x++)
{
%assid=%assquery.getAsset(%x);

%ass=AssetDatabase.acquireAsset(%assid);

//get the module the asset belongs to
%moduleid=AssetDatabase.getAssetModule(%assid);

echo(%moduleid.ModuleId);
echo(%ass.AssetCategory);

//check if the module is the same as the one we're in
if (%moduleid.ModuleId$=%this.getName())
{
echo("found the asset in the category specified within this module's assets");
}

AssetDatabase.releaseAsset(%ass.getAssetId());
}

Searching through modules is easier:
//Type="level" within the module definition
%levelmodules=ModuleDatabase.findModuleTypes("level",false);
#8
09/11/2013 (1:18 am)
Need a gui editor? If you bought TGB you can use that one. You just have to port the values to the taml format but that's trivial. You can also use T3D's gui editor (free).
#9
09/11/2013 (6:34 am)
Good call - I've done it myself. There is some tweaking to do but overall it's only an hour or so of futzing to get it up and running.
#10
09/23/2013 (8:02 am)
When removing objects from a simset within a for-loop, reset the loop counter since you modified the simset count:

for (%x=0;%x<%Simset_Banana.getCount();%x++)
{

%Script_Object_Banana=%Simset_Banana.getObject(%x);

%Simset_Banana.remove(%Script_Object_Banana);

%x=-1;//Important bit.

}

Edit: Reset the counter to -1 not 0 because it gets incremented at the end of the loop.
#11
09/23/2013 (9:58 am)
SimSets already have some useful functions for clearing and removing objects.

Use "%Simset_Banana.clear();" to do the same thing as the code above.

Use "%SimSet_Banana.deleteObjects();" to do the same thing as above and also deletes the objects inside the list.
#12
09/27/2013 (2:22 am)
3D game engine should include a variety of 3D graphics algorithms integrate the SDK provides a convenient interface to facilitate others to develop games based on this module.
#13
09/27/2013 (2:23 am)
3D game engine should include a variety of 3D graphics algorithms integrate the SDK provides a convenient interface to facilitate others to develop games based on this module.
#14
12/13/2013 (8:04 am)
If you want to pop an attachGui()'d GuiControl, you first have to push it onto the canvas, then pop it out.
#15
06/03/2014 (8:02 am)
If you want to set/remove specific collision groups for an object:
%SceneObject_Object.setCollisionGroups(0 SPC 1);

%String_Mask=SceneObject_Object.getCollisionGroups() SPC "2";//Add group 2.

SceneObject_Object.setCollisionGroups(%String_Mask); 

%String_Mask=SceneObject_Object.getCollisionGroups();

%String_Mask=strreplace(%String_Mask,"2","");//Remove group 2.

SceneObject_Object.setCollisionGroups(%String_Mask);
#16
06/03/2014 (2:38 pm)
You can access children of GUI controls via their internal names using the -> operator:

%parentControl = new GuiControl(MainParent);

%childControl = new GuiControl()
{
    InternalName = "ChildControl";
    TestData = "Test";
};

%parentControl.add(%childControl);

%data = %parentControl->ChildControl.TestData;
#17
06/03/2014 (2:41 pm)
Unique to T2D MIT is an operator for accessing sub-elements of a string:

%words = "One Two Three";
%one = %words._0;
%two = %words._1;
%three = %words._2;

This is very useful for a lot of fields in native objects and can be a useful shortcut in your script code.
#18
06/07/2014 (7:02 am)
If you ever want to make a perfect hexagon polygon object, this code will easily allow you to make the collision shape:

%sidelength = %object.GetSize().Y*0.5;
%fancymathcalculation = msqrt((%sidelength*%sidelength) - ((%sidelength * 0.5) *(%sidelength * 0.5)));

//Coordinates
//Center (aka this node) is 0,0 - or in this case ThisX,ThisY

//Top Left point is (ThisX),(ThisY + 1)
%topleftpoint = (-%fancymathcalculation) SPC (%sidelength/2);

//Bottom Left Point is (ThisX - 1),(ThisY)
%bottomleftpoint = (-%fancymathcalculation) SPC (-%sidelength/2);

//Bottom point is (ThisX - 1),(ThisY - 1)
%bottompoint = (0) SPC -%sidelength;

//Bottom Right Point is (ThisX),(ThisY - 1)
%bottomrightpoint = (%fancymathcalculation) SPC (-%sidelength/2);

//Top Right point is (ThisX + 1),(ThisY)
%toprightpoint = (%fancymathcalculation) SPC (%sidelength/2);

//Top Point is (ThisX + 1),(ThisY + 1)
%toppoint = (0) SPC %sidelength;


%points = %topleftpoint SPC %bottomleftpoint SPC %bottompoint SPC %bottomrightpoint SPC %toprightpoint SPC %toppoint;	

	%object.cshapeindex = %object.createPolygonCollisionShape(%points);
#19
06/07/2014 (3:09 pm)
If you have a GUI window on a scenewindow, and want to be able to direct the keyboard input to that scenewindow right after validating a text entry on the GUI window, this will do,
// Here "TextEntry" is a specific class of GuiTextEditCtrl
function TextEntry::onValidate( %this )
{
	// Do your stuff
	...
	// And then give away the focus
	%this.makeFirstResponder(false);
}
#20
06/15/2014 (3:22 pm)
If you want to create a directory, put a slash as the final character of the path argument when using createPath(). This is only told in T3D reference.
Page «Previous 1 2