Error while deleting an object

Blitz3D Forums/Blitz3D Programming/Error while deleting an object

Yue(Posted 2014) [#1]
Function Init_ObjPrimitiva.Primitiva(TipoPrimitiva%)
	
	
	
	
	
	
	Local objPrimitiva.Primitiva = New Primitiva
	
	Select TipoPrimitiva%
			
		Case 1
			objPrimitiva.Primitiva\Figura% = CreateCube()
	End Select 
	
	
	
	
	
			
	
	
	Return objPrimitiva.Primitiva 
	
	
	
End Function

; - Se eliminan todos los osbjetos del Tipo Primitiva
Function Delete_ObjPrimitiva()
	
	
	
	Local Objeto.Primitiva = New Primitiva
	
	
	
	For Objeto.Primitiva = Each Primitiva
		
		FreeEntity Objeto\Figura%
		Delete Objeto.Primitiva 
		
	Next 
	
	
	
End Function 



; - Se crea un objeto Quad.
Function Init_ObjQuad.Quad(Textura%=0)
	
	
	Local Rectangulo.Quad = New Quad
	
	Rectangulo.Quad\Rec% = Quad(Textura%) 
	
	
	Return Rectangulo.Quad
	
	
End Function 

; - Se eliminan todos los Objetos del tipo Quad.
Function Delete_ObjQuad%()
	
	Local Objeto.Quad = New Quad
	For Objeto.Quad = Each Quad
		
		FreeEntity Objeto\Rec%
		Delete Objeto.Quad
		
	Next 
	
End Function 

 



At the end use the following program.

Delete_ObjQuad%()
Delete_ObjPrimitiva%()



The problem is that tells me that the entity does not exist to be deleted, if I leave a single function, no matter whether the prmitiva eliminate or quad, does not happen.

What can it be?, Am I doing wrong?


stayne(Posted 2014) [#2]
Why don't you just check to see if the entity exists first before deleting it? Probably bad practice...which is why I don't program anymore :)

For Objeto.Quad = Each Quad
    If Objeto\Rec% > 0
	FreeEntity Objeto\Rec%
	Delete Objeto.Quad
    EndIf
Next



Guy Fawkes(Posted 2014) [#3]


Never use "> 0" unless it's for a uniform integer. It's just bad practice. Instead, remove "> 0" and just say "If Objeto\Rec%".

Sincerely,

~GF


Stevie G(Posted 2014) [#4]
Both examples above are bad practice as you are simply masking the problem.

In all your delete functions you are creating a new object by using the new statement. You shouldn't do this as you're adding to the Objeto list. As you haven't assigned and entity to this new Objeto instance, as soon as you then iterate through the instances it will cause this 'entity does not exist' error.

For example ...

Function Delete_ObjPrimitiva()
	
	Local Objeto.Primitiva = New Primitiva
	
	For Objeto.Primitiva = Each Primitiva
		FreeEntity Objeto\Figura%
		Delete Objeto.Primitiva 
	Next 

End Function 


Should be ...

Function Delete_ObjPrimitiva()

	Local Objeto.Primitiva
	
	For Objeto.Primitiva = Each Primitiva
		FreeEntity Objeto\Figura%
		Delete Objeto.Primitiva 
	Next 
	
End Function 


Stevie


Guy Fawkes(Posted 2014) [#5]
^ Good job, @Stevie. You beat me to it. I was half asleep last night so I didn't realize I forgot the "Local Objeto.Primitiva" line. -.-