An issue with loadAnimMesh or freeEntity?

Blitz3D Forums/Blitz3D Programming/An issue with loadAnimMesh or freeEntity?

dena(Posted 2009) [#1]
I'm not sure exactly where the issue is stemming from, so I'll try and explain my situation best I can and provide the chunks of code involved with the issue.

I am attempting to build in a game-restart function into my project, where a "restart" choice is chosen from a list while the game is paused, and it returns you to the title screen. The error I'm receiving when it attempts to re-load the game after returning to the title seems to be with LoadAnimMesh -- essentially, I am clearing the world/freeing all entities, pushing the user back to the beginning of the program and beginning again, which includes running a world-load function that rebuilds collision objects, environment objects and so forth. When it gets to objects tied to an animated texture, it claims the entity does not exist, despite it working the first time the game is loaded using the exact same function.

(note: I realize that there are definitely more efficient ways of loading in information for my map from files -- Unfortunately, my other programmer and I under a really tight deadline and don't have the time to rework the system)

Here's the process that it goes through:

Step 1 - load the map from file/s:
Function loadMap()

	If restart = 0
		collCollection.collObj = First collObj
		envCollection.envObj = First envObj
		objCollection.objObj = First objObj
		For collCollection.collObj = Each collObj
			FreeEntity collCollection\mesh
			Delete collCollection.collObj
		Next
		For envCollection.envObj = Each envObj
			FreeEntity envCollection\mesh
			Delete envCollection.envObj
		Next
		For objCollection.objObj = Each objObj
			FreeEntity objCollection\mesh
			FreeEntity objCollection\sprite
			Delete objCollection.objObj
		Next
	EndIf

	fileIn = ReadFile("data\maps\hildurColl.dat")
	
	While Not Eof(fileIn)
		collCollection.collObj = New collObj
		collCollection\X = ReadInt(fileIn)
		collCollection\Y = ReadInt(fileIn)
		collCollection\Z = ReadInt(fileIn)
		collCollection\objType = ReadInt(fileIn)
		checkCollType()
		PositionEntity collCollection\mesh, collCollection\x, collCollection\y, 0
	Wend
	
	fileIn = ReadFile("data\maps\hildurEnv.dat")
	
	While Not Eof(fileIn)
		envCollection.envObj = New envObj
		envCollection\X = ReadInt(fileIn)
		envCollection\Y = ReadInt(fileIn)
		envCollection\Z = ReadInt(fileIn)
		envCollection\objType = ReadInt(fileIn)
		checkenvType()
		envCollection\alpha = ReadInt(fileIn)
		envCollection\layer = ReadInt(fileIn)
		PositionEntity envCollection\mesh, envCollection\x, envCollection\y, 0
		PositionEntity envCollection\mesh2, envCollection\x, envCollection\y, 0
	Wend
	
	fileIn = ReadFile("data\maps\hildurObj.dat")
	
	While Not Eof(fileIn)
		objCollection.objObj = New objObj
		objCollection\X = ReadInt(fileIn)
		objCollection\Y = ReadInt(fileIn)
		objCollection\Z = ReadInt(fileIn)
		objCollection\objType = ReadInt(fileIn)
		checkobjType()
		objCollection\alpha = ReadInt(fileIn)
		PositionEntity objCollection\sprite, objCollection\x, objCollection\y, objCollection\z
	Wend
	
End Function


Step 2 - during pause screen, restart to title screen
	If KeyHit(p_k_action)
		If pauseOption = 0
			gameMode = 1
			EntityAlpha pause_sprite, 0
			EntityOrder pause_sprite, -100
			FreeEntity  pause_sprite
		ElseIf pauseOption = 1 ; <-- this is the restart option
			restart = 1
			quit = 1
			gameMode = 1 ; <-- 1 = gameplay 2 = worldbuild 3 = pause
			done = False
			EntityAlpha pause_sprite, 0
			EntityOrder pause_sprite, -100
			FreeEntity  pause_sprite
			ClearWorld
		ElseIf pauseOption = 2
			quit = 1
			gameMode = 1
			EntityAlpha pause_sprite, 0
			EntityOrder pause_sprite, -100
			FreeEntity  pause_sprite
		EndIf
	EndIf	
		
End Function


Step 3 - re-run the loadMap code


Within the mainLoop, a function called animateObjects is run to re-draw the animated textures based on millisecs(), yadda yadda, and that's where I'm receiving the error upon restart. as of this post, there's only a single object type with an animated mesh built in, but there are several that use the exact same process.

Function animateObjects()

	objCollection.objObj = First objObj
	
	For objCollection.objObj = Each objObj
		If objCollection\objType = 6
			frame = MilliSecs()/100 Mod 4
			EntityTexture objCollection\sprite, objCollection\texture, frame
		EndIf
	Next
	
End Function


The last function that plays into this is a function that checks which type of object to create upon map load (checkObjType(), for example), and physically creates the object in the world.

Function checkObjType()

	objCollection.objObj = Last objObj
	objCollection\sprite = CreateSprite()

	If objCollection\objType = 1
		objCollection\texture = LoadTexture("Data\Sprites\rainbowR.bmp", 51)
		objCollection\mesh = CreateCube(objCollection\sprite)
		EntityTexture objCollection\sprite, objCollection\texture
		MoveEntity objCollection\mesh, -3, -4.5, 0
	ElseIf objCollection\objType = 2
		objCollection\texture = LoadTexture("Data\Sprites\rainbowL.bmp", 51)
		objCollection\mesh = CreateCube(objCollection\sprite)
		EntityTexture objCollection\sprite, objCollection\texture
		MoveEntity objCollection\mesh, 3, -4.5, 0
	ElseIf objCollection\objType = 3
		objCollection\texture = LoadTexture("Data\Textures\gustTest.bmp", 51)
		objCollection\mesh = CreateCube(objCollection\sprite)
		EntityTexture objCollection\sprite, objCollection\texture
		ScaleEntity objCollection\mesh, 2, 2, 2
	ElseIf objCollection\objType = 4
		objCollection\texture = LoadTexture("Data\Textures\gustTest2.bmp", 51)
		objCollection\mesh = CreateCube(objCollection\sprite)
		EntityTexture objCollection\sprite, objCollection\texture
		ScaleEntity objCollection\mesh, 2, 2, 2
	ElseIf objCollection\objType = 5
		objCollection\texture = LoadTexture("Data\sprites\vent.bmp", 51)
		objCollection\mesh = CreateCube(objCollection\sprite)
		EntityTexture objCollection\sprite, objCollection\texture
		ScaleEntity objCollection\mesh, 2, 18, 2
		MoveEntity objCollection\mesh, 0, 20, 0
		MoveEntity objCollection\sprite, 0, 0, 1.9
	ElseIf objCollection\objType = 6
		objCollection\texture = LoadAnimTexture("Data\sprites\brazier_bw_anim.bmp", 51, 256, 256, 0, 4)
		objCollection\mesh = CreateCube(objCollection\sprite)
		frame = MilliSecs()/100 Mod 4
		EntityTexture objCollection\sprite, objCollection\texture, frame
	ElseIf objCollection\objType = 7
		objCollection\texture = LoadTexture("Data\sprites\iceWall.bmp", 51)
		objCollection\mesh = CreateCube(objCollection\sprite)
		EntityTexture objCollection\sprite, objCollection\texture
		MoveEntity objCollection\mesh, 0, 0, 0
		ScaleEntity objCollection\mesh, 2, 6, 2
		EntityType objCollection\mesh, C_SCENERY
	ElseIf objCollection\objType = 8
		objCollection\texture = LoadTexture("Data\sprites\dye_platform.bmp", 51)
		objCollection\mesh = CreateMesh(objCollection\sprite)
		s = CreateSurface (objCollection\mesh)
		AddVertex s, -6, 2, -1 : AddVertex s, +6, 2, -1
		AddVertex s, -6, 2, 1 : AddVertex s, +6, 2, 1
		AddTriangle s, 2, 1, 0 : AddTriangle s, 2, 3, 1
		EntityTexture objCollection\sprite, objCollection\texture
		EntityType objCollection\mesh, C_SCENERY
	ElseIf objCollection\objType = 9
		objCollection\texture = LoadTexture("Data\sprites\geyser.bmp", 51)
		objCollection\mesh = CreateCube(objCollection\sprite)
		EntityTexture objCollection\sprite, objCollection\texture
		MoveEntity objCollection\mesh, 0, 3.5, 0
		ScaleEntity objCollection\mesh, 1.5, 1.5, 1.5
	EndIf
	
	If objCollection\objType = 3 Or objCollection\objType = 4
		ScaleSprite objCollection\sprite, 2, 2
	ElseIf objCollection\objType >= 5 Or objCollection\objType = 1 Or objCollection\objType = 2
		ScaleSprite objCollection\sprite, 6, 6
	EndIf
	
	EntityAlpha objCollection\sprite, 1
	EntityAlpha objCollection\mesh, 0
	EntityColor objCollection\mesh, 80, 80, 255
	EntityFX objCollection\sprite, 1
	EntityOrder objCollection\sprite, 2
	
	totalobjObj = totalobjObj + 1


I'm completely stumped as to why it is not properly reloading the objects, however I'm afraid I'm just missing something very important when the objects are being created that is throwing this entire process out of whack. My hope is that someone else here can catch my mistake -- Thank you in advance!

- Dena


FlagDKT(Posted 2009) [#2]
Maybe you just have to closeFile(filein) after reading.(however you should do it)

and you don't need those:
		collCollection.collObj = First collObj
		envCollection.envObj = First envObj
		objCollection.objObj = First objObj
etc..
as for each loop will work anyway.

and you should modify the checkObjType() in: checkObjType(objToCheck.objObj)
Now use objToCheck instead of Last objObj.

before entityTexture put (compile in debug mode and watch the debug log):
debuglog objCollection\sprite
debuglog objCollection\texture
If they are zero you didn't load them.

And you have to free the textures too with: freeTexture().


dena(Posted 2009) [#3]
Wow, I had no idea about the debuglog commands. That should definitely help me out, thanks!

Doesn't ClearWorld free all of the textures?


FlagDKT(Posted 2009) [#4]
Yes...I didn't notice you used that :)