Direct access to an instance.
BlitzMax Forums/BlitzMax Beginners Area/Direct access to an instance.
| ||
I'm currently making homming missles and need a way for them to find the target without using a for loop to change the direction to that of the target. Is there a way to directly access instances without iterating with a for loop through the list of target instances? Perhaps the missle could have a field object value for the target instance to follow. |
| ||
Ah I have fixed it. Thanks. |
| ||
No problem! (literally...) |
| ||
Do I detect sarcasm? :) |
| ||
For the benefit of anybody that doesn't know, you can use tList.valueAtIndex to retrieve an object from a tList without iterating through the entire list. |
| ||
valueAtIndex ITERATES through the list. (it only knows first and last object, anything else has to be found through iteration) If you need to get a specific instance, either use Arrays or TMap which both have far better mechanisms to get a specific object by index or by key (TMap) If you need access to specific objects within a list, add a link:TLink field to your type and save the return of addfirst / addlast there so you can easily access the respective list container object. |
| ||
Yeah, "valueAtIndex" is a fun little performance trap I've fallen into in the past. It's not doing anything fancy. |
| ||
why not just use arrays? |
| ||
heres an interesting little test showing Dreamora and Warrens performance notes. definately avoid using ValueAtIndex on large lists if at all possible.Framework BRL.Basic Import BRL.LinkedList Import BRL.Map Local list:TList=CreateList() Local map:TMap=CreateMap() Local arr:Object[100000] Local testObj:String="The Value " Local findObj:Object ' populate For Local i:Int=0 Until 100000 list.AddLast(testObj+" "+i) map.Insert(String.FromInt(i),testObj+" "+i) arr[i]=testObj+" "+i Next Local start:Int start=MilliSecs() findObj=list.ValueAtIndex(99999) Print("list.ValueAtIndex() found "+String(findObj)+": "+(MilliSecs()-start)) start=MilliSecs() findObj=map.ValueForKey(String.FromInt(99999)) Print("map.ValueForKey() found "+String(findObj)+": "+(MilliSecs()-start)) start=MilliSecs() findObj=arr[99999] Print("array index found "+String(findObj)+": "+(MilliSecs()-start)) |
| ||
Just as a sidenote: If the list does not change very often, you can use the to array function of the list and use array access there (temporally save it somewhere). That way you can get the best of both sides. |
| ||
Why would you want to find an object from an ID anyways? To link to it directly do somthing like this Type TMissile Field Target:TInstance ... If missile.x<missile.target.x Then missile.angle:+... |
| ||
Because an ID does not raise the ref count and thus reduces the chance for "forgotten links" I would assume ... and because you need it to interface with scripting languages. |