What you want is a linked list ...
The following code was for a tile-based system that would allow you to position as many units to a tile as you wish (it was for a war strategy game I think). You should find it relatively easy to adapt to what you want (Tile_t becomes your object type, Unit_t becomes your limb type)
type Tile_t
Head as integer
` Other tile information added as needed
endtype
type Unit_t
PrevUnit as integer
NextUnit as integer
TileX as integer
TileY as integer
` Other unit information added as needed
endtype
dim Tiles(4, 4) as Tile_t
dim FreeUnits() as integer
dim Units() as Unit_t
global FreeUnitCount as integer = 0
print "***Initialising***"
InitialiseTiles()
InitialiseUnits()
print ""
print "***Creating 4 units***"
UnitA = NewUnit()
UnitB = NewUnit()
UnitC = NewUnit()
UnitD = NewUnit()
print ""
print "***Adding all units to tile (0,0)***"
AddUnitToTile(UnitA, 0, 0)
AddUnitToTile(UnitB, 0, 0)
AddUnitToTile(UnitC, 0, 0)
AddUnitToTile(UnitD, 0, 0)
print ""
print "***Clearing all units from tile (0,0)"
ClearTileList(0, 0)
print ""
ShowTileList(0, 0)
print "The free list now contains "; FreeUnitCount; " items"
wait key
end
function ShowTileList(TileX as integer, TileY as integer)
local Unit as integer
local Count as integer
` Get the pointer to the first in the list
Unit = Tiles( TileX, TileY ).Head
Count = 0
print "Tile "; TileX; "/"; TileY; " contains ";
` Repeat while the unit number is a valid unit
while Unit >= 0
inc Count
print Unit; " ";
` Get the next unit number
Unit = Units( Unit ).NextUnit
endwhile
if Count = 0
print "***nothing***"
else
print ""
endif
endfunction
function ClearTileList(TileX as integer, TileY as integer)
local Unit as integer
local Count as integer
Unit = Tiles( TileX, TileY ).Head
while Unit >= 0
FreeUnit( Unit )
DeleteUnit( Unit )
Unit = Tiles( TileX, TileY ).Head
endwhile
endfunction
function AddUnitToTile(Unit as integer, TileX as integer, TileY as integer)
local NextUnit as integer
` If this unit has a tile coord, it needs removing from that tile first
if Units(Unit).TileX >= 0 then FreeUnit(Unit)
print "Adding unit "; Unit; " to tile "; TileX; "/"; TileY
` Get the top of the current list
NextUnit = Tiles( TileX, TileY ).Head
` Make this unit point to the old 'top' as the next, and none as the previous
Units(Unit).NextUnit = NextUnit
Units(Unit).PrevUnit = -1
` Make the old one point back to this one
Units(NextUnit).PrevUnit = Unit
` Make the tile point to this unit
Tiles(TileX, TileY).Head = Unit
` Store the tile coords
Units(Unit).TileX = TileX
Units(Unit).TileY = TileY
endfunction
function FreeUnit(Unit as integer)
local PrevUnit as integer = -1
local NextUnit as integer = -1
if Units(Unit).TileX < 0 then exitfunction
print "Removing unit "; Unit; " from tile "; Units(Unit).TileX; "/"; Units(Unit).TileY
PrevUnit = Units(Unit).PrevUnit
NextUnit = Units(Unit).NextUnit
` If not the end of the list, point the next unit back at the previous one
if NextUnit >= 0
Units(NextUnit).PrevUnit = PrevUnit
endif
` If not at the top of the list, point the previous unit to the next one
if PrevUnit >= 0
Units(PrevUnit).NextUnit = NextUnit
else
` If we are at the top of the list, make the tile point to the next unit
Tiles( Units(Unit).TileX, Units(Unit).TileY ).Head = NextUnit
endif
Units(Unit).TileX = -1
Units(Unit).TileY = -1
endfunction
function InitialiseTiles()
local i as integer
for i = 0 to array count( Tiles() )
Tiles(i).Head = -1
next i
endfunction
function InitialiseUnits()
` The units array is automatically has one item ... may change in future updates
DeleteUnit(0)
endfunction
function NewUnit()
local NewItem as integer
` If the free list has something in, use the top one and decrease the counter
if FreeUnitCount > 0
dec FreeUnitCount
NewItem = FreeUnits( FreeUnitCount )
print "Retrieved unit "; NewItem; " from the free list"
else
` Otherwise, extend the units array and return that item
array insert at bottom Units()
NewItem = array count( Units() )
print "Created a new unit "; NewItem
endif
` Initialise the new unit
Units(NewItem).PrevUnit = -1
Units(NewItem).NextUnit = -1
Units(NewItem).TileX = -1
Units(NewItem).TileY = -1
endfunction NewItem
function DeleteUnit(Unit as integer)
print "Adding unit "; Unit; " to the free list"
` Increase the free count
inc FreeUnitCount
` If the list is too small, enlarge it
if array count( FreeUnits() ) < FreeUnitCount then array insert at bottom FreeUnits()
` Store the unit in the free list at the top
FreeUnits( FreeUnitCount ) = Unit
endfunction