Dynamic arrays?
by AzraelK · in Torque Game Builder · 08/23/2006 (10:28 am) · 9 replies
Im doing a ship game and I need to hold control of each ship created in the game, however I dont want to set a specific number of ships in an array to do this. Is there a way to make dynamic arrays or lists for this? or maybe a way to check all created objects of a certain class?
Thanks!
Thanks!
#2
You might want to look at simsets. They're like lists, and in the update (v1.1.1), I believe you can now reorder lists. Once you have a simset, you can scroll through using a FOR loop.
08/23/2006 (12:22 pm)
It's wise not to bump after almost two hours. You might want to look at simsets. They're like lists, and in the update (v1.1.1), I believe you can now reorder lists. Once you have a simset, you can scroll through using a FOR loop.
#3
-Peter
08/24/2006 (1:16 am)
Well the thing is, since you're using Torque Script, arrays are much different than in C++. To be honest you really can't make nondynamic arrays since you never really define the array to begin with. You just use a variable with a number at the end and its part of an array. Any number. You don't have to start at 0. You can skip a few hundread. It really doesn't matter. My suggestion is that you learn as much as you can about how Torque handles arrays from the docs. Then I think you'll see what I'm talking about.-Peter
#4
As far as implementing a SimSet / SimGroup solution, you could do something like this:
$spaceshipGroup = new SimGroup();
.
.
.
$spaceshipGroup.add(%newlyCreatedShip);
When you need to reference them, as Apurva noted, you could use something like:
for(%i = 0; %i < $spaceshipGroup.getCount(); %i++)
{
%currentShip = $spaceshipGroup.getObject(%i);
}
One feature of using SimGroup, if you are just using it as a container/list , is that if you delete it, the objects within it will be implicitly deleted (I'm mostly sure this is true :). If you want to avoid this behavior, use SimSet instead, as an object can exist within several SimSet's concurrently.
08/25/2006 (4:45 pm)
Peter put it well. As far as implementing a SimSet / SimGroup solution, you could do something like this:
$spaceshipGroup = new SimGroup();
.
.
.
$spaceshipGroup.add(%newlyCreatedShip);
When you need to reference them, as Apurva noted, you could use something like:
for(%i = 0; %i < $spaceshipGroup.getCount(); %i++)
{
%currentShip = $spaceshipGroup.getObject(%i);
}
One feature of using SimGroup, if you are just using it as a container/list , is that if you delete it, the objects within it will be implicitly deleted (I'm mostly sure this is true :). If you want to avoid this behavior, use SimSet instead, as an object can exist within several SimSet's concurrently.
#5
for(...)
{
%u = new t2dStaticSprite(blackPeiceBlock);
%level.addToScene(%u);
}
echo(%level.getSceneObjectCount());
When I echo I get an accurate count, every sprite I add to the scene is counted. If there is another forum where this is addressed, that would be really helpful.
09/07/2006 (7:25 am)
I am having the same problem. My issue is specifically that I want to create objects that will be added to the scene. I can create the object and add it to the scene but they are not showing up.for(...)
{
%u = new t2dStaticSprite(blackPeiceBlock);
%level.addToScene(%u);
}
echo(%level.getSceneObjectCount());
When I echo I get an accurate count, every sprite I add to the scene is counted. If there is another forum where this is addressed, that would be really helpful.
#6
09/07/2006 (7:35 am)
Are you setting the size and position of %u?
#7
echo(%u.getPosition()); //is "0, 0"
echo(%t.getLayer()); //is "0"
echo(%t.getVisible()); //is true
echo(%t.getHeight() @ "x" @ %t.getWidth()); //is "10x10"
I get valid values. I'll double check it though, thanks @Philip.
09/07/2006 (7:40 am)
No, it's loading that from the datablock.echo(%u.getPosition()); //is "0, 0"
echo(%t.getLayer()); //is "0"
echo(%t.getVisible()); //is true
echo(%t.getHeight() @ "x" @ %t.getWidth()); //is "10x10"
I get valid values. I'll double check it though, thanks @Philip.
#8
%t = new t2dStaticSprite()
{
scenegraph = SceneWindow2d.getSceneGraph();
class = blackPeiceClass;
};
%t.setImageMap(blackPeiceImage);
%t.setPosition("0 0");
%t.setSize("40 50");
The datablock wasn't initializing the image itself. Apparently I had to set the image explicitly for it to draw correctly.
*shaking my head*
09/07/2006 (8:07 am)
I found the issue. What I should have been doing is:%t = new t2dStaticSprite()
{
scenegraph = SceneWindow2d.getSceneGraph();
class = blackPeiceClass;
};
%t.setImageMap(blackPeiceImage);
%t.setPosition("0 0");
%t.setSize("40 50");
The datablock wasn't initializing the image itself. Apparently I had to set the image explicitly for it to draw correctly.
*shaking my head*
#9
You're not using any datablock in that code. To use a datablock you'd have to add a "config" attribute a la:
09/07/2006 (9:35 am)
@GCYou're not using any datablock in that code. To use a datablock you'd have to add a "config" attribute a la:
datablock t2dSceneObjectDatablock(MyDatablockName)
{
ImageMap = "blackPeiceImage";
Position = "0 0";
Size = "40 50";
};
%t = new t2dStaticSprite()
{
scenegraph = SceneWindow2d.getSceneGraph();
class = blackPeiceClass;
config = "MyDatablockName";
};
Torque Owner AzraelK