Acces entity with name ?

Blitz3D Forums/Blitz3D Programming/Acces entity with name ?

An example of direct access without using types - the link to the include file which this example uses is:
http://www.blitzbasic.com/codearcs/codearcs.php?code=820
;================= example of direct access to an entity via name
;by sergio marcello - semar63@...
;using Ken Lynch GetProperty and SetProperty code from the code archive


Include "property_include.bb"  ;<-- you need to set this up to include the library code
SeedRnd MilliSecs()

Graphics3D 640,480,0,2

cam = CreateCamera()

;our propertybag which contains the properties. it can be a pivot, a cube, a sprite, anything 3D
Global pbag = CreatePivot()

;some local for positioning the cubes
a = 10
z = 25
msg$ = "" ;a simple message system

;let's create 10 cubes, we call it toto1 ... toto10
For n = 1 To 10
	cube = CreateCube()
	PositionEntity cube, Rand(-a,a),Rand(-a,a), z
	SetProperty (pbag, "toto"+n, Str(cube))
	DebugLog.Print GetProperty(pbag, "toto"+n)
Next


While Not KeyDown(1)

;now we input the name of a cube, and change its color to red
If KeyHit(57) Then ;space bar to input

	;enter the cube name, for example toto5
	name$ = Trim(Input$("name of the cube ?"))
	
	;get the property if exists
	retval$ = GetProperty(pbag, name)
	DebugLog.Print name +  " = <" + GetProperty(pbag, name) + ">"
	
	;if the property exists, then transform it to an entity, and change the color
	If retval <> "" Then
		entity = retval
		EntityColor entity, 255,0,0
		msg = "Entity " + name + " has changed the color "
	Else
		msg = "Entity " + name + " not found "
	EndIf
EndIf


UpdateWorld
RenderWorld
Text 0,400,"Press space bar to enter a cube name"
Text 0,450,msg$
Flip
Wend

End