CountChildren ( entity )

Parameters

entity - entity handle

Description

Returns the number of children of an entity.

Only direct children are counted - children of children are not included. An entity gains children when other entities are created with it as their parent parameter, attached with EntityParent, or loaded as part of a model's hierarchy (a character's limbs, for example).

The usual pattern is a loop from 1 to CountChildren(entity) calling GetChild on each index - handy for walking a loaded model's parts, or for doing something to everything attached to a pivot.

See also: GetChild, FindChild, GetParent, EntityParent.

Example

; CountChildren Example
; ---------------------

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

SeedRnd MilliSecs()

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

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

; The sun - each planet is created as a child of it
sun=CreateSphere(16)
EntityColor sun,255,220,0

; Start with two planets
Gosub add_planet
Gosub add_planet

While Not KeyDown(1)

    ; Space adds another child planet
    If KeyHit(57) Then Gosub add_planet

    ; Turning the sun swings all of its children around it
    TurnEntity sun,0,1,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: add a planet   Arrow keys: move camera   Esc: exit"
    ; CountChildren returns how many entities are parented to the sun
    Text 0,20,"CountChildren(sun) = "+CountChildren(sun)

    Flip

Wend

End

; Create one planet parented to the sun, a little further out each time
.add_planet
radius#=CountChildren(sun)*1.2+2
angle=Rand(0,359)
planet=CreateSphere(8,sun)
ScaleEntity planet,0.3,0.3,0.3
PositionEntity planet,Cos(angle)*radius,0,Sin(angle)*radius
EntityColor planet,Rand(50,255),Rand(50,255),Rand(50,255)
Return

Index