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

MultiplayerDemo

 import Felgo 3.0
 import QtQuick 2.0
 import "scenes"
 import "common"
 import Qt.labs.settings 1.0
 import QtQuick.Dialogs 1.2

 GameWindow {
   id: window

   // You get free licenseKeys from https://felgo.net/licenseKey
   // With a licenseKey you can:
   //  * Publish your games & apps for the app stores
   //  * Remove the Felgo Splash Screen or set a custom one (available with the Pro Licenses)
   //  * Add plugins to monetize, analyze & improve your apps (available with the Pro Licenses)
   //licenseKey: "<generate one from https://felgo.net/licenseKey>"

   title: gameNetwork.user.deviceId + " - " + gameNetwork.user.name

   Settings {
     id: settings
     property alias counterAppInstances: gameNetwork.counterAppInstances
     Component.onCompleted: {

       // use this to reset the counterAppInstances value to 0
       // you might need this, if the app is destroyed forcefully (e.g. from QtCreator with the red quit application button), because then no Component.onDestruction is called and the counter does not get decreased
       // settings.counterAppInstances = 0

       console.log("settings loaded with counterAppInstances value:"+ counterAppInstances+ ", userName: " +gameNetwork.user.name)
       settings.counterAppInstances++
     }
   }

   FelgoGameNetwork {
     id: gameNetwork
     // set this to true for testing a first time app start
     clearAllUserDataAtStartup: true
     gameId: 175
     secret: "multiplayerDemo"
     gameNetworkView: gameNetworkScene.gnView

     user.deviceId: generateDeviceId()

     property int counterAppInstances: 0//: counterAppInstances

     function generateDeviceId() {
       // on mobile devices, no 2 app instances can be started at the same time, thus return the udid there
       if(system.isPlatform(System.IOS) || system.isPlatform(System.Android) || system.isPlatform(System.WindowsPhone)) {
         return system.UDID
       }
       // this means the app was started on the same PC more than once, for testing a multiplayer game
       // in this case, append the counterAppInstances value to the deviceID to have 2 separate players
       if(counterAppInstances > 1) {
         return system.UDID + "_" + (counterAppInstances) % multiplayer.playerCount
       } else {
         return system.UDID
       }

       // TODO: add button where to set the counterAppInstances on PC, then start from this number again
       // TODO: add start quick match button
     }
   }

   FelgoMultiplayer {
     id: multiplayer
     playerCount: 4
     startGameWhenReady: false
     gameNetworkItem:gameNetwork
     multiplayerView: matchmakingScene.mpView
     maxJoinTries: 5
     fewRoomsThreshold: 3
     joinRankingIncrease: 200
     enableLateJoin: true // allow joining a running match after it was started (if the match has non-human (AI) players to fill the game

     // TODO: set your own multiplayer appKey after you created your own game here: http://cloud.felgo.net/
     appKey: "03df145d-0dac-46ab-aea2-79067d03dcd2"
     // TODO: set your own multiplayer pushKey after you created your own game here: http://cloud.felgo.net/
     pushKey:"93e69486-bc59-4d25-8109-778c107a2558"
     // NOTE: do NOT use these demo keys for publishing your game, we might remove the demo apps in the future!
     // instead, use your own ones from http://cloud.felgo.net/

     onGameStarted: window.state = "game"
   }

   // menu scene
   MenuScene {
     id: menuScene

     onMenuButtonPressed: {
       console.log("PRESSED BUTTON "+ button)
       switch (button){
       case "game":
         window.state = "game";
         break;
       case "matchmaking":
         multiplayer.showMatchmaking()
         window.state = "multiplayer";
         break;
       case "quick" :
         multiplayer.joinOrCreateGame()
         multiplayer.showMatchmaking()
         window.state = "multiplayer";
         break;
       case "invites":
         multiplayer.showInvitesList()
         window.state = "multiplayer";
         break;
       case "inbox":
         multiplayer.showInbox()
         window.state = "multiplayer";
         break;
       case "friends":
         multiplayer.showFriends()
         window.state = "multiplayer";
         break;
       case "increase_score":
         gameNetwork.reportRelativeScore(1)
         break;
       default :
         window.state = button
       }
     }
   }

   // game scene
   GameScene {
     id: gameScene
     onBackButtonPressed: window.state = "menu"
   }

   // matchmaking scene
   MultiplayerScene {
     id: matchmakingScene
     onBackButtonPressed: window.state = "menu"
   }

   GameNetworkScene{
     id: gameNetworkScene
     onBackButtonPressed: window.state = "menu"
   }

   // menuScene is our first scene, so set the state to menu initially
   state: "menu"
   activeScene: menuScene

   // state machine, takes care reversing the PropertyChanges when changing the state, like changing the opacity back to 0
   states: [
     State {
       name: "menu"
       PropertyChanges {target: menuScene; opacity: 1}
       PropertyChanges {target: window; activeScene: menuScene}

     },
     State {
       name: "game"
       PropertyChanges {target: gameScene; opacity: 1}
       PropertyChanges {target: window; activeScene: gameScene}
     },
     State {
       name: "multiplayer"
       PropertyChanges {target: matchmakingScene; opacity: 1}
       PropertyChanges {target: window; activeScene: matchmakingScene}
     },
     State {
       name: "gn"
       PropertyChanges {target: gameNetworkScene; opacity: 1}
       PropertyChanges {target: window; activeScene: gameNetworkScene}
     }
   ]

   Component.onDestruction: {
     settings.counterAppInstances--
Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded