Limitless Array
BlitzMax Forums/BlitzMax Beginners Area/Limitless Array
| ||
Is it possible to create a limitless array? For instance, when you create an array you have to specify what its maximum is (unless it's empty) like so: MyArray[30] That array would have a maximum of 30 'entries'. Is there a way to make it unlimited? I ask because in my gorilla remake I store all the buildings in an array of a type, and I let the user dicide how many buildings there are, up to a mamimum of 30. It works fine, 30 is more than enough, but I'm just curious, what if I wanted the users to select as many buildings as they wanted, and are not limited to 30? EDIT: I suppose I would just create a new array with the max number the same as the number of buildings the user wants, instead of just editing the same array... |
| ||
a=a[..200] 'resize a[] to 200 elements |
| ||
arrays can be resized at any time and don't need their size declared before using:Local myArray:Int[] Local i:Int SeedRnd MilliSecs() myArray = myArray[..10] ' give it 10 elements For I = 0 To myArray.length myArray[i] = Rand(0,100) Next For I = 0 To myArray.length Print myArray[i] Next 'resize to a random number myArray = myArray[..Rand(10 , 100)] Print "Array length: " + myArray.length |
| ||
Ah, you can resize them... Thanks. I was trying to think of a reason to use lists over array's of type's. |
| ||
is there a (speed-)advantage for using arrays over TLists? i'd like to be able to add items quickly to an array/list of undefined size. Resizing the array seems a little less handy than using ListAddLast and what exactly does TLink do? the IDE-help doesn't give me much info. |
| ||
Well the maiin problem of [..200] is that it make a whole new array and copies the old array over to the new array. So thats quite a big hit if you are resizing arrays all the time. The reason we sudjested array resizing for this question was that the number of buildings was set once for each game, and so array resizing was the simplest solution. (Well ok, just recrateing the array each time was the simpleist, but you know). Now if what was wanted was say adding a new unit, or such during game play time, then array resizing might not be the correct way. And possibley TLists would be better. Also it a lot lot easier to remove elements from within a Tlist. Basicly you use arrays when the siz is going to be basicly static during gamePlay time, and you use Tlist when you want more active control. However its a lot easyer to find say the tenth elemet in an array than a Tlist, so again you need to concider how you are going to use the data. If you are going to look through the building list (for example), every frame, then you are not looking at the first entry then the second entry, but rather the first entry and then the next entry. I know this might sound stupid if you are concidering whether to use Tlists or arrays, but often you can use both. That is you have a Tlist and then you have an array with the TLinks in it. (But you only do this when the advatage of using both outwights the disadvantage of using both) Now TLink is the actual handle for each "element" of the TList, so if for example you do keep them in an array, then you can simply "look" for each member of the list rather than looping though them all. My advice would be, use arrays right up until you need to use Tlists, then find or write a really good class to handle TLists and then use it from then on [By really good I mean one you understand]. (some people would say that you should put all the objects you create in A Tlist al la B3d, But then you run the Problem in the "How many instances are left", because the Tlist element is always there, so no GC untill you destroy the TLink/element) |
| ||
I wrote a list type that uses arrays a little while ago (there are some issues with it, but it works fine). http://www.blitzbasic.com/codearcs/usercode.php?user=1870 |
| ||
How would I resize an array of a type? Like my Create function creates the array like so: Function Create:City(amount:Int,maxheight:Int,minheight:Int) Local c:City=New City c.Buildings=New Building[30] For Local i:Int=0 To amount-1 c.Buildings[i]=New Building Next Return c End Function How exactly would I resize the Buildings:Building[] array? |
| ||
c.building = c.building[..n] n is the size you want to resize it to. you must initialize any added index element with "new building" in order to use them or you will get an unhandle error. |
| ||
In a = a[..200] Who said a wasnt a user type? |
| ||
yes, who said that? Maybe he understands better now. or not. |
| ||
Eh... I'm getting an error with this: Function Resize:tilemap(tnumx:Int,tnumy:Int) Local tm:tilemap=New tilemap tm.tile=tm.tile[tnumx,tnumy,11] Return tm End Function Identifier 'tile' not found. |
| ||
we need to see the type "TileMap" |
| ||
Type tilemap Field tset:TImage,tiles:tile[,,] Function Create:tilemap(tsx:Int,tsy:Int,timg$,tnumx:Int,tnumy:Int,frms:Int) Local tm:tilemap=New tilemap tm.tset=LoadAnimImage(timg$,tsx,tsy,0,frms) tm.tiles=New tile[tnumx,tnumy,11] For Local l:Int=1 To 10 For Local x:Int=0 To tnumx-1 For Local y:Int=0 To tnumy-1 tm.tiles[x,y,l]=New tile Next Next Next Return tm End Function Function Resize:tilemap(tnumx:Int,tnumy:Int) Local tm:tile=New tile tm.tile=tm.tile[tnumx,tnumy,11] Return tm End Function End Type with the Draw() method taken out as it's big. |
| ||
Function Resize:tilemap(tnumx:Int,tnumy:Int) Local tm:tile=New tile tm.tile=tm.tile[tnumx,tnumy,11] Return tm End Function tm is a tile not a tilemap. Hence tm doesnt have a field tile[] |
| ||
I guess, I comfused you more. Go to the Blitzwiki and read on arrays maybe it can help you better: http://www.blitzwiki.org/index.php/Arrays <--- this explains arrays and slices http://www.blitzwiki.org/index.php/Slices <----this explains slices |