Question regarding object arrays and lists

BlitzMax Forums/BlitzMax Programming/Question regarding object arrays and lists

Grim(Posted May) [#1]
So, let's say I have a sim-like game where I want the player to be able to assign villagers to jobs at certain buildings.

The type for buildings contains a TList that links to each villager object currently working there.

The villagers on the other hand are stored as a global array of types that grows in size every time a new villager moves in, so they can more easily be indexed.

I've read that slicing an array to resize it essentially just creates a new copy of the array in memory. So then what happens to lists that link to elements of the old array? Would they still point to the correct objects, or would I have to change it so the list contains indexes rather than array elements?


TomToad(Posted May) [#2]

This sample shows that when slicing adds elements to the end of the array, then the items in the list still match up to the original array index. If slicing adds elements to the beginning of the array, then the list items will now be at a different index.

It follows that adding elements to the middle of the array or removing elements will change the listed index depending on where in the array the slicing is done.


Grim(Posted May) [#3]
Ah, thank you. It sounds like I had the right idea for coding the thing I described, since the worker lists would still be referencing the same villagers even if their array index happens to change. I'm glad it works that way.

Am I correct in assuming this is because arrays and lists point to objects independently of where they reside in memory?

I seem to recall that objects can exist on a list even if they can no longer be referenced by their original declaration, (e.g. adding a local variable to a global list) but it was always kind of second nature to me. I never had to put much thought into the difference between copying data or just pointing to it from another location.