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

Forums

OverviewFelgo 3 Support (Qt 5) › How to access parent components methods from subcomponents

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

    Martin

    I have two cases, so far, where I find myself needing to access methods/properties of a parent from a child.

    Right now I have the EntityManager in my Scene.

    In my levels (loaded using Loader, as per the examples), I’m finding myself doing

       the_thing = parent.parent.entityManger.getEntityById(somthing_id)

    whenever I need to get an entity to operate on.

    This is because the parent of the level is the Loader, and what’s needed is the Scene that contains the loader.

    This is particularly bad because it means the level _has to be_ called from a Loader, it can’t be loaded direct into a Scene (since parent.parent would not be the right thing)

    Similarly, in game-objects I find myself needing to call on functions in the level.   Here is a platform that disappears a while after it is created.   It needs to tell the level that it is gone:

    EntityBase {
        entityId: "startPlatform"
        entityType: "platform"
        //...
        Timer {
            interval: 3000
            running: true
    
            onTriggered: {
                parent.parent.removePlatform(entityId)
                removeEntity()
            }
        }
    

     

    How can I avoid this nasty “parent.parent” thing?

    I can see that in the case of removing the platform, this could be a signal, since no return value is required, but how about the first case?

     

    • This topic was modified 9 years, 3 months ago by  GreenAsJade.
    #8658

    Alex
    Felgo Team

    Hi,

    property aliases come in handy for this use case. If you have e.g. something like this:

    Scene {
      id: gameScene
      
      property alias entityManager: entitiyManager
      EntitiyManager {
        id: entityManager
      }
    
      // your other stuff
    
    }

    Then every child of this gameScene, and also their children (basically every item that has the gameScene anywhere along their hierarchy) can access the entityManager by gameScene.entityManager. This removes the need to navigate up the exact structure with lots of parent calls.

    Cheers,
    Alex

     

    #8662

    Martin

    This is an option “to consider”.

    My initial reaction is that “gameScene.entityManager” is worse than “parent.parent.entitityManager” to put in a component, because then that component can only be used in an application that has a Game Scene called gameScene.

    I’ve been pondering putting the property on the _component_ and requiring the calling entity to set it.   That’s a bit cumbersome too 🙂

     

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