Direct access to an instance.

BlitzMax Forums/BlitzMax Beginners Area/Direct access to an instance.

Ryan Burnside(Posted 2007) [#1]
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.


Ryan Burnside(Posted 2007) [#2]
Ah I have fixed it. Thanks.


marksibly(Posted 2007) [#3]
No problem! (literally...)


Xerra(Posted 2007) [#4]
Do I detect sarcasm? :)


GfK(Posted 2007) [#5]
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.


Dreamora(Posted 2007) [#6]
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.


Warren(Posted 2007) [#7]
Yeah, "valueAtIndex" is a fun little performance trap I've fallen into in the past. It's not doing anything fancy.


bradford6(Posted 2007) [#8]
why not just use arrays?


gman(Posted 2007) [#9]
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))



Dreamora(Posted 2007) [#10]
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.


Curtastic(Posted 2007) [#11]
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:+...



Dreamora(Posted 2007) [#12]
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.