Acces entity with name ?

Blitz3D Forums/Blitz3D Programming/Acces entity with name ?

Filax(Posted 2004) [#1]
Hi :)

I have try to make this function to acces directly an entity
with a name :

Type Obj
Field Name$
Field Entity
Field Px#
Field Py#
Field Pz#
End Type

Graphics3D 640,480,32,2

Cam=CreateCamera()
PositionEntity Cam,0,0,-10

Light=CreateLight(2)
PositionEntity light ,-20,-20,0

N.Obj=New Obj
N\Name$="TOTO"
N\Entity=CreateCube()
NameEntity N\Entity,Handle(N)
N\Px#=0
N\Py#=0
N\Pz#=0

PositionEntity N\Entity,N\Px#,N\Py#,N\Pz#


While Not KeyDown(1)
Turn("TOTO")
UpdateWorld
RenderWorld

Flip
Wend

Function Turn(Name$)
For N.Obj=Each Obj
If N\name$=name$ Then
TurnEntity N\Entity,1,1,1
EndIf
Next
End Function

But ... It is possible to accelerate the name search
directly with handle or object function ????


semar(Posted 2004) [#2]
From what I understand, you need an Hash Table, which is a table where the elements can be stored using a key, and this key could be either a number or a string.

With an Hash Table you could something like Turn("TOTO"), and it will pick from the table the object which is connected to the word "TOTO".

Such thing is not possible with the current type structure; even using Object and Handle, you are limited because the name of your entity should be a number, which represent the handle of the type element.

But you could build up an hash table using a pivot as property container, and the very good SetProperty and GetProperty functions by Ken Lynch, which you find in the code archive:
http://www.blitzbasic.com/codearcs/codearcs.php?code=820

With that simple piece of code - IMO a gem of programming - you can set and get property values using a property name.

For example, you could do something like:
SetProperty mypropertybag, "TOTO", type_element

Later, you can do something like:
type_element = GetProperty mypropertybag, "TOTO"

The trick here is to use a pivot as propertybag, and give to the property "TOTO" the value of the created type element.

Once you have retrieved the type element, you use Object to access to it, and you're done. I'm not sure, but you could also do something like:
temp.mytype = GetProperty propertybag, "TOTO"
print temp\whatever

Hope this has sense for you,
Sergio.

P.S.
If you just need to access the entity, and not the type element, then it would easier:
acube = createcube()
SetProperty propertybag,"TOTO",acube
.
.
;want to turn the cube "TOTO" ?
entity = GetProperty propertybag, "TOTO"
turnentity entity,1,1,1



Filax(Posted 2004) [#3]
Thanks semar !!


semar(Posted 2004) [#4]
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