Game Development Community

rearrange simset objects

by Isaac Barbosa · in Torque Game Builder · 02/12/2009 (11:13 am) · 9 replies

Hello,
My first comment is that I have performed several search in the forum: "Simset", "rearange objects", "rearange simset objects"... and nothing. So I must ask for help unless I want to start reading thousand of threads to see if there is something for me since the forums are now for every category....

Well, I´m creating 10 elements on stage in a column, and then I´ve added to a simset. But since they´re created in different times, the simset is not ordered as a column. So my question is: is the a way to rearange all the simset objects so the first object in the top of my column becomes 0 (in the simset) and then 1,2,3,4.. and so on so last item in my column is 9(in the simset, but 10 in the column).

I´ve found

Thanks

#1
02/12/2009 (11:18 am)
simSet.pushToBack() and simSet.bringToFront() may be what you're after.
eg
==>new simset(set);
==>set.add(new simObject(a));
==>set.add(new simObject(b));
==>set.add(new simObject(c));
==>set.dumpTree();
13931-SimSet-set
 13932-SimObject-a
 13933-SimObject-b
 13946-SimObject-c
==>set.pushToBack(b);
==>set.dumpTree();
13931-SimSet-set
 13932-SimObject-a
 13946-SimObject-c
 13933-SimObject-b
==>set.bringToFront(b);
==>set.dumpTree();
13931-SimSet-set
 13933-SimObject-b
 13932-SimObject-a
 13946-SimObject-c
#2
02/12/2009 (11:32 am)
@Orion,

Thanks, but I think that´s not useful here :(

Since my objects are created from different simsets and then added to a global set for the whole column:

$Line1.add(%objecto);

So my Column will be arranged automatically:

J
A
F
G
E
B
C
D
H
I

And I want to set a function to rearange it so the column becomes:

A
B
C
D
E
F
G
H
I
J

Thanks
#3
02/12/2009 (11:39 am)
i'm not sure i entirely follow,
but it sounds like you'll need to sort the objects programatically
and use something like bringToFront() to reorder them in the set once they're sorted.

or, consider avoiding requiring them to be in a particular order in the set.

#4
02/12/2009 (12:37 pm)
Yeah, something like that, but don´t know how to... any ideas are welcomed
#5
02/12/2009 (12:46 pm)
I'm a little unclear about the problem, but it seems that you could add a dynamic field 'index' to each of the objects that is incremented as each object is created.

Then loop through the Simset and sort according to this value. Am I way off base with what you are looking for?
#6
02/12/2009 (12:49 pm)
Give this a whirl Isaac. It isn't very efficient, but as long as you use it sparingly, it should be fine.

function SimSet::sortList(%this)
{
   %objectCount = %this.getCount();
   for (%j = 0; %j < %objectCount; %j++)
   {
      for (%i = %objectCount - 1; %i > %j; %i--)
      {
         %objectA = %this.getObject(%i - 0);
         %objectB = %this.getObject(%i - 1);
         
         if (SimObjectSort::compare(%objectA, %objectB) < 0)
         {
            %this.reorderChild(%objectA, %objectB);
         }
      }
   }
}

function SimObjectSort::compare(%objectA, %objectB)
{
    return strcmp(strlwr(%objA.getName()), strlwr(%objA.getName()));
}
#7
02/12/2009 (1:57 pm)
I would create a link list using the the Objects I have. After creating all the objects you needed, sort them out using Phillip's "SimObjectSort::compare" seems to do the trick. Then use those objects and create a link list from it.

%objA.prevObject = 0;
%objA.nextObject = %objB;

%objB.prevObject = %objA;
%objB.nextObject = %objC;

%objC.prevObject = %objB;
%objC.nextObject = %objD;
.
.
.
%objZ.prevObject = %objY;
%objZ.nextObject = 0;

Now you'll know what's the first and last object of this list. When you want to insert a new object to the list, you just change the .prevObject and .nextObject variable of the surround objects at the level you want to insert the new object.
#8
02/12/2009 (2:01 pm)
if you're concerned about speed (eg you have a lot of these items)
and you're able to recompile the source,
you might consider exposing dQsort() to script.

i've implemented that as a tge resource: SortWords() and SortNumbers(), and the same code should work for TGB.
#9
02/12/2009 (2:58 pm)
Thanks guys. I have my own system to sort my objects. But now I´ve discovered that actually this is not my problem. So I must work out something that seems is a bug.