Get enemy type from collision

Blitz3D Forums/Blitz3D Beginners Area/Get enemy type from collision

Kippykip(Posted 2014) [#1]
I'm having a little trouble with my bullets, I found other threads but they don't have any code for getting a type
(e.g. enemyhealth = enemyhealth - 1)

I have 2 types, 1 for a bullet and 1 for the enemy
There were threads about getting the entity from a collision but not the type info

	Method Update()
		If(EntityCollided(bulletmesh,TYPE_TESTDUMMY))
			For Local collision_id = 1 To CountCollisions(bulletmesh)	
				DebugLog(collision_id)
				Local enemymesh:TEntity = CollisionEntity(bulletmesh,collision_id)
				If GetEntityType(enemymesh) = TYPE_TESTDUMMY
					HideEntity(enemymesh)
					Remove()
				EndIf			
			Next	
		EndIf
	endmethod

I'm actually using minib3d but I can only get the enemy entity from the bullet collision
Local enemymesh:TEntity = CollisionEntity(mesh,collision_id)

how could I get for example c\health (c.health in minib3d) of the enemy it touches from a collision?


Mikorians(Posted 2014) [#2]
Type array vs Type of collision?
Your question is unclear to me.

Sounds like a problem I went through recently.
I had to use my entities name field to cross reference it to the type array index number.
Like this pseudocode:
During the creation of your objects:
For t=1 to numenemies
A=createcube()
En.enemydata(t)\health=100
En.enemydata(t)\idx=A
NameEntity A,str(t)
Next
Detect enemy routine:
For x=1 to countcollisions(bullet)
I=collisionentity(bullet,x)
Z=val(entityname(I))
En.enemydata(Z)\health=En.enemydata(Z)\health-1
Next

The catch is you can't use findchild any more.


Kryzon(Posted 2014) [#3]
With BlitzMax you can take the elegant road.

Go to your MiniB3D source code and open "TEntity.bmx".
It should be located at ( Blitzmax\mod\sidesign.mod\minib3d.mod\inc\TEntity.bmx ).

Among the Fields of the TEntity declaration, add a...
	Field extra:Object
Then rebuild your MiniB3D module ( IDE -> Program -> Build Modules ).
Now when you create your 3D objects, you can assign an object to that "extra" field (that is, a reference to an instance of a custom Type object).

In your case, you would do this:
	Local enemy:TCharacter = CreateCharacter( "Alien", 13, 50.5, CHARACTER_ENEMY )
	
	'Assign a reference to the 'enemy' object in the object's mesh's 'extra' field.
	enemy.mesh.extra = enemy 

	[...]

	Method Update()
		If(EntityCollided(bulletmesh,TYPE_TESTDUMMY))
			For Local collision_id = 1 To CountCollisions(bulletmesh)	
				DebugLog(collision_id)
				Local enemymesh:TEntity = CollisionEntity(bulletmesh,collision_id)
				
				Local enemy:TCharacter = TCharacter( enemymesh.extra ) 'Cast the 'extra' field of the mesh as a TCharacter object. 
				
				If enemy Then

					'Use 'enemy' for something here, like subtracting one from its health.
					enemy.health :- 1

				EndIf			
			Next	
		EndIf
	End Method



Mikorians(Posted 2014) [#4]
Kyrzon- did you see my post to techlord in this forum?
Would you know what he'd meant in his example?
I hate having to sacrifice my entity names for type referencing.


Kryzon(Posted 2014) [#5]
Regarding his comment in this code-archive entry, he is mistaken that it's a "faster method to look up an entity".

In his example, he already knows what index of the array his object is stored in (index #2).
He does not use the name of the entity to discover that index, he only uses the index to retrieve the value from the array.
If he had used the name of the entity to store the index (as it is needed in a game, as you don't know what kind of entity you retrieved from a collision or pick), he might as well have stored the Handle() of the object that contains the entity as the code-archive entry originally illustrates.

A string can hold as much information as you append to it. You can build a string from several small strings, with one of these small strings being the handle of the object that contains the mesh and the other strings being additional information.

- - - - -
Also, read this: http://blitzbasic.com/Community/posts.php?topic=101889#1213224


Kippykip(Posted 2014) [#6]
Ended up using Kryzon's method, it's so useful!
Thanks everyone


RemiD(Posted 2014) [#7]
I have posted examples to show a way to store the kind and the id of a "pickable" and of a "collider/collidable", and to retrieve the kind and the id of a " pickable" after a pick, and to retrieve the kind and the id of a "collider/collidable" after a collision.
See :
http://blitzbasic.com/codearcs/codearcs.php?code=3094 (with dim arrays)
http://blitzbasic.com/codearcs/codearcs.php?code=3095 (with types)


Mikorians(Posted 2014) [#8]
Thanks!