trouble withFreeEntity

Blitz3D Forums/Blitz3D Programming/trouble withFreeEntity

dena(Posted 2005) [#1]
I am trying to load mesh "a" at a certain time of day, and at a later time, unload mesh "a" and load mesh "b", etc etc.
Everytime I use FreeEntity in the context below, I get a message telling me that the entity does not exist, but it is loaded into memory. What am I missing?
thanks
Dena
++++++++++++++++++++++++++++++++++++++++++
While Not KeyHit(1)



If MilliSecs()- Time < 7000 Then
Status$="Night"
carbon=LoadMesh("meshes/carbon"+Rand(1,4)+".x")
ElseIf MilliSecs() - Time < 19000 Then
Status$="Day"
If Status$="Day" Then
FreeEntity carbon
Nitrogen=LoadMesh("meshes/nitrogen"+Rand(1,4)+".x")
EndIf
Else
Status$="Night"
If Status$="Night" Then
FreeEntity nitrogen2
carbon=LoadMesh("meshes/carbon"+Rand(1,4)+".x")
EndIf
End If

If MilliSecs() - Time > 24000 Then
Time = Time + 24000
End If

+++++++++++++++++++++++++++++++++++++++


Matty(Posted 2005) [#2]
freeentity nitrogen2 - you never load a mesh which you assign a handle to nitrogen2

Also I think what you are doing is really bad - loading meshes in part of your main loop will slow down the program as it has to access the hard drive. Also, in the example of "millisecs()-time<7000" - you are loading a mesh every frame and overwriting the value stored in carbon which is going to chew up resources pretty quickly.


Graythe(Posted 2005) [#3]
Hi,

I think it should look more like...

Time=MilliSecs()

Carbon=LoadMesh("meshes/carbon"+Rand(1,4)+".x")
Nitrogen=LoadMesh("meshes/nitrogen"+Rand(1,4)+".x")

;Start main loop

While Not KeyHit(1)

	;Code fragment
	
	Elapsed=MilliSecs()-Time
	
	If Elapsed < 7000 Then
		If not MeshHandle=Carbon Then
			Status$="Night"
			If MeshHandle then HideEntity MeshHandle
                        MeshHandle=Carbon
		End If
	Else
		If Not MeshHandle=Nitrogen then
			Status$="Day"
                        If MeshHandle then HideEntity MeshHandle
			MeshHandle=Nitrogen
		End If	
		If Elapsed>24000 Then
			Elapsed=MilliSecs()
		End If
	End If



;Wend




Oops. Some hasty editing.... cough.


Paolo(Posted 2005) [#4]
For more stable results,
every time you free an entity that has a direct relation
to a variable, it is better to make the variable equal to cero:

if mesh_A=0
mesh_A=LoadMesh()
endif

if mesh_A>0
FreeEntity mesh_A : mesh_A=0
endif



dena(Posted 2005) [#5]
Matty:
sorry, nitrogen2 is a typo.
I see what you mean, it is really bad...but I need to get a random reload/replace of the geometry every 20 minutes or so. This will be a projected animation in an art gallery; so it will run continually for 6 hours a day, so the inefficiency you mention would be a huge issue, good call.
I think that if I preload and use hideentity/showentity, then I won't get a "real" random update?


Graythe:

I don't think I want to HideEntity because I want to, every 20 minutes or so, replace geometry by randomly loading a new one. If I preload and use hideentity, then how will I get the random update? This will be a projected animation in an art gallery; so it will run continually for 6 hours a day.

Eurythmia:
I like this, but do I do this if-then from within the main loop?


Graythe(Posted 2005) [#6]
Oops - I see your point and I should have seen it from the code. Then it should look something like this...

Time=MilliSecs()

Dim CarbonArray(4)
Dim NitrogenArray(4)
	
Carbon=LoadMesh("meshes/carbon"+Rand(1,4)+".x")
Nitrogen=LoadMesh("meshes/nitrogen"+Rand(1,4)+".x")

For LoadLoop=1 To 4
	CarbonArray(LoadLoop)=LoadMesh("meshes/carbon"+LoadLoop+".x")
	NitrogenArray(LoadLoop)=LoadMesh("meshes/nitrogen"+LoadLoop+".x")
Next

;Start main loop

While Not KeyHit(1)

	;Code fragment
	
	Elapsed=MilliSecs()-Time
	
	If Elapsed < 7000 Then
		If Not MeshHandle=Carbon Then
			Status$="Night"
			If MeshHandle Then HideEntity MeshHandle
            MeshHandle=CarbonArray(Rnd(1,4))
			
		End If
	Else
		If Not MeshHandle=Nitrogen Then
			Status$="Day"
            If MeshHandle Then HideEntity MeshHandle
			MeshHandle=NitrogenArray(Rnd(1,4))
		End If	
		If Elapsed>24000 Then
			Elapsed=MilliSecs()
		End If
	End If



;Wend



dena(Posted 2005) [#7]
Thanks a lot Graythe, I will have a look at this. I was chasing after something using "Select" and "Case", but using an array looks like it might be a better idea. I will post it when I get something to work.


dena(Posted 2005) [#8]
Graythe:
Below is what I have so far. It initially loads one instance of nitogen and carbon, and then at 7000 (hour 7), it loads another pair. I would expect that from the code it would load a third instance at 19000 (hour 19), but it does not. Also not sure if previous instances are being unloaded from memory, or just hidden.
My ultimate goal is to have four arrays of geometry, with one instance from each on screen all the time, and have them reload randomly at different intervals.
like:

carbonarray(1,4) reloads every 5 minutes
nitrogenarray(1,4) reloads every 10 minutes
phosphorusarray(1,4) reloads every 15 minutes
oxygenarray(1,4) reloads every 20 minutes

since this will be running for 6 hours a day, I will need to make sure that the previous geometry files are unloaded as I go.

Is this the best way to go about this? I have been looking through the forums, trying to understand arrays and select/cases. I am not understanding how to load and unload the geometry outside the main loop.....

HELP please!!!!!!
dena

++++++++++++++++++++++++++++++++++++++++
Graphics3D 640,480
SetBuffer BackBuffer()

Global Time = MilliSecs()

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

light=CreateLight (2)

SeedRnd (MilliSecs())

Dim CarbonArray(4)
Dim NitrogenArray(4)

Carbon=LoadMesh("meshes/carbon"+Rand(1,4)+".x")
Nitrogen=LoadMesh("meshes/nitrogen"+Rand(1,4)+".x")
EntityAlpha Nitrogen, .5

For LoadLoop=1 To 4
CarbonArray(LoadLoop)=LoadMesh("meshes/carbon"+LoadLoop+".x")
NitrogenArray(LoadLoop)=LoadMesh("meshes/nitrogen"+LoadLoop+".x")
Next

;Start main loop

While Not KeyHit(1)

;Code fragment

Elapsed=MilliSecs()-Time

If Elapsed < 7000 And Elapsed > 19000 Then
Status$="Night"
If Not MeshHandle=Carbon Then
If MeshHandle Then HideEntity MeshHandle
MeshHandle=CarbonArray(Rnd(1,4))
End If
; Else
ElseIf Elapsed > 7000 And Elapsed < 19000 Then ; 19000 means 19:00 hours
Status$="Day"
If Not MeshHandle=Nitrogen Then
If MeshHandle Then HideEntity MeshHandle
MeshHandle=NitrogenArray(Rnd(1,4))
End If
Else ; the else is because it isn't day time, so it must be night time
Status$="Night"
End If

If Elapsed > 24000 Then ; check to see if 24 hours has past, and if so, start a new day.
Time = Time + 24000 ; wrap the day back round to midnight again
End If

RenderWorld

Text 10,10, " Day or Night:"+status$
Text 10,20," Hour:"+(Elapsed/1000)

Flip

Wend
End
++++++++++++++++++++++++++++++++++++++