Returning the Complete Array of types
Blitz3D Forums/Blitz3D Programming/Returning the Complete Array of types
| ||
OK I have an array of types, easy enough. The array can contain 1 or more elements for each type (I would like to do a case for no type as well but I'm going for simplicity). Now how can I return from my array of types ALL the elements of the array of types. I've tried this and variations on it but it doesn't work. What am I missing? Graphics 800,600,32,2 SetBuffer BackBuffer() Type cars Field wheels End Type Dim race.cars(100) For i=1 To 100 c=6 While c>=5 b=Rnd (4) If b>=2 Then race(i)= New cars race(i)\wheels=Rand (4) EndIf c=Rnd(10) Wend Next For m=1 To 100 For s=Each cars Print race(m).s\wheels Next Next Any ideas? I want to return the complete array (1 to 100) with all elements of the type (however many there may be) and disregard any null elements. How do you do this? Thanks, Darkheart |
| ||
For rc.cars = Each cars ;blah Next |
| ||
Is this what you mean ?Graphics 800,600,32,2 SetBuffer BackBuffer() Type cars Field CarNumber,wheels End Type Dim race.cars(100) For i=1 To 100 c=6 While c>=5 b=Rnd (4) If b>=2 Then race(i)= New cars race(i)\wheels=Rand (4) race(i)\CarNumber=i EndIf c=Rnd(10) Wend Next ;For m=1 To 100 For s.cars =Each cars Print s\CarNumber+ ". "+s\wheels Next ;Next I added Carnumber to the Car Type, so i could check output. |
| ||
Thanks Beaker I got it now, wasn't using the syntax properly. Darkheart |
| ||
I've got the same problem and can't see what i have done wrong?Type level Field id End Type Dim background.level(10) For i=1 To 10 For j=1 To 5 background(i)=New level background(i)\id=Rand(100) Next Next For i=1 To 10 For l.level=Each level Print i+" : "+background(i).l\id Next Next WaitKey End I keep getting "expecting next" errors. I get the same with Darkhearts version when I change 's=each cars' to 's.cars=each cars'... so what did you change DarkHeart? |
| ||
I don't think my problem is actually solved... I want to do an iteration of the array and return each element of the type in the array with the index number. Equivelent of doing: for i=1 to 10 print race(i).cars\wheels next But returning each element, when I try to do this I get wierd results. This returns the elements each time. Graphics 800,600,32,2 SetBuffer BackBuffer() Type cars Field wheels End Type Dim race.cars(100) For i=1 To 10 race(i)= New cars race(i)\wheels=Rand (4) Next For m=1 To 10 For rc.cars=Each cars Print "i number= " + m + " carval= " + rc.cars\wheels Delay 100 Next Next I want to return each element just once but via index number. So for example I could print ALL the types from index (4) but only those. Rather than EACH element of the array. If I try this: Graphics 800,600,32,2 SetBuffer BackBuffer() Type cars Field wheels End Type Dim race.cars(100) For i=1 To 10 race(i)= New cars race(i)\wheels=Rand (4) Next For m=1 To 10 For rc(m).cars=Each cars Print "i number= " + m + " carval= " + rc(m).cars\wheels Delay 100 Next Next I get, "Expecting variable assingment". Darkheart |
| ||
You only use the dot syntax when defining what type a variable is. Once it has been defined, you should not (need to) use it again. The following will produce the result you want:For m=1 To 100 If Not (race(m) = Null) Print race(m)\wheels EndIf NextIf you haven't defined all the cars in the race() array you need to check if they are valid, that's what the Null command is for. Fredborg |
| ||
That's what i want to do. This is for a 2d game using layers and I want to be able to loop through all of the images on each layer where each layer is defined by a part of the array. I have a feeling this isn't possible though :( |
| ||
But what if there is more than 1 element to the array like in the first example? Darkheart |
| ||
Where? I can't find any arrays with more than 1 element? Fredborg |
| ||
fredborg - have a look at my example. The array is created then 5 types are assigned to it. I only want to be able to get access to those 5 types when I access that particular bit of the array. |
| ||
Like this:Graphics 800,600,32,2 SetBuffer BackBuffer() Type cars Field wheels End Type Dim race.cars(100) For i=1 To 10 For h=1 To 3 race(i)= New cars race(i)\wheels=Rand (4) Next Next For m=1 To 10 For rc(m).cars=Each cars Print r Delay 100 Next Next But let's say that as it is in my real code (which is too long and has too many dependancies to use for this) the creation of additional elements in the type is conditional so I don't know how many there will be. However I then want to be able to pull up a specific index number e.g. (4) and use do something with all the elements of (4). But only those which have index number 4 and ignore the other elements. Darkheart |
| ||
Graphics 800,600,32,2 SetBuffer BackBuffer() Type cars Field wheels Field trunks Field drivers End Type Dim race.cars(100) For i=1 To 100 c=6 While c>=5 b=Rnd (4) If b>=2 Then race(i)= New cars race(i)\wheels=Rand (4) race(i)\trunks = Rand(20,50) race(i)\drivers = Rand(100) EndIf c=Rnd(10) Wend Next Repeat Cls Locate 0,0 car = Rand(100) Print "Car "+car+" is like this:" If Not (race(car) = Null) Print " Number of wheels: "+race(car)\wheels Print " Number of trunks: "+race(car)\trunks Print " Number of drivers: "+race(car)\drivers Else Print " My, my, car "+car+" does not exist" EndIf Print Print "Press any key to see another car!" WaitKey() Until KeyHit(1)Like this? Fredborg |
| ||
No, not the number of fields I mean the number of elements, let's say that each item in the array can have multiple cars. Through multiple loops of the New loop. Now I want to find all cars that have index number 4. The code I posted above has only 10 index references but contains 30 cars. I want to return all the cars for each index number. Darkheart |
| ||
erm... no The while c>=5 wend bit could create>1 type but it only stores the reference to the last type created. I am pretty convinced this (what i want) won't work and I will have to look at other ways of doing things. This could be time to look into using banks... :( |
| ||
But the array DOES contain 30 cars...You have to be able to get them back somehow.Graphics 800,600,32,2 SetBuffer BackBuffer() Type cars Field wheels End Type Dim race.cars(100) For i=1 To 10 For h=1 To 3 race(i)= New cars race(i)\wheels=Rand (4) Next Next For rc.cars=Each cars Print rc\wheels Delay 100 Next Here I can print them off quite happily as long as I do a complete iteration of the type which is very wasteful when you get a big array with lost of elements. Darkheart |
| ||
If I get what you're trying to say (or indeed is saying :), then you need to go about it another way. Either create a multidimensional array, or do it like this:Graphics 800,600,32,2 SetBuffer BackBuffer() Type Car_type Field wheels End Type Type Race_type Field car.car_type[100] End Type Dim race.Race_type(10) For i=1 To 10 race(i) = New Race_Type For h=1 To 3 race(i)\car.car_type[h] = New car_type race(i)\car[h]\wheels=Rand (4) Next Next For m=1 To 10 Print "Stats for race "+m For n=1 To 100 If Not (race(m)\car[n] = Null) Print " Car "+n+" has "+race(m)\car[n]\wheels+" wheels" Delay 100 End If Next NextMultidimensional array variant: Graphics 800,600,32,2 SetBuffer BackBuffer() Type Car_type Field wheels End Type Dim race.car_type(10,3) ;10 races, 3 cars in each For i=1 To 10 For h=1 To 3 race(i,h) = New car_Type race(i,h)\wheels=Rand (4) Next Next For m=1 To 10 Print "Stats for race "+m For n=1 To 3 If Not (race(m,n) = Null) Print " Car "+n+" has "+race(m,n)\wheels+" wheels" Delay 100 End If Next NextFredborg |
| ||
What I think happens is that blitz knows there are 30 cars created which is why 'for rc.cars=each cars' works fine. All the data in the array holds is the reference to the pointer to that particular types data :S This is the same as loading two images with the load image command and assigning them to the same variable. The image is still there, you just can't do anything with it anymore. At least that's the way i see it. I hope I'm wrong cos it will make my game a lot easier to program. BTW - my 'erm... no' was directed at Fredborg. I was typing my reply whilst you were posting yours. |
| ||
Doesn't look like I can do it then, because doing it that way means again doing 100 loops to return the full type. The way I was doing it before was setting up an mulit dimensional array full of silly values then reading the list until a silly value was encountered at which point I stopped and incremented the first index of the array. Then I thought, "A array of types would be much neater because I could just return the elments for each array index.", but it doesn't look like you can return them without predefining an array in the type (these cause problems when releasing memory). I guess I'll just have to go back to using my arrays, might put a feature request in for this though as it would be much more useful to use arrays of types if you could return just the elements required from a specific index number. Darkheart |
| ||
^ look up ^ added another example...Of course you can do it, it's Blitz for crying out loud :) Fredborg |
| ||
how about this? NOTE : I have changed it to something better Graphics 800,600,32,2 SetBuffer BackBuffer() Type Car_type Field wheels End Type Type Race_type Field car.car_type[10] Field count End Type Dim race.Race_type(10) For i=1 To 10 race(i) = New Race_Type race(i)\count=Rand(10) For h=1 To race(i)\count race(i)\car.car_type[h] = New car_type race(i)\car[h]\wheels=Rand (4) Next Next For m=1 To 10 Print "Stats for race "+m For n=1 To race(m)\count Print " Car "+n+" has "+race(m)\car[n]\wheels+" wheels" Delay 50 Next Next waitkey end I think this is what i am going to be using in my game. Tis the first time I have used types within types but it looks like it will save me a lot of hassle. |