Forums

OverviewV-Play 1 SupportDynamically creating Button-like entities › Reply To: Dynamically creating Button-like entities

OverviewV-Play 1 SupportDynamically creating Button-like entities › Reply To: Dynamically creating Button-like entities
#7173

Christian
V-Play Team

Hi bliss, welcome back to the forums!

Your attempt to create entities at runtime is actually correct. To create them, you need a component or QML file that has an EntityBase as top-level element.

You also need an EntityManager in your GameWindow (the template should have one already included). Be sure to set the entityContainer in your EntityManager, for example to the scene where the buttons should be created in.

For example, your code can look like this:

  1. Create a Component for your dynamic buttons (this will be instantiated by EntityManager) which contains an EntityBase and a SimpleButton. Be sure to set entityId to a different value every time. Note how I have specified an index that sets the ID and the x/y properties.
     Component {
            id: btnComp
    
            EntityBase {
                property int index: 1
                entityId: index
                entityType: "buttonEntity"
                SimpleButton {
                    id: btn
    
                    property int col: index % 6 + 1
                    property int row: Math.floor(index / 6) + 1
    
                    x: col * 40
                    y: row * 40
    
                    width: 30
                    height: 30
    
                    text: col + "/" + row
    
                    onClicked: console.debug("button " + btn.text + " clicked")
                }
                Component.onCompleted: console.debug("btn created " + index)
            }
        }

     

  2. Call entityManager.createEntityFromComponentWithProperties
            for(var i = 0; i < jsonObj.length; i++)
            {                        entityManager.createEntityFromComponentWithProperties(btnComp, {index: i})
            }

     

This should create all buttons at different positions. You can then use the onClicked() handler in the Component for playing the wave file.

 

Please let us know if this helped :)

 

Cheers, Chrisu

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