[bb] Object and Handle with Entities by Rob [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:40

Previous topic - Next topic

BlitzBot

Title : Object and Handle with Entities
Author : Rob
Posted : 1+ years ago

Description : Example on how to use object and handle to hold unlimited data about entities with very low lookup time.

Code :
Code (blitzbasic) Select
; Example on how to use object and handle
; to hold unlimited data about entities with
; very low lookup time.
; By Rob Cummings

Graphics3D 640,480,0,2

Type info
Field name$,a,b,c,stuff,mass#,physics,etc
End Type

;create a ball and some related information.
a.info=New info
a
ame$="bowling ball"
amass#=0.9
ball=CreateSphere()
NameEntity ball,Handle(a) ; the type handle held within the entity name.


;now all you do is grab the entity using any of Blitz's own commands.
;This could be LinePicks, Collisions And so forth.


;uncomment for a proper example within a collision framework
;ent = EntityCollided(bat,collisiontype_ball)
ent=ball ; test

If ent
a.info = Object.info(EntityName(ent)) ; quickly retrieve source type
Print a
ame$
Print amass#
EndIf


Comments :


Techlord(Posted 1+ years ago)

 Another option is to use a Array of Types. Ideal for managing types with a specified collection amount and provides faster access than using Object/Handle.
; Example on how to use array of types
; to look up types with a extremly low lookup time.
; By Rob Cummings and Frankie Taylor

Graphics3D 640,480,0,2

Type info
Field name$,a,b,c,ball,stuff,mass#,physics,etc
End Type
Dim infoHandle.info(100) ;<-- arrays of types

; Part I create a ball and some related information.
now=MilliSecs()
a.info=New info
a
ame$="bowling ball"
amass#=0.9
aall=CreateSphere()
NameEntity aall,Handle(a) ; the type handle held within the entity name.


;now all you do is grab the entity using any of Blitz's own commands.
;This could be LinePicks, Collisions And so forth.


;uncomment for a proper example within a collision framework
;ent = EntityCollided(bat,collisiontype_ball)
ent=aall ; test

If ent
a.info = Object.info(EntityName(ent)) ; quickly retrieve source type
Print a
ame$
Print amass#
EndIf
Print "time:"+Str(MilliSecs()-now)





;Part II using a Array of Types
now=MilliSecs()
b.info=New info
b
ame$="bowling ball2"
bmass#=0.9
ball=CreateSphere()
infoHandle(2)=b ;we store our second type in the second array element

;now all you do is grab the entity accessing the array element.
;This could be LinePicks, Collisions And so forth.

;uncomment for a proper example within a collision framework
;ent = EntityCollided(bat,collisiontype_ball)
ent=ball ; test

If ent
b.info = infoHandle.info(2) ; quickly retrieve source type
Print b
ame$
Print bmass#
EndIf
Print "time:"+Str(MilliSecs()-now)

WaitKey()



Dabbede(Posted 1+ years ago)

 I prefer the 1st solution! Great!


Mikorians(Posted 1+ years ago)

 Techlord's 2nd infohandle solution didn't work out and alwaysRetrieves type 2.Are we supposed to iterate through each infohandle?Could you finish your line of thought please?Basically- Ent had no connection to infohandle at the end of the program. [/i]