Blitz3D+ Command Reference

FindChild ( entity,name$ )

Parameters

entity - entity handle

name$ - name of the child entity to find

Description

Returns the first child of an entity with a matching name, or 0 if none is found.

The whole hierarchy below the entity is searched, not just direct children, so you can grab a deeply nested part of a loaded model in one call - the hand bone of a character, the turret of a tank - as long as you know the name it was given in the modelling program.

Names come from the model file or from NameEntity. If you are not sure what the parts of a model are called, walk it with CountChildren/GetChild and print each EntityName.

See also: GetChild, CountChildren, EntityName, NameEntity.

Example

; FindChild Example
; -----------------

Graphics3D 640,480,0,2
SetBuffer BackBuffer()

camera=CreateCamera()
PositionEntity camera,0,4,-10
RotateEntity camera,18,0,0

light=CreateLight()
RotateEntity light,45,45,0

; A sun with four named child planets
sun=CreateSphere(16)
EntityColor sun,255,220,0

Dim names$(4)
names$(1)="mercury"
names$(2)="venus"
names$(3)="earth"
names$(4)="mars"

For i=1 To 4
    planet=CreateSphere(8,sun)
    ScaleEntity planet,0.3,0.3,0.3
    PositionEntity planet,Cos(i*90)*(i*1.1+1),0,Sin(i*90)*(i*1.1+1)
    ; Give each planet a name that FindChild can look up
    NameEntity planet,names$(i)
Next

index=1

While Not KeyDown(1)

    ; Space looks up the next planet by name
    If KeyHit(57)
        index=index+1
        If index>4 Then index=1
    EndIf

    ; Turning the sun swings its children around it
    TurnEntity sun,0,1,0

    ; Colour all planets blue, then use FindChild to find one BY NAME and light it up
    For i=1 To 4
        EntityColor FindChild(sun,names$(i)),100,100,255
    Next
    EntityColor FindChild(sun,names$(index)),0,255,0

    ; Arrow keys move the camera
    If KeyDown(200) Then MoveEntity camera,0,0,0.1
    If KeyDown(208) Then MoveEntity camera,0,0,-0.1
    If KeyDown(203) Then TurnEntity camera,0,1,0
    If KeyDown(205) Then TurnEntity camera,0,-1,0

    RenderWorld

    Text 0,0,"Space: find next planet   Arrow keys: move camera   Esc: exit"
    Text 0,20,"FindChild(sun,"+Chr$(34)+names$(index)+Chr$(34)+") is the green planet"

    Flip

Wend

End

Index