Iterate trough all entities

Blitz3D Forums/Blitz3D Programming/Iterate trough all entities

jfk EO-11110(Posted 2007) [#1]
There was a release by Halo some time ago that allowed to iterate trough all loaded entities. Was that based on a plain userlib or was a dll used? And is this thing still around somewhere.

EDIT: found it:
http://www.blitzbasic.com/Community/posts.php?topic=28768#303347
As expected, the memory dll has gone. But I hope it's possible to do the same with a simple decls call of RTLMemoryMove? As far as I see:
PeekL(CURRENTENTITYPOINTER+BLITZ_NEXT_ENTITY)
is the only call of an external function?

(BTW this also allows EntityExist etc., so it may be useful for all of us)


jfk EO-11110(Posted 2007) [#2]
Ok, done, works:

; This code will automaticly iterate trough all entities. Based on the original idea and code by
; Halo, this one works with a simple "decls" userlib.


; Userlib declaration: RTLMoveMemory2 from the kernel32.dll is used,
; you need at least the following 2 lines in your kernel32.decls:

; .lib "kernel32.dll" 
; RtlMoveMemory2%(Destination*,Source,Length) : "RtlMoveMemory"


; Note: The pivot that is created in the init section must be the first entity
; that was created or loaded. You may have to recreate it after a "ClearWorld()"


Graphics3D 800,600,32,2
SetBuffer BackBuffer()

camera=CreateCamera()



; init Cycles entity iteration------------------------
Global Cycle_bank=CreateBank(16)
Const  Cycle_NextEntity=4
Const  Cycle_LastEntity=8
Global Cycle_FirstEntity=CreatePivot()
Global Cycle_CurrentEntityPointer=Cycle_FirstEntity
;----------------------------------------------------




; create some test entities
For i=0 To 7
 dudu=CreateCube()
 NameEntity dudu,Chr$(Rand(65,90))+""+Rand(1000)
Next



; how to cycle trough all entities:
While MoreEntities()
 entity= NextEntity()
 Print "Handle: " + entity
 Print "Name:   " + EntityName$(entity)
 Print "Class:  " + EntityClass$(entity)
 Print "-------------------------------"
Wend



; How to use EntityExists():

;freddy=CreatePivot() ; try unrem
Print
Print EntityExists(freddy)

WaitKey()
End






Function MoreEntities() ; check if there are further entities
 RtlMoveMemory2(Cycle_bank,Cycle_CurrentEntityPointer+Cycle_NextEntity,4)
 If PeekInt(Cycle_bank,0)<>0
  Return True
 Else
  Cycle_CurrentEntityPointer=Cycle_FirstEntity
 EndIf
End Function


Function NextEntity()
 Local entity
 RtlMoveMemory2(Cycle_bank,Cycle_CurrentEntityPointer+Cycle_NextEntity,4)
 entity=PeekInt(Cycle_bank,0)
 Cycle_CurrentEntityPointer = entity
 Return entity
End Function


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




Mikele(Posted 2007) [#3]
I like "dudu" entities ;).
Thanks for a code.


slenkar(Posted 2007) [#4]
thanks,

I wonder if its possible to use something like this to get rid of all loaded images,without having to close and re-open the window


jfk EO-11110(Posted 2007) [#5]
You can try it simply use an image instead of a pivot for the FirstEntity. I wouldn't be surprised if it works. Tho, images ain't entities, and using for example EntityClass with an image handle will MAV.


Naughty Alien(Posted 2007) [#6]
..thanks jfk...I'm going to use this....very handy..