Help with types please.

Blitz3D Forums/Blitz3D Beginners Area/Help with types please.

Yue(Posted 2012) [#1]
Hello, I have a problem that has me stuck, pass the following code and try to explain it the best way possible.


.Ini

Graphics3D 800,600,32,2
SetBuffer BackBuffer()
SeedRnd MilliSecs ()
Global n#,k=0,w=1.1 ,free,in_mesh,enable=1
Global ocu.Px_Cube
Delete ocu.Px_Cube

pxCreateWorld(1, "key")
pxSetGravity(0, -10, 0)


camera=CreateCamera() 
light=CreateLight()
PositionEntity camera,20,25,-20
RotateEntity camera,30,30,0

plane=CreatePlane( )
EntityColor plane,0,100,200

;--------------Create Trimesh----------------
Text 10,10,"please wait while precomputing pmap..."
Flip
free= LoadMesh("..\media\spring.b3d")
ScaleMesh free,0.2,0.2,0.2
PositionEntity free,0,5,0
   in_mesh=BodyCreateMesh%(free)
   pxBodySetPosition(in_mesh,0,5,0)


		surf = GetSurface(free,1)
		nft = CountTriangles(surf)
		nvv = CountVertices(surf)

While Not KeyDown( 1 )

	
	If KeyHit (200) Exit
	
If KeyHit(57)
  Create_Px_Sphere()
	k =k+1
EndIf  



If KeyHit( 17 )=True Then enable=1-enable 

; Enable/disable wireframe rendering 
WireFrame enable
  
time=MilliSecs ()
pxRenderPhysic(30,0)
Update_Px_Cube()
New_time=MilliSecs ()-time



If KeyDown (17) i=i+4

UpdateWorld ()
RenderWorld
   frames=frames+1  
   If MilliSecs()-render_time=>1000  fps1=frames : frames=0 : render_time=MilliSecs()  
 Text 10,10,"FPS: "+fps1+"  dll time: "+New_time
 Text 10,20,"w -   Enable/disable wireframe rendering   "
 Text 10,30,"space - add sphere  "

Flip 
Wend 

ClearWorld 
pxDestroyWorld()

Goto Ini


Type Px_Cube
	Field cube_body%
	Field cube_mesh%
End Type



 Function Create_Px_Sphere()
   ocu.Px_Cube = New Px_Cube

  ocu\cube_mesh=CreateSphere ()
  ScaleEntity ocu\cube_mesh,0.5,0.5,0.5 
  ocu\cube_body=pxBodyCreateSphere(0.5, 1)

   pxBodySetPosition(ocu\cube_body,3,30,0)

 End Function

Function Update_Px_Cube()
	For ocu.Px_Cube = Each Px_Cube
		pxBodySetEntity(ocu\cube_mesh, ocu\cube_body)
	Next
End Function


Function BodyCreateMesh(mesh%)

	nsurf = CountSurfaces(mesh)
	nvert = 0
	nface=0
	For ns = 1 To nsurf
		Local surf = GetSurface(mesh,ns)
		nface = nface+CountTriangles(surf)
		nvert = nvert +CountVertices(surf)
	Next

	fbank = CreateBank(nface*4*3)
	nf = 0
	vbank = CreateBank(nvert*4*3)
	nv = 0
	For ns = 1 To nsurf
		surf = GetSurface(mesh,ns)
		nfv = CountTriangles(surf)
		For nfc = 0 To nfv -1
			PokeInt fbank,nf*12+0,TriangleVertex(surf,nfc,0)
			PokeInt fbank,nf*12+4,TriangleVertex(surf,nfc,1)
			PokeInt fbank,nf*12+8,TriangleVertex(surf,nfc,2)
			nf=nf+1
		Next

		nvv = CountVertices(surf)
		For nvc = 0 To nvv - 1
			PokeFloat vbank,nv*12+0,VertexX(surf,nvc)
			PokeFloat vbank,nv*12+4,VertexY(surf,nvc)
			PokeFloat vbank,nv*12+8,VertexZ(surf,nvc)
			nv = nv+1
		Next
	Next
	bbb%=pxCreateTriMesh(vbank, fbank, nvert, nface,0)
	FreeBank vbank
	FreeBank fbank
	Return bbb%
End Function



What happens is that when I use the goto after exiting the loop to start again throw me this error code site.

Function Update_Px_Cube()
	For ocu.Px_Cube = Each Px_Cube
		pxBodySetEntity(ocu\cube_mesh, ocu\cube_body); ERROR HERE
	Next
End Function


Help Please.


Yasha(Posted 2012) [#2]
That's because you're not deleting your list of Px_Cube objects after exiting the loop. All of the original Px_Cubes are still there, but- because you used ClearWorld and pxDestroyWorld, the two handles they contain are both invalid and don't point to any new meshes or PX entities.

At the absolute minimum, you should be doing:

Delete Each Px_Cube


...as part of your cleanup code. Or better, write a proper cleanup function for each Px_Cube object, and loop over the whole list before running ClearWorld and pxDestroyWorld.

As a general guideline, you should never have to use functions like ClearWorld or pxDestroyWorld: if you're cleaning up your assets properly, there should be nothing left for them to catch. ClearWorld especially is of really questionable usefulness because as this example demonstrates, it only kills 3D engine objects and leaves logical objects alone, regardless of internal references, which will usually leave your game system in a mess.

You also don't normally need to destroy the whole physics environment, since the physics engine is separate from the graphics system and will happily survive a graphics mode switch untouched.


Yue(Posted 2012) [#3]
Ho! thank you very much.

Function ClearBodies()
	For R.Ragdoll=Each Ragdoll
		For x = 0 To 100
			pxDeleteBody R\Bodies[X]
		Next
		
		
		Delete R.Ragdoll
	Next
End Function