Forums

OverviewFelgo 2 Support (Qt 5) › EntityManagers getLastAddedEntity()

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #16111

    Alexander

    Hi!

     

    I have a question regarding the above mentioned method of the EntityManager.

     

    I want to display a 2-dimensional grid, where one cell is represented by an Entity “Cell” which should contain some data about it.

     

    The size of the grid should be fixed but still I do not want (and probably anyway should not) manually create every cell – So using the entityManager for this seemed obvious. However I want quick and easy access to my cell objects via their position – which would work if I would use the “list” – e.g.

    cellList[x * dimensionX + y]….

    I could do it like in the documentation:

    Item {

    cellList: [

    Cell{…},

    Cell{…}

    ]

    }

    But again, this would mean manual work. I than came up with the idea to create each cell with the entityManager.createEntityFromUrl(), get it out with getLastAddedEntitity(), setup the needed data and than add it to my list.

     

    However I see 2 problems:

    1. If the getLastAddedEntitiy() returns a copy (which is no where stated) I would needlessly blow up my data needs
    2. “list” anyways seems to be kinda static (documentation states objects cannot added or removed)

    So I would like to know if 1) is true and what possible alternatives I have for 2).

     

    Thx in advance!

     

     

    #16114

    Alexander

    Update:

     

    I decided to use a separate EntitiyManager for my Cells and use getEntityArrayByType() to get my needed data.

     

    However after trying to iterate through a for-loop to create my needed objects AND the creation code is always called – I always end up with just ONE single object in the manager.

     

    My scene with the manager:

     

    Scene {
    ...
    EntityManager {
             id: cellBoardEntityManager
             entityContainer: golScene
        }
    ...
    }

    My cell entitiy

    import Felgo 3.0
    import QtQuick 2.0
    
    EntityBase {
        entityId: Cell
        entityType: "Cell"
    
        Text {
            id: cellText
            text: qsTr("O")
        }
    
        property bool isAlive: false
        property int positionX: 0
        property int positionY: 0
    
        Component.onCompleted: {
            x = positionX
            y = positionY
        }
    }

    And here another entity where I want o use the creation code for cells:

    import Felgo 3.0
    import QtQuick 2.0
    
    EntityBase {
    
        entityId: CellBoard
        entityType: "CellBoard"
    
        Component.onCompleted: {
            for (var x = 0; x < 10; x++)
            {
                for (var y = 0; y < 10; y++)
                {
                    cellBoardEntityManager.createEntityFromUrl(Qt.resolvedUrl("Cell.qml"));
                }
            }
    
            var testArray = cellBoardEntityManager.getEntityArrayByType("Cell");
            var testLength = bac.length;
        }
    }

    When debugging through I see that the “testArray” has only one entry (for the position 0,0 – so the very first creation) and “testLength” is 1.

     

    I tried several changes and looked through the tutorials but couldn’t find out so far why this is not working.

     

    Thx in advance

     

     

    #16118

    Günther
    Felgo Team

    Hi Alexander,

    one thing I notice in your code is that your entities set an incorrect EntityBase::entityId. The entityId is unique identifier of each dynamically created entity, it is set automatically when creating entities with EntityManager. Also, it is a string property – unlike the QML Item id used to reference static QML items in your code.

    When creating entities dynamically, you do not have to worry about much overhead when managing a custom list of the created entities, as only the references and not a deep copy of the objects is used.

    However, if you are going to manually manage your custom created objects in a field, you can probably also skip using the EntityManager. The main benefit of the EntityManager is to not having to care about managing a list of created entities, to access previously created entities or to use pooling to improve performance when heavily creating and destroying entities in a game.

    For a more static game field that you want to initialize with dynamically created objects, you can also create QML Objects dynamically yourself. (The entity manager uses the described features internally and tracks entities created that way). Our demo Juicy Squash is also set up that way for managing the random grid of fruits. You can have a look at the Juicy Squash tutorial for more details how this is implemented.

    Hope this helps!

    Cheers,
    Günther

    #16120

    Alexander

    Hi Günther,

     

    Thx for the detailed answer and the hint to the tutorial! It looks very promising and like the direction I want to go.

     

    Cheers,

    Alexander

Viewing 4 posts - 1 through 4 (of 4 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