Help with lists

BlitzMax Forums/BlitzMax Beginners Area/Help with lists

jkrankie(Posted 2006) [#1]
I'm trying to learn a bit more about linked lists, but have hit a bit of a wall, as i cant get the following method to work properly, i keep getting list index out of bounds errors.

In fairness i'm probably doing it in completely the worng way, so if anyone can suggest a better (and preferably working) alternative i'm all ears.

Anyway, the method controls the movement for some enemies that i used in Bullet Candy (which was made using structured programming rather than OO, with everything stored in arrays and suff) that basically chase down little golden ships and kill them. once they have made contact with the golden ship or the player collects the ship they are chasing, the goldenship is removed from the list and the enemies pick another one to chase or, if there are none left, go after the player.

The golden ships are called rescuables in the code, and a list of them is passed to the method as a parameter to the method, along with the players x and y coordinates.

here is the method:

Method updateHarvesters:TList(inList:TList,inX:Float,inY:Float)
	 
	If enemyType=2
			If inList.count()>0
				thisRescuable=rescuable(inList.ValueAtIndex(targetNumber))
				targetX=thisRescuable.x
				targetY=thisRescuable.y
				
				If thisRescuable.checkCollision(x,y,1,1)
				Print inlist.Count()
						inList.Remove rescuable(inList.ValueAtIndex(targetNumber))
				Print inlist.Count()		
						If inList.count()>0
							Local i:Int=Rand(0,inList.Count()-1)
							targetNumber=i
							Print "t:"+i
							thisRescuable=Null
							thisRescuable=rescuable(inList.ValueAtIndex(targetNumber))
							targetX=thisRescuable.x
							targetY=thisRescuable.y
						Else
			
							targetX=inX
							targetY=inY
			
						End If
						
				End If
			Else
			
				targetX=inX
				targetY=inY
			
			End If
			rotation=calculateAngle(x,y,targetX,targetY)
			radius=xVel*scaleX
			angle=calculateAngle(x,y,targetX,targetY)-90
			x=radius*Cos(angle)+x
			y=radius*Sin(angle)+y
		End If
		Return inList
	End Method


I hope someone can give me some tips or suggest a resolution for this.

Cheers
Charlie


Gabriel(Posted 2006) [#2]
I'm not much cop at reading other people's code, but why are you using list indices? Every time you remove an item from the list the indices break, and you have to recalculate them. If you're not, that may even be why it's breaking.

Why not just make Target an instance of the type instead of index within the list?


jkrankie(Posted 2006) [#3]
How do you mean? send an instance of the type to the method? how would i be able to find another target once one has been destroyed?

Cheers
Charlie


jkrankie(Posted 2006) [#4]
Ah, after a bit of a head scratch, i think i've got it. I didn't think that using an instace of a type like this would work. I guess i just made it difficult for myself without considering there could be an easier solution.

Method updateHarvesters:TList(inList:TList,inX:Float,inY:Float)
	 
	If enemyType=2
			If inList.count()>0
				
				targetX=thisRescuable.x
				targetY=thisRescuable.y
				
				If thisRescuable.checkCollision(x,y,1,1)
				
						inList.Remove thisRescuable
						
						If inList.count()>0
							Local i:Int=Rand(0,inList.Count()-1)
							targetNumber=i
							
							thisRescuable=Null
							thisRescuable=rescuable(inList.ValueAtIndex(targetNumber))
							targetX=thisRescuable.x
							targetY=thisRescuable.y
						Else
			
							targetX=inX
							targetY=inY
			
						End If
						
				End If
			Else
			
				targetX=inX
				targetY=inY
			
			End If
			rotation=calculateAngle(x,y,targetX,targetY)
			radius=xVel*scaleX
			angle=calculateAngle(x,y,targetX,targetY)-90
			x=radius*Cos(angle)+x
			y=radius*Sin(angle)+y
		End If
		Return inList
	End Method


Cheers
Charlie