Recycling of Types

Blitz3D Forums/Blitz3D Beginners Area/Recycling of Types

I have a problem with my approach to some code I am working on:
If KeyDown(Key_M) Then
   For colonycounter = 1 To 5
   colony.Drone = CreateDrone( Rand(30), Rand(30) )
   Next
EndIf


....

Type Drone
	Field xpos#,zpos#,DroneHandle, droneName$, droneState, homeTunnel% ;, Destination
	Field seeky%, speed#, energy%
End Type
;==================================================
Function CreateDrone.Drone(itsx#, itsz#)
  d.Drone = New Drone
  DroneCount = DroneCount + 1
... etc....




I later wish to recycle the drones which have completed a task, but I have had lots of problems if I try to FreeEntity (does this do what it says in the Help - the example doesn't work as described on my system !)

this pseudocode illustrates my approach...

Function UpdateDrones()
   For thisDrone.Drone = Each Drone
   If thisDrone\energy% < 0 Then
       FreeEntity thisDrone\droneHandle
   EndIf
...

End Function


At some points in related code I pivot objects to the drones and then free the pivot when the drone completes the mission. Again I would like to recycle the objects to avoid wasting resources.

At the moment I am using HideEntity, but I expect that as the demo progresses, this will lead to system slowdown. I have also noticed some anomalies in the collision detection when this is the case - some of the drones 'leak' out of the bounding scenery, which I assume is due to 'tweened' updates of the world getting progressively stretched due to processing large numbers of entities.

.... So ....

Is my approach (i.e. creating New drones via a function) the problem, or can I get at the entities and Free them so that I can forget about them ? Or do I need to use a 'droneInUse' flag (within the Type) and work with that within my main UpdateDrones() function ?

Over to you...

well, why don't you just free the entity, then delete the object

FreeEntity thisdrone\dronehandle
delete thisdrone.drone


then create a new one when you need it, or am i missing something?

try this
Graphics3D 800,600
SetBuffer BackBuffer()


light=CreateLight()
eship=CreateSphere()
PositionEntity eship,-30,0,0

camera=CreateCamera()
PositionEntity camera,0,0,-20

Gosub setup

While Not KeyHit(1)

	If KeyDown(200) Then Gosub createnewship
	
	
	
	Gosub updateships
	
	UpdateWorld
	RenderWorld
	Text 0,0," press up key to generate enemy craft"
	Flip
Wend
	
End


.updateships
	For enemy.ship=Each ship
	enemy\y=enemy\y-enemy\speed
	PositionEntity enemy\entity,enemy\x,enemy\y,0
	If enemy\y<-9 Then
						FreeEntity enemy\entity:Delete enemy.ship; free the entity then delete the type object
	End If
	Next
Return



.createnewship
	enemy.ship=New ship
	enemy\entity=CopyEntity(eship); copy the eship entity
	enemy\x=Rnd(-10,10)
	enemy\y=9
	enemy\speed=Rnd(0.1,0.5)
Return

.setup
	Type ship
	Field entity
	Field x,y#
	Field speed#
	End Type
Return


Actually, it was me that was missing something.... the 'delete' command. I'm too used to programming languages which garbage collect for me !!

cheers for the swift response, joker

And now a quick related question... if I use this method and the memory allocated to drones gets fragmented, will BB3D compact the space or internally recycle it at some point ? Or will the resources be lost after all ?

I believe the situation is that when you create a new instance of a type the type list is expanded in memory. However the reverse is not true, when you delete a type no memory is freed thus avoiding any fragmentation problems. (obviously deleted instances would get reused in this scenario when you create new instances).