iterating through types as a function?
Blitz3D Forums/Blitz3D Programming/iterating through types as a function?
| ||
I'm writing something where I need to iterate through a type to find the one with a particular \ID, and then grab some other \values from it. Rather than insert that code each time I need to do that, I'm inserting an external function to iterate through the types until it finds the one with the right id, then ending. My question is this: once I end the function and return to the main program, will that type item still be the active one? I'm not making this very clear: okay, I have type TRUCK. I want to iterate through until TRUCK\ID$ = "bigtruck", and then make x# = TRUCK\x# etc etc. Can I use a function like this: Function findtruck( id$ ) truck.truck = First truck While truck\ID$ <> id$ truck = After truck Wend End Function ... and then in the main program use this: blah blah findtruck( "bigtruck" ) x# = truck\x# etc Or will it happen so that when the function ends and returns to the main program, the truck\x# I use will again be the First truck? |
| ||
There is no "active one". What you need is to make your function return the found object, or null if not found.Function findtruck.truck( id$ ) For t.Truck = Each truck If truck\ID = id THen Return t End If Next Return Null End Function Then: blah blah t.truck = findtruck( "bigtruck" ) If t = Null Then ;handle case where not found Else x# = truck\x# etc EndIf |
| ||
I would suggest using a Array of Types. I would also suggest using a integer for Id instead of a string for speed. Run the code below its very fast!;truck object Const TRUCK_MAX%=24 Type truck Field id% Field typeid% ;big=1, small=2, long=3, etc Field x# End Type Dim truckID.truck(TRUCK_MAX%) ;<-- truck Type Array ;New truck Function truckCreate.truck(id%,typeid%,x#) this.truck=New truck this\id%=id% this\typeid%=typeid% this\x#=x# truckID.truck(this\id%)=this.truck ;<-- assign truck to array Return this End Function ;create a dozen various trucks with random x# For loop = 1 To 12 truckType=Rnd(3)+1 truckX#=Rnd(-100,100) truckCreate(loop,truckType,truckX#) Print "Truck #"+Str(loop)+", Type="+Str(truckType)+", X#="+Str(truckX#) Next ;find truck truck.truck=truckID(5) x# = truck\x# Print Print "You Selected Truck #"+Str(truck\id%)+", Type="+Str(truck\typeid%)+", X#="+Str(truck\x#) Print "X#="+Str(x#) WaitKey() |
| ||
Both very useful, thanks. I am using integers instead of strings, I just put that to explain better. I'm not familiar with Array Types, are they documented more fully somewhere? [edit] With respect to array types, does that limit you to a certain amount, unlike plain old type types where you can add to the list ad infinitum? or am I wrong there? |
| ||
With respect to array types, does that limit you to a certain amount, unlike plain old type types where you can add to the list ad infinitum? The answer is no. Technically, you cannot create an infinite number of objects due to memory limitations. For improved memory management, you should set a limit to the number of objects created. You could always use a very large number.Type Arrays are documented in the Blitz Basic Language Reference: Custom types. Another possibility is to use the Object/Handle Functions. However, these functions are not documented. |
| ||
The reason I ask about limits, is that I thought: When you dim an array, you set aside that amount of memory, whereas when you define a type, it's dynamically sized. So that if you wanted to dim a huge array you're possibly wasting a lot of memory. That's what I assumed, anyway. |
| ||
you set aside that amount of memory, whereas when you define a type, it's dynamically sized This is the exact reason why limits should be set on the number of objects created.There are truly very few scenerios that really require infinte objects or dynamic memory management. Reserving and controlling your memory usage is more effecient in IMHO. |
| ||
Sounds reasonable. :) |
| ||
An array of Types is actually just an array of Type pointers, at 4 bytes per pointer. There are no special array Types, these are just array storage slots for storing the pointer values of standard Blitz Types. |