Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Forums

OverviewFelgo 3 Support (Qt 5) › Programatic scene configuration/management at scene start?

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #8601

    Martin

    What’s the right way to go about doing construction, and especially management, of scene elements at run time?

    I can’t find an event/signal that I can hook into to do things that require asking the EntityManager for entities and operating on them before the scene gets under way.

    I can see that I could use a timer with a very short interval, or even just use the “triggeredOnStart” of a timer to do stuff at the beginning only, but this seems rather inelegant … is there a better way?

    #8637

    Christian
    Felgo Team

    Hi Martin,

    as we have talked about in this forum thread, you could do initialization/creation of entities in the Component.onCompleted function in your Scene, which is the constructor called once the Scene is created.

    However, for entities the better approach is using the built-in EntityManager pooling functionality for that.

    Entity pooling has the advantage, that you can re-use destroyed entities as they are not completely removed from memory but just set invisible.

    You can then call createPooledEntitiesFromComponent() in the Component.onCompleted handler and create an amount of pooled entities when your app starts, or when the Scene gets loaded. Also, make sure to set EntityBase::poolingEnabled and EntityManager::poolingEnabled to true.

    See this example how to pre-create 10 entities at game startup, which you can then use later in your game:

    import Felgo 3.0
    import QtQuick 2.0
    
    GameWindow {
    
      EntityManager {
        id: entityManager
        // creates the entities in the game scene
        entityContainer: scene
    
        // enables pooling, also set in the Entity that shall be pooled to true
        poolingEnabled: true
    
        Component.onCompleted: {
          // creates 10 pooled entities at startup, which are then made invisible
          createPooledEntitiesFromComponent(entityComponent, 10)
        }
      }
    
      Scene {
        id: scene
    
        Row {
          spacing: 5
    
          SimpleButton {
            text: "Create Entitiy"
            onClicked: {
              entityManager.createEntityFromComponentWithProperties(entityComponent, {x: utils.generateRandomValueBetween(0, scene.width), y: utils.generateRandomValueBetween(0, scene.height) })
            }
          }
          SimpleButton {
            text: "Remove Last Added Entity"
            onClicked: {
              entityManager.removeLastAddedEntity()
            }
          }
          // toggle pooling - when disabled, a call of Entity.removeEntity() leads to destruction of the entity
          SimpleButton {
            text: "PoolingEnabled: " + entityManager.poolingEnabled
            onClicked: {
              entityManager.poolingEnabled = !entityManager.poolingEnabled
            }
          }
        }
      }
    
      // in a real game, make an own qml file out of this and use createEntityFromUrl() instead of createEntityFromComponent()
      Component {
        id: entityComponent
        EntityBase {
          id: poolingTestEntity
          entityType: "poolingTest"
    
          // enable this entity type for pooling (also set EntityManager::poolingEnabled to true)
          // other entity types might not be set for pooling, thus this needs to be enabled per-entity-type
          poolingEnabled: true
    
          width: 20
          height: 30
    
          Rectangle {
            color: "green"
            anchors.fill: parent
          }
    
          MouseArea {
            anchors.fill: parent
            // click on the rectangle to remove it
            onClicked: poolingTestEntity.removeEntity()
          }
    
          onEntityCreated: console.debug("entity created with id", entityId)
          onEntityDestroyed: console.debug("entity destroyed with id", entityId)
    
          Component.onCompleted: console.debug("entity constructor with id", entityId)
          // onDestruction is NOT called when removeEntity is called, because poolingEnabled is set to true
          // this speeds up multiple creation/removal of entities
          Component.onDestruction: console.debug("entity destructor with id", entityId)
        }
      }
    
    }
    

    I’ll also add this example to the docs so this concept it is more clear in future.

    Cheers, Chris

     

     

    #8638

    Martin

    Cool thanks!   At the time I asked this question, I did not know about Component.onCompleted.  I agree it’s what I was looking for.

    An example of why I need this is to add created entities to a list maintained by the level (in my case, platforms) for later management of them.   I now know that I can call the necessary level functions from the onCompleted of the relevant components, so the level “knows” about them.

     

Viewing 3 posts - 1 through 3 (of 3 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded