November 25, 2020, 05:40:29 AM

Author Topic: [bb] EntityExists(), iterate entities by jfk EO-11110 [ 1+ years ago ]  (Read 750 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : EntityExists(), iterate entities
Author : jfk EO-11110
Posted : 1+ years ago

Description : Nothing to say, useful stuff.

Code :
Code: BlitzBasic
  1. [code]
  2. ; This code will automaticly iterate trough all entities. Based on an idea by
  3. ; Halo, this one works with a simple "decls" userlib.
  4.  
  5.  
  6. ; Userlib declaration: RTLMoveMemory2 from the kernel32.dll is used,
  7. ; you need at least the following 2 lines in your kernel32.decls:
  8.  
  9. ; .lib "kernel32.dll"
  10. ; RtlMoveMemory2%(Destination*,Source,Length) : "RtlMoveMemory"
  11.  
  12.  
  13. ; Note: The pivot that is created in the init section must be the first entity
  14. ; that was created or loaded. You may have to recreate it after a "ClearWorld()"
  15.  
  16.  
  17. Graphics3D 800,600,32,2
  18. SetBuffer BackBuffer()
  19.  
  20. camera=CreateCamera()
  21.  
  22.  
  23.  
  24. ; init Cycles entity iteration------------------------
  25. Global Cycle_bank=CreateBank(16)
  26. Const  Cycle_NextEntity=4
  27. Const  Cycle_LastEntity=8
  28. Global Cycle_FirstEntity=CreatePivot()
  29. Global Cycle_CurrentEntityPointer=Cycle_FirstEntity
  30. ;----------------------------------------------------
  31.  
  32.  
  33.  
  34.  
  35. ; create some test entities
  36. For i=0 To 7
  37.  dudu=CreateCube()
  38.  NameEntity dudu,Chr$(Rand(65,90))+""+Rand(1000)
  39. Next
  40.  
  41.  
  42.  
  43. ; how to cycle trough all entities:
  44. While MoreEntities()
  45.  entity= NextEntity()
  46.  Print "Handle: " + entity
  47.  Print "Name:   " + EntityName$(entity)
  48.  Print "Class:  " + EntityClass$(entity)
  49.  Print "-------------------------------"
  50. Wend
  51.  
  52.  
  53.  
  54. ; How to use EntityExists():
  55.  
  56. ;freddy=CreatePivot() ; try unrem
  57. Print
  58. Print EntityExists(freddy)
  59.  
  60. WaitKey()
  61. End
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68. Function MoreEntities() ; check if there are further entities
  69.  RtlMoveMemory2(Cycle_bank,Cycle_CurrentEntityPointer+Cycle_NextEntity,4)
  70.  If PeekInt(Cycle_bank,0)<>0
  71.   Return True
  72.  Else
  73.   Cycle_CurrentEntityPointer=Cycle_FirstEntity
  74.  EndIf
  75. End Function
  76.  
  77.  
  78. Function NextEntity()
  79.  Local entity
  80.  RtlMoveMemory2(Cycle_bank,Cycle_CurrentEntityPointer+Cycle_NextEntity,4)
  81.  entity=PeekInt(Cycle_bank,0)
  82.  Cycle_CurrentEntityPointer = entity
  83.  Return entity
  84. End Function
  85.  
  86.  
  87. Function EntityExists(entity)
  88.  While MoreEntities()
  89.   If NextEntity()=entity Then Return True
  90.  Wend
  91. End Function
  92.  
  93.  
[/code]

Comments :


Damien Sturdy(Posted 1+ years ago)

 I could have killed for this code a few years ago. Lovely work :)


Mike Barwise(Posted 1+ years ago)

 Excellent piece of code, really powerful and professional!I found changing the EntityExists( entity ) function to this:Function EntityExists( entity )   ;make sure pointer is reset to the start of the list   Cycle_CurrentEntityPointer=Cycle_FirstEntity      While MoreEntities()      DebugLog( "entity requested " + entity )          If NextEntity() = entity Then          DebugLog( entity + " found" )         Return True      EndIf   Wend      DebugLog( entity + " not found" )   Return False End FunctionSolved a few problems for me.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal