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

Forums

OverviewFelgo 1 Support › QML properties?

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

    Schustiii

    Hallo,
    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

    #4076

    Christian
    Felgo Team

    Hi,
    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:
    http://doc-snapshot.qt-project.org/4.8/declarative-tutorials-samegame-samegame2.html

    Cheers,
    Chris

    PS: Please write in english so all users can understand

    #4080

    Schustiii

    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.
    thx

    #4082

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

    #4086

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

    #4087

    Schustiii

    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?

    #4089

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

    #4091

    Schustiii

    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!!

    #4092

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

    #4096

    Hochi

    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)

    #4099

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

    Cheers,
    Chris

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