Entity access

Blitz3D Forums/Blitz3D Programming/Entity access

..okay..it may be dumb question, but please be gentle and provide some useful information if you have it..

let say i load some level geometry like this

Map=LoadAnimMesh("Level_01_part_01.B3D")

Map=LoadAnimMesh("Level_01_part_02.B3D")

..so..everything will be fine, level will be completely loaded and nice..now..how to access entities loaded with first Map handle? They all do have names already set in modelling program, but i would like to know is there any other way to access entities, rather than FindChild, wich is conditioned with use of handler name Map, in this case..obviously, all entities are in there, since i see them all and its all working fine..but How to access entities from first one after use of second one Map?

Erm, maybe i don't get what your saying, but use map1 to load the second level part?

You can also use getchild(), along with countchildren() command.

Why don't you just use a different variable name for each mesh? e.g.


MapOne=LoadAnimMesh("Level_01_part_01.B3D")

MapTwo=LoadAnimMesh("Level_01_part_02.B3D")

Why not set the EntityName of each mesh after loading it? Just loop through all the children in your map after loading it and add something to the EntityName i.e "Barrel" > "Map01Barrel" etc

..thats fine guys...I know that..but i was wondering is there any way to access every single entity on the scene, loaded on the way that both parts of level geometry are loaded under same name..I thought, it should be some way since objects are obviously on scene...I mean, practically is there a way to pass trough entities, regardless handler initially assigned during LoadAnimMesh?

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.

..types..of course...slap me all guys... :)

..thats fine guys...I know that..but i was wondering is there any way to access every single entity on the scene, loaded on the way that both parts of level geometry are loaded under same name..I thought, it should be some way since objects are obviously on scene...I mean, practically is there a way to pass trough entities, regardless handler initially assigned during LoadAnimMesh?
Nobody seems to have mentioned it but this is exactly what you don't do without garbage collection as it's how you create memory leaks. If you don't maintain at least one handle/reference to a mesh then there's no way to free it.

What you need is an object system of some kind...


Nobody seems to have mentioned it but this is exactly what you don't do without garbage collection as it's how you create memory leaks. If you don't maintain at least one handle/reference to a mesh then there's no way to free it.


Wrong, Clearworld(). ;D.

maybe you'll also find this one useful: http://blitzbasic.com/codearcs/codearcs.php?code=2012

Wrong, Clearworld(). ;D.

That strikes me as a horrible way to go about things but, ultimately, yes I guess you could nuke everything if you were sloppy enough to lose a reference. Does this definitely get you your memory back?

Wrong, Clearworld(). ;D.

That strikes me as a horrible way to go about things but, ultimately, yes I guess you could nuke everything if you were sloppy enough to lose a reference
Beware that ClearWorld() does not clear everything. It clears entities, brushes and textures only. If you ever work on the assumption that ClearWorld() clears types, sounds etc too, then you're setting yourself up for a colossal memory leak.

Well I was really just talking about entities (as per the thread topic) but, yeah, that as well. Much as it's tempting to steam-roller ahead in B3D 'ad hoc', it's worth making provisions to free everything you create at the point you write the code to create it.

This is where Blitzmax batters Blitz3D to a bloody pulp. In Magicville I have one base object and everything else is created within that. That way all I have to do is Null the base object, and GC gets everything else for me because no other references exist to any of it.

Plus you will still have all your references in your type objects, with values, that point no-where. You'll need to also delete or set to 0, all your entity handles on top of what gfk says.


Plus you will still have all your references in your type objects, with values, that point no-where. You'll need to also delete or set to 0, all your entity handles on top of what gfk says.


Delete Each my_type