Casting an object

BlitzMax Forums/BlitzMax Beginners Area/Casting an object

Pavlov(Posted 2005) [#1]
How do you cast an element of an object array to a specific object? Take the following code:

Local A:Cities = Cities(Collided[i])

If Collided is an object array and I'm trying to get a specific element back into a Cities object called A, is this how its done in Blitz Max? Am I even close? When I try this it says A is null.

Thanks


AaronK(Posted 2005) [#2]
That is correct. Are you sure you've got a Cities object in the Collided?

I tried this code and it seems to do what you wanted.

Type Cities
	Method p()
		Print "I'm Cities"
	End Method
End Type

Type SomethingElse
End Type

Local Collided:Object[10];

Collided[0] = New Cities
Collided[1] = New SomethingElse

Local A:Cities = Cities(Collided[0]) 
Local B:Cities = Cities(Collided[1]) 		' B will obviously be null cause it's not a Cities object.

A.p()
If b = Null
	Print "B is NULL!"
EndIf



Aaron


Pavlov(Posted 2005) [#3]
Very sure but it comes back as null. What are the parameter(s) for a TList remove method? I couldn't find squat about it in the docs that came with BlitzMax.

Local Collided:Object[]

' reset the collisions
ResetCollisions 1

For Local A:AttackingMissile = EachIn AttackingMissilelst
CollideImage(A.anImage, A.x, A.y, 0, 0, 1, A)
Next

' attacking missile collides with a city
For Local B:Cities = EachIn CitiesLst
Collided = CollideImage(B.anImage, B.x, B.y, 0, 1, 0, Null)

If Collided
For Local i = 0 To Len(Collided) - 1
Local A:Cities = Cities(Collided[i])

If A = Null
DrawText "null", 100, 300
End If

CitiesLst.remove(A)
DrawText "city go boom!", 100, 200
Next
End If
Next


Perturbatio(Posted 2005) [#4]
Method Remove( value:Object )
Function ListRemove( list:TList,value:Object )



Pavlov(Posted 2005) [#5]
I got the collisions working correctly. I was looking a little too deep into what I was trying to accomplish with the object array returned from the CollideImage function. Below is a snippet of the game I'm writing.

' small function to do collisions
Function DoCollisions()

Local Collided:Object[]

' reset the collisions
ResetCollisions 1

' go through all the explosions looking for a collided image
For Local A:Explosion = EachIn ExplosionLst
CollideImage(A.anImage, A.x, A.y, 0, 0, 1, A)
Next

' attacking missile collides with an explosion
For Local B:AttackingMissile = EachIn AttackingMissileLst
Collided = CollideImage(B.anImage, B.x, B.y, 0, 1, 0, Null)

If Collided
AttackingMissileLst.remove(B)
score :+ 20
End If
Next

' go through all the attacking missiles looking for a collided image
For Local A:AttackingMissile = EachIn AttackingMissilelst
CollideImage(A.anImage, A.x, A.y, 0, 0, 1, A)
Next

' attacking missile collides with a city
For Local B:Cities = EachIn CitiesLst
Collided = CollideImage(B.anImage, B.x, B.y, 0, 1, 0, Null)

If Collided
CitiesLst.remove(B)
End If
Next

End Function