Cycle through all your entities without types or other structures.

Blitz3D Forums/Blitz3D Programming/Cycle through all your entities without types or other structures.

JoshK(Posted 2003) [#1]
This requires the memory lib from www.leadwerks.com/code.

With this and the entity parameters commands, you don't even have to store your entities in any kind of type or array structure.

These commands will cycle through every entity that exists, even if you "forget" about it:
MoreEntities()
NextEntity()

Graphics3D 400,300,16,2

Include "EntityCycle.bb"

For n=1 To 5
	e=CreatePivot()
	NameEntity e,"Entity Number "+n
	Next

While MoreEntities()
	Print EntityName(NextEntity())
	Wend

Print ""

While MoreEntities()
	Print EntityName(NextEntity())
	Wend

WaitKey
End


EntityCycle.bb:
Const BLITZ_NEXT_ENTITY=4
Const BLITZ_LAST_ENTITY=8

Global FIRSTENTITY=CreatePivot()
Global CURRENTENTITYPOINTER=FIRSTENTITY

Function MoreEntities()
If PeekL(CURRENTENTITYPOINTER+BLITZ_NEXT_ENTITY)
	Return True
	Else
	CURRENTENTITYPOINTER=FIRSTENTITY
	EndIf
End Function

Function NextEntity()
entity=PeekL(CURRENTENTITYPOINTER+BLITZ_NEXT_ENTITY)
CURRENTENTITYPOINTER=entity
Return entity
End Function



JoshK(Posted 2003) [#2]
Oh, and EntityExists() IS possible:

Function EntityExists(entity)
While MoreEntities()
	If NextEntity()=entity Return True
	Wend
End Function



simonh(Posted 2003) [#3]
Nice work.


Beaker(Posted 2003) [#4]
All looks useful. Might be complimented by this:
http://www.blitzbasic.com/codearcs/codearcs.php?code=796


Marcelo(Posted 2003) [#5]
Another idea is to add all your entities to a "world" global pivot and use CountChildren() and GetChild(), this way by deleting or hiding your world pivot, you propagate the command to all the entities.


JoshK(Posted 2003) [#6]
My main loop just went from this:
For player.player=each player : updateplayer player : Next
For flare.flare=each flare : updateflare flare : Next
For trigger_hide.trigger_hide=each trigger_hide : updatetrigger_hide trigger_hide : Next
For trigger_show.trigger_show=each trigger_show : updatetrigger_show trigger_show : Next


To this:
UpdateEntities CLASS_PLAYER
UpdateEntities CLASS_FLARE
UpdateEntities CLASS_TRIGGER_HIDE
UpdateEntities CLASS_TRIGGER_SHOW


There's other ways you could have done this, but accessing the entities directly, and storing data in the entity structure, as opposed to storing the entity somewhere in a data structure, are by far the best/easiest/most versatile methods. This allows a totally different entity-oriented coding style, where all you need to access all parameters is the entity variable.

This is REALLY cool! It's like a new language.


RifRaf(Posted 2003) [#7]
Is it any faster? and if you cyle through just the entity data, how can you access its type info that goes with it.. like powerups, gold, health all that stuff that could be associated with the entity?


JoshK(Posted 2003) [#8]
Whether or not it's slightly faster or slower, you don't worry about speeds for cycling through data when you have much more intensive processes like collision and rendering. I don't know if this gains or loses 2 milliseconds, but it's not really going to make any difference. I'm guessing it would be slightly faster than cycling through all your types looking for the right entity.

Check out the thread called "Add a free four-byte slot..." for info on getting and setting entity properties.


RifRaf(Posted 2003) [#9]
wow. cool stuff


SopiSoft(Posted 2003) [#10]
yeah this is pretty cool ;-)