freeing children before parent

Blitz3D Forums/Blitz3D Programming/freeing children before parent

slenkar(Posted 2004) [#1]
Hi I need to access the children of an entity and free them before I free the entity itself so I can make sure no-one is chasing any of the children,

here is my function which calls itself:
Function set_childs_target_to_zero(ship)

For s.ship=Each ship
If s\turret_attached=ship
set_childs_target_to_zero(s\ship_handle)
Set_Target_To_Zero(s\ship_handle)
FreeEntity s\ship_handle
Delete s
EndIf
Next

For s.ship=Each ship
If s\parent=ship
set_childs_target_to_zero(s\ship_handle)
Set_Target_To_Zero(s\ship_handle)
FreeEntity s\ship_handle
Delete s
EndIf
Next

End Function


it works o.k. for entities that have turrets attached but not for other children that use the s\parent field.


Stevie G(Posted 2004) [#2]
Slenkar,

I'm assuming that s\turret_attached, s\ship_handle and s\parent are pointers to actual entities.

Is the function Set_Target_To_Zero() simply zeroising the variable s\ship_handle? If so then your zeroising the pointer to the entity and they trying to free an entity (which exists) but you have no pointer to. Did that make sense?

Try putting the freeentity s\ship_handle before the call to Set_Target_To_Zero.


slenkar(Posted 2004) [#3]
yes they are all pointers,

set_target_to_zero() sets all the pointers to zero that point to the ship that is being freed, so that no-ones's target is the ship that doesnt exist,

so its
function set_target_to_zero(ship)
for s.ship=each ship
if s\target_handle=ship
s\target_handle=0
endif
next
end function