Another type problem

Blitz3D Forums/Blitz3D Beginners Area/Another type problem

Aussie(Posted 2010) [#1]
Hello all.

I have had this problem before & overcome it by making the types global, however this time it's not working.

I have a bullet, the bullet hits an enemy & the damage points of the bullet is deducted from the enemys life.

here is the section of the code where the problem is occouring.
If anyone could help it would be greatly apreciated.
Type bullet
	Field obj
	Field bspeed#
	Field bdamage#
	Field life#	
End Type

Function create_bullet(parent)
	b.bullet = New bullet
		b\obj = CreateSphere(10,parent)
			EntityType b\obj,bullet_col
			ScaleEntity b\obj,.25,.25,.25
			PositionEntity b\obj,0,0,2
			EntityColor b\obj,0,255,0
			EntityFX b\obj,1
		b\life = 50
		b\bspeed = 4
		b\bdamage = 1
		
	EntityParent b\obj,0
End Function

Function update_bullet()
	For b.bullet = Each bullet
		MoveEntity b\obj,0,0,b\bspeed
		b\life = b\life - 1
		;If EntityX(b\obj) < -71 Then
		;	PositionEntity b\obj,70,0,EntityZ(b\obj)
		;Else If EntityX(b\obj) > 71 Then
		;	PositionEntity b\obj,-70,0,EntityZ(b\obj)
		;Else If EntityZ(b\obj) < -51 Then
		;	PositionEntity b\obj,EntityX(b\obj),0,50
		;Else If EntityZ(b\obj) > 51 Then
		;	PositionEntity b\obj,EntityX(b\obj),0,-50
		;End If

		If EntityCollided (b\obj,enemy_col) Then
			e\life = e\life - b\bdamage ; this is the line where it says object dose not exist
			FreeEntity b\obj
			Delete b
			
		Else If b\life < 0 Then
			FreeEntity b\obj
			Delete b
		End If
	Next

End Function



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Type enemy
	Field mesh
	Field x#,y#
	Field xv#,yv#
	Field life#
	Field alpha#
	Field rotx#,roty#,rotz#
	Field order% = -1
End Type

Function create_enemy()
For a = 0 To 9
	e.enemy = New enemy
		e\mesh = CopyEntity(cube)
		EntityType e\mesh,enemy_col
		e\x# = Rnd(-74,74)
		e\y# = Rnd(-54,54)
		e\xv# = Rnd(-.5,.5)
		e\yv# = Rnd(-.5,.5)
		e\rotx# = Rnd(-2,2)
		e\roty# = Rnd(-2,2)
		e\rotz# = Rnd(-2,2)
		e\life# = 10
		e\alpha# = 1
		e\order = e\order - 1	
	PositionEntity e\mesh,e\x,0,e\y
	EntityOrder e\mesh,e\order
Next
End Function

Function update_enemy()
	For e.enemy = Each enemy
		e\x = e\x - e\xv
		e\y = e\y - e\yv
			
		PositionEntity e\mesh,e\x#,0,e\y#
		
		If e\x# < -75 Then
			e\x# = 74
		Else If e\x# > 75 Then
			e\x# = - 74
		Else If e\y# < -55 Then
			e\y# = 54
		Else If e\y# > 55 Then
			e\y# = -54
		End If
		
		TurnEntity e\mesh,e\rotx#,e\roty#,e\rotz#
			If e\life =< 0 Then
				FreeEntity e\mesh
				Delete e
			End If
	Next
End Function



andy_mc(Posted 2010) [#2]
you're not cycling through teh enmy objects, does it say the enemy object does exist or the bullet one?


Aussie(Posted 2010) [#3]
It says the enemy objects don't exist.
I have tryed a for next loop but it didn't work.
i will give it another go.
Cheers :)


Aussie(Posted 2010) [#4]
Don't know what i was doing befor but it is working now.
Function update_bullet()
	For b.bullet = Each bullet
		MoveEntity b\obj,0,0,b\bspeed
		b\life = b\life - 1
		;If EntityX(b\obj) < -71 Then
		;	PositionEntity b\obj,70,0,EntityZ(b\obj)
		;Else If EntityX(b\obj) > 71 Then
		;	PositionEntity b\obj,-70,0,EntityZ(b\obj)
		;Else If EntityZ(b\obj) < -51 Then
		;	PositionEntity b\obj,EntityX(b\obj),0,50
		;Else If EntityZ(b\obj) > 51 Then
		;	PositionEntity b\obj,EntityX(b\obj),0,-50
		;End If
		
		If b\life < 0 Then
			FreeEntity b\obj
			Delete b
		End If

		For e.enemy = Each enemy 
		If EntityCollided (e\mesh,bullet_col) Then
			e\life = e\life - b\bdamage
			FreeEntity b\obj
			Delete b
		End If
	Next
Next

End Function



_PJ_(Posted 2010) [#5]
It says the enemy objects don't exist.



	If b\life < 0 Then


			FreeEntity b\obj
			Delete b
		End If

		For e.enemy = Each enemy 
		If EntityCollided (e\mesh,bullet_col) Then
			e\life = e\life - b\bdamage
			FreeEntity b\obj
			Delete b
		End If





Very often, when iterating through Types and deleting the instances, you can run into problems when you try to reference the field of a Type instance that has been deleted.

The deleted type instances return as 'Null' when you try to access the type object, this can be checked for with something like:

For IterateTypes.MyType = Each MyType
If (IterateTypes.MyType<>Null)
; Now you can do stuff with the type instance's fields
End If
Next



Aussie(Posted 2010) [#6]
Thanks Malice.
That's handy to know.