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

MultiplayerDemoSimple

 import Felgo 3.0
 import QtQuick 2.0
 import Qt.labs.settings 1.0
 import QtQuick.Controls 1.1

 // This simple demo searches for connected players
 // It continues to send PING and PONG messages between two connected devices
 GameWindow {
   id: window

   // show user deviceId and name in window title to quickly see which application instance belongs to which user
   title: gameNetwork.user.deviceId + " - " + gameNetwork.user.name

   Settings {
     id: gameSettings

     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
       // gameSettings.counterAppInstances = 0
       console.log("settings loaded with counterAppInstances value:"+ counterAppInstances+ ", userName: " +gameNetwork.user.name)
       gameSettings.counterAppInstances++
     }
   }

   FelgoGameNetwork {
     id: gameNetwork
     gameId: 285
     secret: "AmazinglySecureGameSecret"
     multiplayerItem: multiplayer

     user.deviceId: generateDeviceId()
     clearAllUserDataAtStartup: true
     property int counterAppInstances: 0

     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 > 0) {
         //console.debug("counterAppInstances is >1:", counterAppInstances)
         return system.UDID+"_" + counterAppInstances % multiplayer.playerCount
       } else {
         return system.UDID
       }
     }
   }

   FelgoMultiplayer {
     id: multiplayer
     playerCount: 4
     gameNetworkItem: gameNetwork
     multiplayerView: gameScene.multiplayerView
     startGameWhenReady: true
     appKey: "dd7f1761-038c-4722-9f94-812d798cecfb"
     // 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/
   }

   GameScene {
     id: gameScene
   }

   // decrease the amount of connected apps when they are closed
   Component.onDestruction: {
     gameSettings.counterAppInstances--
Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded