Problems with Types

Blitz3D Forums/Blitz3D Beginners Area/Problems with Types

That's right! I'm converting my Arrays to Types!
But I have a few minor problems.
Here's the code:
Type bullet
Field count
Field shape
End Type

ammo.bullet=New bullet
ammo\count=100
ammo\shape=LoadSprite("Shot 1.bmp")
ScaleSprite ammo\shape,.2,.2
EntityType ammo\shape,type_missile
EntityRadius ammo\shape,.02
HideEntity ammo\shape

Type target
Field count
Field shape
End Type

For x=1 To 5
For z=1 To 5
foe.target=New target
foe\count=25
foe\shape=CreateCube()
ScaleEntity foe\shape,2,2,2
PositionEntity foe\shape,Rand(-600,800)+x*5,60,Rand(-600,800)+z*5
EntityType foe\shape,type_target
EntityRadius foe\shape,5,5
NameEntity foe\shape,Handle(foe) ;<<<I’m not quite sure why I put this in here
Next
Next

While Not KeyDown(1)

;Firing Bullets
If MouseHit(1) And reload=0
EntityParent ammo\shape,gun
PositionEntity ammo\shape,1,-7,10
RotateEntity ammo\shape,0,90,0
ShowEntity ammo\shape
EntityParent ammo\shape,0
PlaySound SOUNDbam
ammo\count=ammo\count-1
EndIf

bulletpath(ammo.bullet,foe.target)

UpdateWorld
RenderWorld
Flip
Wend
End

Function bulletpath(ammo.bullet,foe.target)
MoveEntity ammo\shape,.043,-.025,2.5
If CountCollisions(ammo\shape)>0
HideEntity ammo\shape
Else
entity=EntityCollided(ammo\shape,type_target)=True;If EntityCollided(ammo\shape,type_target)=True Then
foe.target=Object.target(EntityName(entity));<<<Not quite sure about this either
HideEntity foe\shape
HideEntity ammo\shape
EndIf
End Function

So here are the problems:

1) It keeps reusing the same bullet when I fire (that's my assumption)

2) It keeps saying "Entity Does Not Exist" and it points to this line:
foe.target=Object.target(EntityName(entity))

I'm not completely sure why I put those lines in there. But I thought it would fix the issue of nothing happening when my bullets collided with the target.

Also, I have set up everything else on the line of "type_target" and such.

I'm pretty sure I already answered this before. You need to put some error trapping in there. You can't be sure that any entity has been collided with or that that entity is actually a target.

Also, you hide the entity as soon as it collides with something so it will never make the second part of the condition.

Indent your code - it makes it so much easier to debug.

Type bullet
	Field count
	Field shape
End Type

ammo.bullet=New bullet
ammo\count=100
ammo\shape=LoadSprite("Shot 1.bmp")
ScaleSprite ammo\shape,.2,.2
EntityType ammo\shape,type_missile
EntityRadius ammo\shape,.02
HideEntity ammo\shape

Type target
	Field count
	Field shape
End Type

For x=1 To 5
	For z=1 To 5
		foe.target=New target
		foe\count=25
		foe\shape=CreateCube()
		ScaleEntity foe\shape,2,2,2
		PositionEntity foe\shape,Rand(-600,800)+x*5,60,Rand(-600,800)+z*5
		EntityType foe\shape,type_target
		EntityRadius foe\shape,5,5
		NameEntity foe\shape,Handle(foe) ;<<<I’m not quite sure why I put this in here
	Next
Next

While Not KeyDown(1)

	;Firing Bullets
	If MouseHit(1) And reload=0
		EntityParent ammo\shape,gun
		PositionEntity ammo\shape,1,-7,10
		RotateEntity ammo\shape,0,90,0
		ShowEntity ammo\shape
		EntityParent ammo\shape,0
		PlaySound SOUNDbam
		ammo\count=ammo\count-1
	EndIf
	
	bulletpath( ammo.bullet )
	
	UpdateWorld
	RenderWorld
	Flip
	
Wend

End

Function bulletpath( ammo.bullet )
	
	MoveEntity ammo\shape,.043,-.025,2.5
	If CountCollisions(ammo\shape) > 0
		entity = EntityCollided(ammo\shape,type_target)
		If entity > 0
			foe.target = Object.target(EntityName(entity))
			If foe <> Null
				HideEntity foe\shape
				HideEntity ammo\shape
			Else
				HideEntity ammo\shape
			EndIf
		EndIf
	EndIf
	
End Function



I apologize for the repetition.
I'm not used to Types or the EntityCollided command for that matter.
I'll try what you've suggested.

Success! The targets are disappearing as I wish. (Currently trying to make it so that it takes three shots to disappear)

I've also indented my code and it looks much better now. Thanks!

Now if I could only figure out how to keep the bullet from being reused...

This is how I create new types

Graphics3D 800,600,0,2
SetBuffer BackBuffer()


Global NewType.TypeName = CreateType()



While Not Keydown(1)
     UpdateTypes()
     
     UpdateWorld
     RenderWorld
     
     Flip
Wend
End


Type TypeName
     Field FieldName
End Type

Function CreateType.TypeName(optional=0)
     Local t.TypeName = New Type Name
          t\FieldName = optional
     Return t
End Function

Function UpdateTypes()
     For t.TypeName = Each TypeName
     
          ;Do something
     
     Next
End Function


Thanks.
I'll see what I can do with it.

Unfortunately, I couldn't get it to work with my code (probably my code's fault).