Forums

OverviewV-Play 1 Support › EntityManager does not remove EntityBaseDraggable

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #4465

    GP

    Hi,

    does anyone know why the method removeAllEntities does not remove an EntityBaseDraggable object? The property preventFromRemovalFromEntityManager is set to false.

    We are using the BuildEntityButton to drag an EntityBaseDraggable around in our GameScene, but if we want to reset the whole Scene with entityManager.removeAllEntities() the EntityBaseDraggable is still there. This behaviour occurs if the onEntityWasBuilt was not called that means the BuildEntityButton is still in the onEntityPressed handler (scene was changed while the user is still dragging the shape around).

     

    Cheers!

    #4472

    Christian
    V-Play Team

    Hi,

    please post a complete example with the relevant source code that demonstrates your issue.

    Cheers, Chris

    #4474

    GP

    Hi Chris,

    here is a little code example. To find that bug you need to drag some boxes on the platform (they will be removed correctly), then let one box fall on the ground and drag (and hold) a new box into to the scene at once. If you restart the scene now, all entities are removed except the EntityBaseDraggable object.

    Here is the source code:

    Box.qml

    // import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
    import QtQuick 1.1
    import VPlay 1.0
    import Box2D 1.0
    
    EntityBaseDraggable {
        id: box
        entityType: "rectangle"
        entityId: "box"
    
        colliderComponent: boxCollider
        selectionMouseArea.anchors.fill: rect
        gridSize: 1
        height: 20
        width: 20
        dragOffset: Qt.point(0,0)
        allowedToBuild: true
    
        property alias sWidth: rect.width
        property alias sHeight: rect.height
        property bool isPlaced:false;
    
        showRectangleWhenBuildingNotAllowed: false
    
    
        Rectangle {
            id: rect
            width: 30
            height: 30
            color: "yellow"
        }
    
    
        BoxCollider {
            id: boxCollider
            anchors.fill:rect
            density:100
    
            friction: 1.6
            restitution: 0
    
            fixture.onBeginContact: {
                if(!isPlaced) {
                    rect.color = "red"; allowedToBuild = false;}}
            fixture.onEndContact:  {
                rect.color = "yellow"; allowedToBuild = true}
    
        }
    }
    

    BoxPreview.qml

    import QtQuick 1.1
    import VPlay 1.0
    import Box2D 1.0
    
    
    BuildEntityButton {
    
        id: ballPreview
        toCreateEntityType: "Box.qml"
        width: 20
        height: 20
    
        property int shapeWidth: 20
        property int shapeHeight: 20
    
        creationProperties: {
    
            "sWidth": shapeWidth,
            "sHeight": shapeHeight,
            "isPlaced":true
        }
    
        onEntityPressed: {
    
             createdEntity.isPlaced = false;
    
         }
    
        Rectangle {
            height: 20
            width: 20
            color: "yellow"
            anchors.fill: parent
        }
    
    }
    

    GameOverScene.qml

    // import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
    import QtQuick 1.1
    import VPlay 1.0
    import Box2D 1.0
    
    Scene {
        id: gameOverScene
    
        Button {
            text: "Restart"
            anchors.centerIn: parent
    
            onClicked: {
                entityManager.removeAllEntities();
                scene.state = "game"
            }
        }
    
    
    }
    

    main.qml

    import VPlay 1.0
    import Box2D 1.0
    import QtQuick 1.1
    
    GameWindow {
        id: window
        // the size of the Window can be changed at runtime by pressing the number keys 1-6 with QML window active
        // the content of the logical scene size (480x320) gets scaled to the window size based on the scaleMode
        width: 960
        height: 640
    
        EntityManager {
            id: entityManager
            entityContainer: scene
        }
    
        GameOverScene {
            id: gameOverScene
            opacity: 0
        }
        
        Scene {
            id: scene
    
            opacity: 0
    
            Row {
                spacing: 5
                anchors {
                    top: parent.top
                    horizontalCenter: parent.horizontalCenter
                }
    
                BoxPreview{shapeHeight: 50; shapeWidth: 50}
                BoxPreview{shapeHeight: 50; shapeWidth: 50}
                BoxPreview{shapeHeight: 50; shapeWidth: 50}
            }
    
            // platform (here you can place shapes)
            Rectangle {
                width: 50
                height: 10
    
                x: 100
                y: parent.height / 2
    
                color: "blue"
    
                BoxCollider {
                    anchors.fill: parent
                    bodyType: Body.Static
                }
            }
    
    
    
            // ground (if shape falls on ground, game is over)
            Rectangle {
                width: parent.width
                height: 5
                color: "grey"
    
    
                anchors.bottom: parent.bottom
    
                BoxCollider {
                    anchors.fill: parent
                    bodyType: Body.Static
    
                    fixture.onBeginContact: {
                        scene.state = "gameOver"
                    }
    
                }
            }
    
    
            PhysicsWorld {
                id: physicsWorld
                gravity.y: -9.81
                z: 10
    
    
                updatesPerSecondForPhysics: 60
                velocityIterations: 5
                positionIterations: 5
            }
    
            onStateChanged: {
                if (state === "gameOver")
                    activeScene = gameOverScene
                else if (state === "game")
                    activeScene = scene
            }
    
            state: "game"
    
            states: [
                State {
                    name: "game"
                    PropertyChanges {target: scene; opacity:1}
                },
                State {
                    name: "gameOver"
                    PropertyChanges {target: gameOverScene; opacity:1}
                }
            ]
    
    
        }
    }
    
    

     

    Cheers!

    #4479

    Christian
    V-Play Team

    Ah I got it, thanks to your great example it was easy to find!

    The reason why it did not work, is the internal behaviro of the BuildEntityButton: it creates an entity for each button, which gets then dragged around. However, this entity should not be removed when you call entityManager.removeAllEntities, thus it sets its preventFromRemoval flag to true. So what you can do to solve your use-case, is just make the created entity invisible once the game is over, or easier done when the BuildEntityButton gets invisible. So add this code to your BoxPreview and it should work fine:

        // at changing the scene, make the internally created entity from BuildEntityButton invisible at well
        onVisibleChanged: {
          if(!visible) {
            createdEntity.visible = false;
          }
        }

    Cheers, Chris

    #4483

    GP

    Hi Chris,

    thank you for your answer. This is a really good approch but the problem is the shape (box) is still there (invisible). I really need to remove the shape completely, so no other shape can collide with it.

    Cheers!

    #4485

    Christian
    V-Play Team

    Hi,

    just set the collidersActive flag also to false, this should deactivate the collider then.

    Cheers, Chris

    #4487

    GP

    Hi again,

    to change the collidersActive flag did not work for me, instead I tried this and this solved my problem:

        onVisibleChanged: {
          if(!visible) {
            createdEntity.visible = false;
            createdEntity.preventFromRemovalFromEntityManager = false;
          }
        }

    You have to change the preventFromRemoval flag explicitly again, it is not enough to set that flag only in the box property. However my problem is solved! Thank you very much for your support!

     

    Cheers!

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

RSS feed for this thread

You must be logged in to reply to this topic.

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
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support