Author Topic: Reordering items.  (Read 1524 times)

Say I had 5 bananas:

Code: [Select]
for(%i=0; %i<5; %i++){
   %client.banana[%i] = "Banana";
   %client.BananaCount++;
}

Then I eat the 3rd one:
Code: [Select]
%client.banana[3].delete();
%client.bananaCount--;

Now I have a list like this:

1 Banana
2 Banana
3
4 Banana
5 Banana

How would I make the list like this:

1 Banana
2 Banana
3 Banana
4 Banana

(Removing the empty gap)

Sorry for the explenation, Couldn't think of any other way to explain it.
« Last Edit: December 20, 2007, 06:07:29 AM by MrPickle »

I hope Ephi gives you one of his
Quote from: Ephialtes
fruity numbers

I hope Ephi gives you one of his
Quote from: Ephialtes
fruity numbers

I Lol'd

Add a loop that moves the next-highest one in the eaten-one's space, then the one after that in the previous slot.

4th Banana -> 3rd Slot
5th Banana -> Now open 4th slot

Like this?

Code: [Select]
for(%i=0;%i<%client.bananaCount;%i++){
   if(!isObject(%client.banana[%i])){
      %client.banana[%i] = %client.banana[%i+1];
      %client.banana[%i+1] = "";
   }
}

I think that'll work
« Last Edit: December 20, 2007, 07:51:25 AM by MrPickle »


I looked at that but it confused me.

Well this is the important bit, I've changed the comments a bit:
Code: [Select]
function serverCmdRemoveMe(%client){
%index = -1; // %index will record where they are in the list, and it's -1 to start because 0 would be a valid position.
for(%i = 1; %i <= $ClientListCount; %i++) { // loop through the whole list
if( $ClientList[ %i ] == %client ) { // if current value at the position in the array is what we are looking for
// Yep, they're on the list
%index = %i; // Remember where in the list
break; // end the loop
}
}

if( %index < 0 ) { // If the -1 has not been changed, this will equate to true
// They're not on the list!!
messageClient(%client, '', '\c3You\'re not on the list...');
return;
}

// Remove them from the list
$ClientList[ %index ] = "";

// Bump back all the clients so we don't have a blank space in our $ClientList array
for(%i = %index; %i <= $ClientListCount; %i++) {
// %i = %index - start from the position we have just removed
// %i <= $ClientListCount - loop to the end of the list
$ClientList[ %i ] = $ClientList[ %i + 1 ]; // Set the current value in the array to the one (above/after) it
}

$ClientListCount--; // Deduct from the total
}
Of course you'd have to change it to deal with bananas or whatever you are doing.
« Last Edit: December 20, 2007, 11:39:57 AM by Randy »

@ MrPickle:

Weren't you just posting about how nothing challenges you anymore?  Lol, lame.

@ MrPickle:

Weren't you just posting about how nothing challenges you anymore?  Lol, lame.

I also said that I didn't know everything.

Why won't this work:
Code: [Select]
function reorderPatrolBots(%client, %num){
for(%i=%num;%i<%client.pbotCount;%i++){
if(!isObject(%client.pbot[%i])){
%client.pbot[%i] = %client.pbot[%i+1];
%client.pbot[%i].number = %i;
%client.pbot[%i+1] = "";
}
}
}

Say we deleted Patrol bot 4 it'd call:

reorderPatrolBots(%client,4);
« Last Edit: December 20, 2007, 12:27:20 PM by MrPickle »

Does %client.pbot[%whatever] start at 0 or 1?
I don't really think the if is necessary.
Code: [Select]
function reorderPatrolBots(%client, %num){
for(%i=%num;%i<%client.pbotCount;%i++){
%client.pbot[%i] = %client.pbot[%i+1];
%client.pbot[%i].number = %i;
}
}

Also, if you're dealing with bots, it may be best to add them to a SimSet which would remove the need to order them.

Their first bot is pbot[1] I think.

I tried what Randy gave but it doesn't work

For some reason if you delete Patrol Bot 1 out of 2 when Patrol bot 2 becomes Patrol Bot 1, Patrol Bot 2 has Patrol Bot 1's object.

After some quick testing:
Code: [Select]
function orderBotTest(%num, %delete){
%client = new ScriptObject();
for(%i=1;%i<=%num;%i++){
%client.pbot[%i] = new ScriptObject(){
number = %i;
};
%client.pbotCount++;
}
%client.dump();
%client.pbot[%delete].delete();
%client.pbotCount--;
reorderPatrolBots(%client, %delete);
%client.dump();
for(%i=1;%i<%num;%i++){
%client.pbot[%i].delete();
}
%client.delete();
}

function reorderPatrolBots(%client, %num){
for(%i=%num;%i<=(%client.pbotCount+1);%i++){
%client.pbot[%i] = %client.pbot[%i+1];
%client.pbot[%i].number = %i;
}
}

I extended the range of the loop and fixed it:
Code: [Select]
function reorderPatrolBots(%client, %num){
for(%i=%num;%i<=(%client.pbotCount+1);%i++){
%client.pbot[%i] = %client.pbot[%i+1];
%client.pbot[%i].number = %i;
}
}