Check for existing object in function?
by Chase Webb · in Torque 2D Beginner · 06/01/2013 (11:23 pm) · 9 replies
Hey all! I'm sure this functionality must exist, because I seem to remember it in one of the base tutorials from TGB, but it's been so long I have no idea what it is called. Basically, I want to have a function check to see if an object exists within a specific location, and if there is an object in that location, not to do anything.
For detail: I'm starting to make some automatically generated terrain for my game prototype, which uses a grid system to create the next block of terrain (perfect squares) as you travel into its sensor area. I've got the function to make the background load and create the next sensor area working with only a few quirks. However, if I travel back and forth I seem to be recreating the same block of terrain over and over on top of itself. I want to check to see if something exists before deciding whether or not to generate that block of terrain.
For detail: I'm starting to make some automatically generated terrain for my game prototype, which uses a grid system to create the next block of terrain (perfect squares) as you travel into its sensor area. I've got the function to make the background load and create the next sensor area working with only a few quirks. However, if I travel back and forth I seem to be recreating the same block of terrain over and over on top of itself. I want to check to see if something exists before deciding whether or not to generate that block of terrain.
#2
06/02/2013 (12:30 am)
Thanks! So how would I parse apart the IDs in %picked? I can get them to display in the log via the echo() command, but beyond that I'm not sure what to do.
#3
06/02/2013 (1:03 am)
%picked will contain a list of the objects. The info I found doesn't specify if that list is an array or SimSet. If it's an array, then you access the objects with brackets (e.g. %picked[0], %picked[1], etc). If it's a SimSet, then use the getObject function (e.g. %picked.getObject(0), %picked.getObject(1), etc). Try the getObject function first, as that will generate an error if it's wrong.
#4
@Chase : Once you have a list of object IDs in %picked, first, get the object count.
Then, iterate through every object in that list
Once you have %object_ID, you can treat it as if it directly represents your object.
You can use %object_ID.Image, %object_ID.Position, %object_ID.Size, any valid property of the chosen object.
For example, if all your terrain tiles are placed in SceneGroup 4...
06/02/2013 (1:03 am)
@Joe : %picked is simply a space separated string of object IDs.@Chase : Once you have a list of object IDs in %picked, first, get the object count.
%count = %picked.count;
Then, iterate through every object in that list
for( %i=0; %i<%count; %i++ )
{
%object_ID = getWord(%picked, %i);
...
}Once you have %object_ID, you can treat it as if it directly represents your object.
You can use %object_ID.Image, %object_ID.Position, %object_ID.Size, any valid property of the chosen object.
For example, if all your terrain tiles are placed in SceneGroup 4...
if(%object_ID.SceneGroup == 4)
{
//do something, or don't do something!
}
#5
06/02/2013 (1:21 am)
Thanks guys! That solved everything! :)
#6
I don't like eval(), for the record. Sometimes it's handy though.
06/02/2013 (7:13 am)
That sounds expensive. I'd just create a global list of generated block names - add the name as you go and check to see if the name is there before you add another.// dynamically create the name of the variable
%blockName = "X" @ %xCoord @ "Y" @ %yCoord;
eval("$Map::" @ %blockName @ " = 1;");
// then check for its existence and bail from the generation function
%blockName = "X" @ %xCoord @ "Y" @ %yCoord;
eval("if ($Map::" @ %blockName @ " ) == 1) return;");I don't like eval(), for the record. Sometimes it's handy though.
#7
$Map::block[%blockName] = 1;
or
$Map[%blockName] = 1;
OR, a 2D array
$Map[%xCoord, %yCoord] = 1;
And I agree with you, I hate eval. I try to limit my use as much as possible.
06/02/2013 (11:04 am)
@Richard instead of using eval, why not just use a torque array$Map::block[%blockName] = 1;
or
$Map[%blockName] = 1;
OR, a 2D array
$Map[%xCoord, %yCoord] = 1;
And I agree with you, I hate eval. I try to limit my use as much as possible.
#8
06/02/2013 (12:32 pm)
I thought about that before and never really researched it (option 1) but I was a little concerned about Torque's handling of sparse arrays. Never did any research on it and then forgot about it completely, but it should be good.
#9
06/03/2013 (7:04 am)
@Simon: Of course, I forgot that possibility. :)
Associate Simon Love
%picked will contain all the object IDs of the objects that overlap the specified world position.
Once you have this list, you can simply iterate through the list and verify each object's name, ID or class and decide what to do in the appropriate situation.
If you are trying to see if there is a Tile in a specific location inside of a Tilemap or CompositeSprite Object, then use
which will return the spriteID (Not the object ID)