# Dynamic Object Number Ordering Problem [SOLVED]

### » Mon Apr 20, 2015 11:17 am

Message: SpookySkeleton can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
Hello! Right now I'm prototyping a turn-based sort of game. You move units around and it creates a wall behind themselves, kind of the same way snake does.

The trouble I'm having is, when you move a unit it spawns a tail piece that I'd like to give an individual private variable number to. Something like, if you move 4 spaces, create a trail of 4 tail pieces and number them 1, 2, 3, 4. I could give them a private variable when I spawn them, but I'd like it so if you lost a piece, the rest would be able to adjust accordingly.

For example, The unit has 4 pieces: 1, 2, 3, 4. Looses the number 3 piece then the others would recalculate their variables to 1, 2, 3 instead of 1, 2, 4.

If you want more info let me know. I've been searching keywords on the forum for an hour or two so I'd really appreciate a helping hand here! Thanks so much!

Cap file:
Last edited by SpookySkeleton on Tue Apr 28, 2015 4:48 am, edited 1 time in total.
B
24
S
7
G
1
Posts: 36
Reputation: 2,049

### » Mon Apr 20, 2015 12:40 pm

I am guessing you could use a for each, sorted by IID.

When object.IID = 0 reset a counter to 1 which increments with 1 each time its assigned to tail piece.

counter = 1

event
for each tail order by tail.IID ascending
sub
tail.IID = 0
action
set counter to 1
sub
blank event
action
tail set variable tail.number = counter
Who dares wins
B
57
S
17
G
21
Posts: 1,880
Reputation: 19,623

### » Mon Apr 20, 2015 6:29 pm

@SpookySkeleton
Give the tail pieces a private variable called "index".
Then every time you create a tail piece set their index to -1. Finally to do the ordering use a "for each ordered" loop:

For each tail ordered by tail.value('index') ascending
--- tail: set index to loopindex

That will make the tail piece right behind the player always be 1 counting up to the oldest one. This loop also cleanly handles destroying pieces, so they will be recalculated correctly.
B
97
S
36
G
131
Posts: 5,517
Reputation: 83,472

### » Wed Apr 22, 2015 11:58 pm

@lennaert
Thanks guys! This solved my problem, and more!
@R0J0hound
woof woof!
B
24
S
7
G
1
Posts: 36
Reputation: 2,049