Entity access

Blitz3D Forums/Blitz3D Programming/Entity access

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Naughty Alien, you can create a parent pivot for organizating the world into different categories as it's loaded, and use the GetChild method to search for it.

Function GetChildByName%(Name$, Entity%, Recursive=true)
    Local i, Results
    If (EntityName$(Entity%) = Name$) Then Return Entity%
    If (Recursive = False) Then Return 0

    For i=1 to CountChildren(Entity%)
        Results = GetChildName%(Name$, Entity%, Recursive)
        If (Results <> 0) Then Return Results
    Next

   Return 0
End Function


; Create a parent pivot to store all meshes related to the World.
Global World = CreatePivot()

LoadAnimMesh("Plains.b3d", World)
LoadAnimMesh("Houses.b3d", World)
LoadAnimMesh("Rocks.b3d", World)

; Now, what if we want to look up for one? Let's say Rocks.b3d contains an object called "ShinyRock". Let's look for it.
ShinyRock = GetChildByName%("ShinyRock", World)
If (ShinyRock = 0) Then RuntimeError("Shiny Rock object not found!")

; Operate with it
MoveEntity(ShinyRock, 0, 1, 0)


If this method doesn't suit well and you want something more "general" or "clean", you can store up your meshes in types so you can access them easily. Something of the like:

Type tMeshPoolNode
    Field Name$
    Field Entity
End Type

Function LoadMeshToPool%(Name$, Filename$, Parent=0)
    Local Entity% = LoadMesh(Filename$, Parent)
    AddMeshToPool(Name$, Entity%)
    Return Entity%
End Function

Function LoadAnimMeshToPool%(Name$, Filename$,Parent=0)
    Local Entity% = LoadMesh(Filename$, Parent)
    AddMeshToPool(Name$, Entity%)
    Return Entity%    
End Function

Function AddMeshToPool(Name$, Entity%)
    Local Node.tMeshPoolNode = new tMeshPoolNode
        Node\Entity% = Entity%
        Node\Name$ = Name$
End Function

Function GetMeshFromPool%(Name$)
    Local Node.tMeshPoolNode
    For Node = each tMeshPoolNode
         If (Node\Name$ = Name$) Then Return Node\Entity%
    Next
    Return 0
End Function

Function FreeFromPool(Name$)
    Local Node.tMeshPoolNode
    For Node = each tMeshPoolNode
         If (Node\Name$ = Name$) Then
             FreeEntity(Node\Entity)
             Delete Node
             Return
         End If
    Next
Function

Function FreePool()
    Local Node.tMeshPoolNode = First tMeshPoolNode
    For Node = each tMeshPoolNode
         FreeEntity(Node\Entity)
    Next

    Delete Each tMeshPoolNode
End Function


Usage example:
LoadMeshToPool("World1", "World1.b3d")
LoadMeshToPool("World2", "World2.b3d")
LoadMeshToPool("Player", "Player.b3d")

While(Not KeyHit(1))
    ...
    Player = GetMeshFromPool("Player")
    MoveEntity(Player, 0, 1, 0)
    ...
Wend

FreePool()
End


It doesn't look into the Submeshes names, but rather on the assigned name to the model file loaded. It's a clean way to do the job and keep track of your resources too.