OverviewV-Play 2 Support (Qt 5)Programatic scene configuration/management at scene start? › Reply To: Programatic scene configuration/management at scene start?

OverviewV-Play 2 Support (Qt 5)Programatic scene configuration/management at scene start? › Reply To: Programatic scene configuration/management at scene start?

V-Play 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 VPlay 2.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: {
      // 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



Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
create apps
create games
cross platform
native performance
3rd party services
game network
level editor
easiest to learn
biggest time saving
best support