Code archives/Algorithms/Object and Handle with Entities

This code has been declared by its author to be Public Domain code.

Download source code

Object and Handle with Entities by Rob
(Posted 24 years ago)
Example on how to use object and handle to hold unlimited data about entities with very low lookup time.
; 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\name$="bowling ball"
a\mass#=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\name$
	Print a\mass#
EndIf

Comments

Techlord
(Posted 22 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\name$="bowling ball"
a\mass#=0.9
a\ball=CreateSphere()
NameEntity a\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=a\ball ; test

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





;Part II using a Array of Types
now=MilliSecs()
b.info=New info
b\name$="bowling ball2"
b\mass#=0.9
b\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=b\ball ; test

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

WaitKey()


Dabbede
(Posted 22 years ago)
I prefer the 1st solution! Great!

Code Archives Forum