Getting the index of an array...
BlitzMax Forums/MiniB3D Module/Getting the index of an array...
| ||
Hey guys, this is probably a real easy question to answer... I have an TEntity array and i then use camera pick: pick:TEntity=CameraPick(camera,MouseX(),MouseY()) How the hell do I get the index of the array object that pick holding? I know I can do it with a for next statement, but thats just stupid and will slow things down sooooo much. Any other ideas? |
| ||
Why would you need access to the array? The array will only point to the same Entity returned on 'pick'. Change 'pick' properties, the array will reflect those changes. |
| ||
Even if there was a native function to seek the index of a value, it would be a For->Next loop comparing the values of each index in the array against the one you wanted to find. To optimize your code and avoid having to seek the whole array for something equal to the picked entity, you can store the index of the entity in the entity's own name. You'd do this at the time you append\store the entity in the array: Function appendEntity:TEntity[](array:TEntity[], entity:TEntity) Local newSize:Int = (array.length + 1) array = array[..newSize] temp[newSize-1] = value 'Add the new entity to the last slot. 'I'm not sure if this '-1' is correct here; the documentation is wrong when explaining slices, so you'd have to test to know it right. End Function Function findIndex:Int(entity:TEntity) Local tempName:String = EntityName(entity) Local stringIndex:Int = tempName.length-1 Local arrayIndex:String = "" 'Value to be returned. Repeat arrayIndex = tempName[stringIndex] + arrayIndex stringIndex = stringIndex - 1 Until tempName[stringIndex] = "_" Return Int(arrayIndex) End Function '______________________________________________ appendEntity( myArray, entity) 'Call the function. This should be done for every entity. '[...] 'When it's time to find out the index of the picked entity: index:Int = findIndex(pickedEntity) 'Got the index. |
| ||
@SLotman: I dont need access to the array. But I do need to know the number of the entity in the array. Which you cant get from just reading pick. @Kryzon: Ok I didnt even realise you could do that. I'll give it a go. Again, thanks for your help. |
| ||
Function PositionOfObjecInList:Int(list:TList, target:Object) Local position:Int = 0 For Local onObj:Object = EachIn list If(onObj = target) Then Return position position:+1 Next Return -1 End Function What I use for finding positions, should work for you. Works on generics so it should work for any list. |
| ||
lol... something occurred to me. It's as simple as going to your sidesign.minib3d source and adding a field to the TEntity.BMX definition:Field arrayIndex:Int Then re-build the module. When storing the entity in the array you fill that property with the appropriate value and later retrieve it from the picked entity. It's lightning fast and doesn't affect the rest of the framework. pick:TEntity=CameraPick(camera,MouseX(),MouseY()) index = pick.arrayIndex 'Do what you want with the index. '[...] Last edited 2011 |
| ||
True, but it involves modifying the module, which you now have to remember to re-modify any time you re-install, move computers, or upgrade. Depends on if that time is execution critical, I prefer sticking with stock so I don't have to patch things constantly (too many modules, too many computers...) but if it impacts performance in a meaningful way then why not take the shortcut, that's the point of an exposed extension structure :0) |
| ||
Kryzon, that sounds like a bloody fantastic idea. The only issue I have now is that my build modules menu item is disabled. Blitzmax just doesnt want to do it anymore. Yet more things stand in my way of success lol. |
| ||
And that problem has been fixed by putting my environment vars back to what they should have been. I have no idea what changed them, but they got changed. Probably a bloody windows update or something. Anway... Thanks for your help guys! |
| ||
Hey Kryzon, is there a perticular way I should be storing the arrayIndex? I tried this: cube[gridcount].arrayIndex = gridcount but its coming up with an error: Identifier 'arrayIndex' not found. I have put the Field arrayIndex:Int in the TEntity.bmx under: Field no_seqs=0 Field anim_update Is that correct? Sorry but I have previously only worked with b3d so this way of doing things is a little different for me to grasp. |
| ||
Dont worry, its coz cube wasnt defined as TEntity. |