Game Development Community

I don't understand setTileCustomData()

by Brian Wilmeth · in Torque Game Builder · 02/15/2006 (7:16 am) · 4 replies

I want to set the custom data for a tile, so I tried entering this in the customData field in the tile editor

"%this.canBlowUp = 1;"

is this correct logic? It's not working the way I think it should work, because when I call onCollision and have

if( dstObj.canBlowUp == 1 )
echo("OBJECT IS BLOWING UP");

the object I collided with doesn't echo the statement. What am I doing wrong here?

#1
02/16/2006 (12:11 am)
Brian,

It doesn't work like that, let me try to explain.

If you want to retrieve the custom-data, use "getTileCustomData()". The "data" here can be anything, it isn't executed, it's just "data". You can put in strings, numbers, whatever but the data only needs to make sense to you, not to T2D.

Assuming you only want to store whether the object can "blow up" or not then in the custom-data field in the editor (which uses the "setTileCustomData()" call) simply enter "1" or "0" , "true" or "false", "yes" or "no", even "blowup" or "notblowup". Again, it isn't a script-statement, it's "custom" data so use whatever you want, in this case I'll use "1" or "0".

So, in your editor you enter "1" or "0" in your tiles. Then, either from a collision or just interrogating a tile for the fun of it and assuming your tile object is called "%tile" then you would retrieve it using...
if ( %tile.getTileCustomData("10 5") )
{
  echo("Blowing up!");
}
else
{
  echo("NOT blowing up.");
}

Notice that I had to specify while tile I wanted, in this case at position (10,5). So as you can see, the parameters tell the "getTileCustomData()" call which tile to retrieve the data from. This is because the "%tile" object is the whole layer, not the tile itself.

In your example above, you use it in the "onCollision" so you need to retrieve the tile-position as well but how do you know which tile it is as the "onCollision" on returns the tile-layer object, not the tile itself?

The good news is that for objects that have child objects (such as tiles in a tile-layer), T2D returns a reference for both the source/destination objects. This "reference" depends upon the object in question but for tile-layers, it's the logical tile position (not the world-position). The default names in the "onCollision" callback for these references are "%srcRef" and "%dstRef" which refer to the "%srcObj" and "%dstObj" respectively.

Therefore, to retrieve the custom-data, simply use the following...
if ( %dstObj.getTileCustomData(%dstRef) )
{
  echo("Blowing up!");
}
else
{
  echo("NOT blowing up.");
}

Note that I used the "%" prefix on the "dstObj" which I assumed was just an ommision in your post.

Does this help?

- Melv.
#2
02/16/2006 (1:09 am)
Yes it does.

And if I suppose I wanted two paramaters such as canBlowUp and canMove, I can make the custom data a string like a custom data value of

"1 0" // this object can blow up but can't move

and then do something like

if( getWord( 0, %dstObj.getTileCustomData(%dstRef) ) )
echo("this object can blow up");
if( getWord( 1, %dstObj.getTileCustomData(%dstRef) ) )
echo("this object can move");


if this makes sense to u then I get it.

THANKS :)
#3
02/16/2006 (2:50 am)
Yup, that's how I've done custom data by using string objects. You just need to remember what order you put the data in so you can extract it in the correct order :)

And if you suddenly need to add 'can sparkle' (or whatever), then you'll probably need to go through all of your tiles to update the custom data.
#4
02/16/2006 (8:15 am)
Wouldn't something like the following work too?



for (%i = 0; %i < getWord(%dstObj.getTileCount(),0); %i++)
{
     for (%j = 0; %j < getWord(%dstObj.getTileCount(),1);%j++)
    {
          $myTiles[%i,%j] = new ScriptObject(); //New object to keep track of individual tiles
          %obj = $myTiles[%i, %j];
          eval(%dstObj.getTileCustomData(%i SPC %j));

     }
}

If the custom data for tile at (1, 7) was something like "%obj.canBlowUp = 1; %obj.canMove = 0; %obj.sparkly = 3;" then shouldn't that make $myTiles[1,7].canBlowUp = 1, .canMove = 0 and .sparkly = 3?

That way you wouldn't have to worry about parsing or adding new fields in the future.

Completely untested so I reserve the right to be 107% incorrect.