Blitz3D+ Command Reference

EntityParent entity,parent[,global]

Parameters

entity - entity handle

parent - parent entity handle, or 0 to remove the entity's current parent

global (optional) - true for the child entity to keep its global position and orientation when reparented; true (default)

Description

Attaches an entity to a parent.

Once parented, the child moves, turns and scales with its parent - a sword parented to a hand, a turret parented to a tank, a camera parented to a player. The relationship is one way: moving the child never affects the parent.

With global true (the default) the entity stays exactly where it is in the world at the moment of reparenting; its local position is recalculated to suit the new parent. With global false the entity keeps its local coordinates, so it jumps to wherever those place it relative to the new parent.

Pass 0 as the parent to detach the entity, making it a free-standing entity in the world again.

See also: GetParent, CreatePivot, CountChildren, GetChild, FindChild.

Example

; EntityParent Example
; --------------------

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

camera=CreateCamera()
PositionEntity camera,0,6,-10
RotateEntity camera,25,0,0

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

; A truck that drives in a circle
truck=CreateCube()
ScaleMesh truck,1,0.5,1.5
EntityColor truck,0,150,255
PositionEntity truck,4,0,0

; A crate sitting in the middle of the yard
crate=CreateCube()
ScaleMesh crate,0.5,0.5,0.5
EntityColor crate,0,255,0

attached=0

While Not KeyDown(1)

    ; Space toggles the crate's parent between the truck and none
    If KeyHit(57)
        If attached
            ; Parent 0 releases the crate; it keeps its global position
            EntityParent crate,0
            attached=0
        Else
            ; Attach the crate: from now on it moves with the truck
            EntityParent crate,truck
            attached=1
        EndIf
    EndIf

    ; Drive the truck in a circle
    TurnEntity truck,0,1,0
    MoveEntity truck,0,0,0.08

    ; 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: attach/release the green crate   Arrow keys: move camera   Esc: exit"
    If attached
        Text 0,20,"EntityParent crate,truck - the crate moves with the truck"
    Else
        Text 0,20,"EntityParent crate,0 - the crate is released where it is"
    EndIf

    Flip

Wend

End

Index