OverviewFelgo 1 Support › QML properties?

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
  • #3996


    wir würden gerne bei unserem Spiel (Bejeweled) ein 2-dimensionales Array verwenden, welches die IDs der Entities abspeichern soll, und zur Auswertelogik (in einem javascript file) dient.
    Wie bzw. wo kann man ein solches am besten erstellen bzw. würde es vielleicht andere Möglichkeiten dafür geben.
    mfg schustiii


    Felgo Team

    you have two options to store arrays: either in QML files with a variant property, or in JavaScript files.

    In your case, it is better to store it in a JS file. The reason is, that changing of variant properties in QML files would need to overwrite the whole array when any of its entries changes which is slower than just modifying the array.

    Thus create a js file named something like GameLogic.js and import that file in a qml file where you have access to. Consider this tutorial, which also uses a 2-dimensional-array:


    PS: Please write in english so all users can understand



    Thanks for the tip!
    But now i’ve encountered another problem 🙁
    It seems that my array isn’t stored properly, because when i am calling a function in the gamelogic.js at another time, the array isn’t initalized anymore, and all my data is lost.


    Felgo Team

    What I’ve found out in QML: when you use a JS file in two different files, it is instantiated twice and you can’t access the same variables from different QML files…


    Felgo Team

    Yes this is expected behavior. You can compare a QML file like a class definition, and once you call Qt.createObject() or createEntity() or whenever you put this QML component as a child to some other QML file, an instance, of this class / qml definition is created. And each of these instances holds an own JavaScript context. So put your gamelogic component only once in a top-level item, like in the Level.qml file or in the GameScene.qml file. This is also how it is done in the tutorial link above.



    So i should call the JS file only from one QML file (for example game.qml) in my case?
    And how can my other QML files communicate with the game.qml?


    Felgo Team

    Yes exactly, only include the Game.qml once for example in the Level, or wherever your entityContainer is pointing to. All your children elments can then access the game component with the id. Consider this example:

    GameWindow {
    Game {
    // all entities can access the functions and properties in Game.qml now with gameLogic.
    id: gameLogic

    //.. put the scenes and the EntityManager here


    Also see the Chicken Outbreak or Squaby example, which allow accessing the player instance from all entities similar to this approach.



    Ok so if i understand that correctly, you can access the player through the alias property?
    But in our game we have no entity at beginning (like in chicken outbreack Player with id:player), the entities are generated at runtime, so i really don’t know how to communicate with my game.qml file from runtime generated ones.
    thx for helping so far!!


    Felgo Team

    But you dont store the level-data in each entity right? Instead you have a single Game.qml instance in the whole game. If you need to access other entities, you can use the entityManager.getEntityById() function.



    Yes, that’s how we do it now. But the problem was that the entities have to talk with each other too. And that can’t work only over JS (See Post 4). So now we make a Combo out of JS and QML. (Save some things in QML)


    Felgo Team

    Great you figured that out! Could you post the relevant parts of your current solution here please – it might be interesting for other users having the same problems!


Viewing 11 posts - 1 through 11 (of 11 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
create apps
create games
cross platform
native performance
3rd party services
game network
level editor
easiest to learn
biggest time saving
best support