Free character entity from Type collection

Blitz3D Forums/Blitz3D Programming/Free character entity from Type collection

Naughty Alien(Posted 2008) [#1]
..okay...after enemy in my game die, I do for example:

For Troll.enemy=Each enemy
If Troll\Die
Delete Troll
end if
Next

..so..its working fine..but character mesh remain visible on last position where it died..i cant free it because its also assigned trough Types just deleted from collection..I thought by deleting it from Type collection it will be freed, soo..how?


Moraldi(Posted 2008) [#2]
You need to create a copy of character mesh when creating Trolls and free this copy when deleting Trolls


Naughty Alien(Posted 2008) [#3]
hmm..what you mean by copy?? This is how i create trolls, so
after deleting it from Type collection they are still visible, but how if everything from collection should be freed?

Troll.enemy=New enemy
;storing data in to Types

Troll\ID=LoadAnimMesh("L1_Characters\Troll\Idle_01.b3d")
LoadAnimSeq(Troll\ID,"L1_Characters\Troll\Idle_02.b3d")
LoadAnimSeq(Troll\ID,"L1_Characters\Troll\Idle_03.b3d")

..blah..
blahhh


mtnhome3d(Posted 2008) [#4]
you need to use FreeEntity Troll\ID to make the troll dissapear. i think its cause you only delete the handle not the mesh. same as if you use the same name for 2+ objects it still loads it but the first objects handle is erased.
 c=createsphere()
c=createcube()
c=createcone(8)



Naughty Alien(Posted 2008) [#5]
..nope...if i do that, i got 'Unexpected error'...anyway, i did it on the way Moraldi suggested, so its working..


H. T. U.(Posted 2008) [#6]
Mayaman is right with the Delete command, it only erases handles. I've had this problem many times and have found it to be an easy fix. Here's what I'd do:

For Troll.enemy=Each enemy
If Troll\Die=True and Troll\gone=False
Delete Troll
Freeentity Troll\model
Troll\gone=True
end if
Next


Adding the troll\gone field ensures that the computer doesn't try to delete or free data that doesn't exist anymore.

EDIT: Oops, sorry about that mixup (Duh :))!


Vertigo(Posted 2008) [#7]
if Troll\model <> 0 then freeentity else do nothing at all works better for me. Never try to free something that doesnt exist... easy check... But yes you need to use copies.


Moraldi(Posted 2008) [#8]
Troll\ID=LoadAnimMesh("L1_Characters\Troll\Idle_01.b3d")

A 'stupid' question first: Why you named ID the animated mesh field? any way...
If you code:
If Troll\ID <> 0 then FreeEntity Troll\ID

then mesh troll will disappear from the scene...
Note: I suppose that "Idle_01.b3d" is an animated mesh and not only an animation sequence

Usually I load all models of my game in the initialization function:
Function Game_Init()
  ...
  Model_Troll = LoadAnimMesh("A Troll Mesh")
  ...
End Function

And then when I need to load a level of the game:
Function Level_Load()
  ...
  Troll1 = CopyEntity(Model_Troll)
  Troll2 = CopyEntity(Model_Troll)
  ...
End Function

If a Troll is killed then:
  FreeEntity(Troll1)

etc.


Dreamora(Posted 2008) [#9]
The code by H.T.U was nearly right
just vice versa.
First FreeEntity
Then Delete

if you delete the type first, you can not access it to get the entity


Naughty Alien(Posted 2008) [#10]
@ Moraldi
On that way you suggest I have one Trool more on scene(origin) and I dont want it..so what i did is this:

Character(CharIndex)=LoadAnimMesh("L1_Characters\Troll\Idle_01.b3d")
Troll\ID=Character(CharIndex)
LoadAnimSeq(Troll\ID,"L1_Characters\Troll\Idle_02.b3d")
LoadAnimSeq(Troll\ID,"L1_Characters\Troll\Idle_03.b3d")
LoadAnimSeq(Troll\ID,"L1_Characters\Troll\walk.b3d")
.
.
Troll\Index=CharIndex
..and so on...where CharIndex I increase every time I load new character..

and deleting it as:
For Troll.enemy=Each enemy
If Troll\Dead
DeleteIndex=Troll\Index
Delete Troll
FreeEntity Character(DeleteIndex)
End If
Next


..this works just fine for me..