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

Forums

OverviewFelgo 1 Support › Dynamically creating Button-like entities

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

    Preston

    Hi,

    I’m working on my first V-play game.

    I’m using the Multi Scene Multi Level Game Template.

    In one of my levels I have this code:

     

    import QtQuick 1.1
    import VPlay 1.0
    
    Item {
        property string levelName: "Thirty Consonants"
        property url jsonFile: "../json/ThirtyConsonants.json"
    
        Component.onCompleted: {
            console.debug(jsonFile)
            console.debug("inside ThirtyConsonants")
    
            var data = fileUtils.readFile(jsonFile)
            console.debug(data)
    
            var jsonObj = JSON.parse(data)
            console.debug("jsonObj")
            console.debug(jsonObj)
    
            function methodToCall(){
                console.debug("methodToCall")
            }
    
            for(var i = 0; i < jsonObj.length; i++)
            {
                //print to console each of the consonants of the tibetan language
                console.debug(jsonObj[i])
    
                //THIS IS WHERE I WANT TO CREATE BUTTONS FOR EACH CONSONANT DYNAMICALLY
                //SO THAT WHEN THE BUTTON IS TAPPED, IT PLAYS THE FILE ASSOCIATED WITH THAT CONSONANT
                //e.g. a button that displays ཀ should play ཀ.wav
            }
    
        }
    }
    

     

    I need the code to put inside the for loop that will dynamically create the number of buttons depending on the length of the  json.

    After my research I found this:

     

    var newEntityProperties = {
                         x: Math.random()*scene.width,
                         y: Math.random()*scene.height,
                         rotation: Math.random()*360
                     }
    
                     entityManager.createEntityFromComponentWithProperties(
                                 boxEntity,
                                 newEntityProperties);

     

    My question is, how do I transform the above code to a button-like entity that will work inside  for loop to create buttons dynamically????

     

    Thank you very much.

     

    #7173

    Christian
    Felgo 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

    #7176

    Preston

    Hi Chrisu,

     

    Thank you very much.

    This was a big help. 🙂

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