1

I've just started with QML game development using V-Play and coming from java, I stil have problems understanding some basics.

I know how I'd do it in Java, but don't know how it works in QML, so I'll explain it in Java terms:

I have a "base class" with some properties and methods. Then I have modules extending this base class with more specific features. (Like "class Module1 extends BaseModule")

Then I have an object ("ModuleContainer") that contains a Module, but I don't know which one - it's loaded dynamically at runtime. Thus, in Java i'd create a new object like "BaseModule someModule = new Module1()", and can later access it and also replace it (like "someModule = new Module2()").

How could I do that in QML?

I've tried properties, but I haven't found a way to create a new object and then use it. Dynamic creation of objects with an entityManager doesn't quit seem to work for this either.

5
  • did you try to google it? Commented Nov 12, 2015 at 11:06
  • Yes, but I didn't know what to google exactly. Nothing I've found was doing anything close to what I'm trying to achieve, but only assigning a value once to a property or something similar. Commented Nov 12, 2015 at 11:23
  • I would have used "replace" instead of "change" inside that text. That said, the first result of @folibis search keywords is exactly what you are searching for. Start from there. Commented Nov 12, 2015 at 13:47
  • Good suggestion. The first result answers some questions, I've found that components are probably the kind of "object holder" that I'm looking for. Still, a simple example would be helpful. I'll see if it works the way I need it to and will post the code if it does. Commented Nov 12, 2015 at 13:55
  • In the page is linked an actual example. This example. Have a look at it. Commented Nov 12, 2015 at 17:30

1 Answer 1

1

I played around with components a bit and together with a Loader, it's pretty much exactly what I wanted.

Here's my code if anyone else needs something like this:

Basically, I create a baseclass (BaseModule), create two Modules that extend that class (java terms).

In a new class (ModuleSlot), I create two components containing a Module each, that can be dynamically loaded and replaced in the Main code.

Important parts

//define a component and make it accessible from the outside
property Component cmodule1: module1   
Component {
   id: module1
   Module1 {
   }
}

//define a component to hold the component to use (for easier changing)   
property Component dynamicModule: dynamicModuleHolder.cmodule1 

ModuleSlot {
   id:dynamicModuleHolder
}  

//the magic happens here: the defined component is loaded dynamically on runtime, 
when changed, the old one is removed and the new one loaded

Loader {
   sourceComponent:dynamicModule  
}

Full Code

Main.qml:

GameWindow {
id: gameWindow

//licenseKey: "<generate one from http://v-play.net/licenseKey>"

activeScene: scene

width: 640
height: 960


property Component dynamicModule: dynamicModuleHolder.cmodule1
property int activeElement: 1

Scene {


    id: scene

    width: 640
    height: 960



    Loader{
        sourceComponent:dynamicModule
    }

    ModuleSlot {
        id:dynamicModuleHolder
    }

    MouseArea {
           anchors.fill: parent
           onClicked: {
               if(activeElement == 1) {
                   dynamicModule = dynamicModuleHolder.cmodule2
                   activeElement = 2
               } else {
                   dynamicModule = dynamicModuleHolder.cmodule1
                   activeElement = 1
               }
           }
       }



}

}

BaseModule.qml:

Item {
       property int someAttribute: 0
}

Module1.qml/Module2.qml (Module2 only has a different Rectangle)

BaseModule {

    someAttribute: 5 // just to show that Module1 inherits from BaseModule

    Rectangle {
        color: "red"
        width: 50
        height: 50
        x: 200
        y: 200
    }
}

ModuleSlot

Item {

    property Component cmodule1: module1
    property Component cmodule2: module2

    QtObject {
        id: internalSettings
        property color color: "green"
    }

    Component {
        id: module1
        Module1 {

        }
    }

    Component {
        id: module2
        Module2 {

        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.