Type Functionality
BlitzPlus Forums/BlitzPlus Programming/Type Functionality
| ||
Is there any way of 'remembering' which object in a type array I'm currently working on? Currently, I'm using a boolean flag within the array, but I'm dealing with something a bit more complex - here goes: I've got an array of units, and I'm increasing an initiative number on each of these units until it matches a target number, after which that unit's "selected" variable is toggled 'on'. If I run my NextActiveUnit function again afterwards, it starts with the first unit in the list- meaning that units near the end of the list might rarely or never see an increment. I can think of a lot of workarounds (storing the unit's ID in a function, or searching through until I find the selected unit and then starting my search from there) but these seem to be wasteful of processor cycles/memory. Is there a way to store the direct pointer/index of an object in a type array so that I don't have to do this? Thanks, Telemental |
| ||
I've read it multiple times, but I guess I still don't quite understand what you're asking. Do you have an array of custom type instances? Do you have an array inside a type instance? One reason I don't understand is because whether it's an array element or a type you want direct access to, both are possible: 1) Arrays are indexed (by definition) 2) Types can be assigned to regular old % variables So, if you had an array of types, you might access it like this: array(3)\importantfield At any rate, would you mind describing your data structures a bit more? Or on the other hand, maybe someone else will understand better than me. =) |
| ||
No problem: I'm just using the generic TYPE data type, cycling through with a For...Each loop. (I didn't even realize you could create an array containing TYPEs). The link to my project is http://www.slackify.com/Downloads/Op%20Firefly/PC-Game/ds002/ds002.zip In this source code, the UNIT.BB file defines the unit TYPE. Currently, the code selects a unit when you click on it, then allows you to perform actions; I'm trying to find out a way of forcing an initiative system and am running into difficulty keeping track of which unit TYPE object I'm referring to. (The array thing might be the answer.) If any of you hackish types find anything else I could have trimmed down in this code, all opinions are welcome. Thanks, Telemental |
| ||
Say, that's pretty good. Organized and logical. Anyway, (ah!) I understand your issue now. No, you do not have to cycle through the Type list each time you want to find a specific type. Remember, you can make yourself a Type pointer (a variable that represents one of the Type instances), like this, for example: Global CurrentUnit.unit = Null This is just a pointer that is pointing to nothing. It is ready to be assigned to any new instance that you create. Of course you can also do something like this: Local Enemy.unit = New unit This creates a new Type instance, automatically inserts it at the end of the internal Type list (as you know), and assigns the variable 'Enemy' to that single instance. Remember that once you define a custom type, you can declare it and use it just like you would any other variable type, e.g. VariableName[type], where [type] is '%', '#', '$', or '.unit'. So now, in your Clicked() function, where you are selecting the unit, you might say this: UnitSelected = True For Soldier.unit = Each unit If Soldier\x = CursorX And Soldier\y = CursorY Then Soldier\Selected = True CurrentUnit = Soldier ; <-- new line Movement(Soldier\x,Soldier\y,Soldier\move) ;remove later End If Next ...and in your Movement routine, you won't have to cycle through each Unit instance. You just say Soldier = CurrentUnit. As a side effect, you don't need UnitSelected. You can test that like this: if (CurrentUnit <> Null) then ... And if you really wanted, when you create each Unit instance, you could put each one in an indexed array, just like this: Dim Units.unit(NUMUNITS) For i = 0 to NUMUNITS Units(i) = new unit Units(i)\x = ... Units(i)\y = ... Next Then you can access any one you want, directly. Anyway, I hope this helps. Ask if you have any questions. |
| ||
I remember talking about this in the past ... not specifically for your example, but it never hurts to mention this stuff again ... A lot of folks worry that iterating through an ENTIRE type list is "too time consuming" so they end up being more worried about trying to track types individually instead of the full iteration of the type collection. I've actually run some benchmarks (might even have the code around here somewhere) where I threw a couple thousand animated items on the screen and did mouse collision detection doing full iterations as well as type-specific coding. The difference was honestly negligible. I'd say unless your types list gets WAY out of control (like 5-6 digits) you may not see the difference. |
| ||
I'd say unless your types list gets WAY out of control (like 5-6 digits) you may not see the difference. Interesting. This goes against the usual understanding (i.e. Order(1) is always significantly faster than Order(n), especially when performed multiple times). It makes me wonder why the difference was only negligible. |
| ||
Good deal. I sense another code revision coming up... |
| ||
Thanks for all the help. With the ability to maintain pointers, I should be able to get around some tough obstacles. |
| ||
soja - Yeah, I thought it seemed odd too - just a testiment to how damn fast Blitz is :) |