Freeentity / Copyentity - is they slow?

Blitz3D Forums/Blitz3D Programming/Freeentity / Copyentity - is they slow?

Vorderman(Posted 2005) [#1]
I'm using Freeentity to remove sections of a racetrack as the car passes, then Copyentity to copy from a pre-loaded store to a new entity which is then placed down.

However, even with a simple set of 10 track pieces and around 3mb of textures I'm getting severe stuttering the further you get down the track, as if there is some buildup that gets worse and worse the more times you use the Free and Copy commands - perhaps there's a delay bewteen Freeing the entity and the memory being freed?

Has anyone else found stuttering problems using these commands regularly within their main loop (not every frame, but perhaps once or twice a second)?


Damien Sturdy(Posted 2005) [#2]
Den Bros 4 suffers from this, and everything is created in it using copyentity and freeentity (to keep things in and out of view)

Good find, thought it was me doing something wrong before...

Let me know if you manage to fix it :D

Anyone got any tips?


RiverRatt(Posted 2005) [#3]
Are you using types for the entities data? The reason I ask is becouse you most likly need to delete the types for those at the same time.
I have a space game that has hundreds of ships and bullets (all meshes) and I don't see any slowdown when deleting them.


Rob Farley(Posted 2005) [#4]
Why not just hide/show?


HappyCat(Posted 2005) [#5]
I'm using this kind of method and I was having the same kind of stuttering problems (started off okay, but eventually got a very noticable stutter every second or so).

I fixed it by nothing more hi-tech than trawling through all my code where I was copying and deleting entities and making absolutely sure that I was freeing everything when I needed to (types, entities, textures). In my case slightly sloppy code (not freeing dynamically created textures properly) was causing my video memory useage to continually increase until it was full - fixing that got rid of the stuttering.


fall_x(Posted 2005) [#6]
I personally wouldn't free the entities all the time. Just set it all up once, and set up a grid of "occlusion area's", which are basically pivots. Then you parent your entity's to them upon creation, and you can easily hide/show occlusion areas's (and their child entities) on the fly. This will speed up your game because collision detection is not done on entities that are hidden - otherwise, the engine will have to check wheter your car collided with all entities.
Also set up CameraRange so the engine doesn't have to draw things that are very far away.

I did this like this (these are just code snippets from troughout my game, you'll have to read them and modify them and put them in the right places if you want to use it) :

global OcclusionAreas(0,0)

type OcclusionArea
	field entity%
	field x%
	field z%
end type

function GetOcclusionArea.OcclusionArea(x,z)
	for o.OcclusionArea = each OcclusionArea
		if o\x=x and o\z=z then
			return o
		end if
	next
end function


; gVar_Width% is the width of my current level, I have an occlusion area every 32 units.

dim OcclusionAreas(gVar_Width%/32,gVar_Width%/32)

	for i=0 to gVar_Width%/32
		for j=0 to gVar_Width%/32
			o.OcclusionArea=new OcclusionArea
			o\entity=createpivot()
			hideentity o\entity
			o\x=i
			o\z=j
			positionentity o\entity,i*32,0,j*32
			OcclusionAreas(i,j)=o
		next
	next


; this is to parent an entity to the pivots
					o.OcclusionArea=OcclusionAreas(your_entity_x/32,your_entity_z/32)
					entityparent your_entity_here,o\entity


Now, for every player (or race car in your case), you'll need a field to store the nearest occlusion area of the previous frame. Then you can easily call this code every frame (it first hides the player's previous occlusion area and it's surrounding area's, and then it show the current area and it's surrounding area's - which may be the same as the previous one off course.
My game has multiple players (all controlable by the player, but they can be on different locations far from eachother), so they still need collision detection with everything, that's why I loop trough them and show/hide the occlusion area's for all of them.

	for pl.player=each player
		if not pl\prevArea=null then
			for xc=pl\prevArea\x-1 to pl\prevArea\x+1
				for zc=pl\prevArea\z-1 to pl\prevArea\z+1
					if xc>0 and zc>0 and xc<(gVar_Width/32) and zc<(gVar_height/32)
						o.occlusionarea=occlusionAreas(xc,zc)
						hideentity o\entity
					end if
				next
			next
		end if
	next
	
	for pl.player=each player
		currentArea.OcclusionArea=occlusionAreas(entityx(pl\entity)/32,entityz(pl\entity)/32)
		if not currentArea=null then
			for xc=currentArea\x-1 to currentArea\x+1
				for zc=currentArea\z-1 to currentArea\z+1
					if xc>0 and zc>0 and xc<(gVar_Width/32) and zc<(gVar_height/32)
						o.occlusionarea=occlusionAreas(xc,zc)
						showentity o\entity
					end if
				next
			next
		end if
	next



I hope this is helpful, it gave my game a speed-increase of about 20-30 fps, and should work better than using freeentity/copyentity all the time. Only if you have a reeealy big level, you may not want to load it all at the same time and then you might need to free and copy the entities, but that can be done using a same area-principle.

BTW, if anyone thinks I'm doing this wrong, don't hesitate to tell me :) I've only been using blitz for the past 2/3 months.
Oh, and there is no uppercasing in my code, because apparantly my editor displays all the functions and keywords with right casing, but when I copy-paste it, it's all lowercase.


Vorderman(Posted 2005) [#7]
I am using types, but what I have is a single instance of a TRACK type, with a subfield array of segments. Each segment is an entity (section of track mesh) and the mesh is freed and then the segment is assigned a new mesh.

So I can't really free up any types, all I can do is free the mesh. I don;t think I'm missing freeing anything up as nothing else bar the mesh is loaded.


fall_x(Posted 2005) [#8]
I don't understand why you would want to free them instead of hiding them, unless it's a very large track and you want to preserve memory.


Vorderman(Posted 2005) [#9]
It's a constantly updating system, meaning that the only objects present are the track section the car is on, and about 3 in front and a couple behind. These 6 segment objects are recycled as the car moves along, allowing me to have a track of effectively infinite size.

However, it doesn't seem to work on one of my test PCs, so I am investigating what happens if all segments are present by alphaed out to zero to hide them. Apparently Hideentity is slower than setting the alpha to zero.


Yan(Posted 2005) [#10]
Apparently Hideentity is slower than setting the alpha to zero.

It used to be, but Mark updated the the entity system (v1.81) to make hide/show entity 'instantaneous'.