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

LevelStore

Allows to monetize user-generated levels with in-app purchases. More...

Import Statement: import Felgo 3.0
Inherits:

Store

Properties

Signals

Methods

Detailed Description

The LevelStore allows to monetize your user-created levels with in-app purchases. It also allows to award your best level creators to give them an incentive to create good levels.

The LevelStore makes it very easy to connect your level sharing with the LevelEditor component with in-app purchases with these main use cases:

  • Call buyLevel() to purchase & download the level for a player.
  • Call buyCurrencyPack() to let the player buy new playerCredits with the native in-app purchase plugin for iOS & Android by Felgo.
  • To reward your players with new currency, call giveCurrency(). You can reward players for example when they start your game to give an incentive for returning, or if their published level reaches a certain number of downloads. For more details see the Felgo Crash Course Lesson 8 about How to Motivate Players to Create Levels.
  • During development, the functions resetCurrency() and clearAllBoughtLevels() are useful to test the level purchase functionality in your game.

Example Usage

For a tutorial about monetizing user-generated levels see Felgo Crash Course Lesson 8 - How to benefit from user-generated content in your game with Felgo Level Store.

The following example shows a minimal example of the interaction between LevelEditor, LevelStore, a LevelScene and a ShopScene.

 import Felgo 3.0

 GameWindow {

   Component.onCompleted: {
     // give the player 1 credit every time starting the game to motivate to return
     levelStore.giveCurrency(1)
   }

   // use the LevelEditor to create new levels, save them and later on publish the levels
   // for showing a list of user-generated levels, call loadCommunityLevels()
   LevelEditor {
     id: levelEditor
   }

   LevelStore {
     id: levelStore

     currencies: [
       Currency { id: moneyCurrency; itemId: "currency_money_id"; name: "money"; }
     ]

     currencyPacks: [
       CurrencyPack {
         id: money2Pack
         itemId: "money_pack_2_id"
         name: "2 Coins"
         currencyId: moneyCurrency.itemId
         currencyAmount: 2
         purchaseType:  StorePurchase { id: money2Purchase; productId: money2Pack.itemId; }
       }
       // you can add multiple packs here, e.g. for 5 coins, 10 coins and 50 coins
       // define the € or $ price for them in the Google Play Store Backend or iTunes Connect
     ]

     onInsufficientFundsError: {
       // notify the player he has not enough playerCredits to buy this level and to go to the shop
       nativeUtils.displayMessageBox("Not enough Credits to buy this level", "You do not have enough credits to buy this level. You can go to the shop and purchase more levels.")
     }
   }

   LevelScene {

     onBuyLevelClicked: {

       // the LevelScene contains a list of levels and emits a buyLevelClicked(levelData) signal if one was selected for buying
       // if the player has enough playerCredits, the level is purchased (levelBoughtSuccessfully() is emitted) and downloaded (levelDownloadedSuccessfully() is emitted)
       // if he has not enough playerCredits, insufficientFundsError() is emitted in LevelStore
       levelStore.buyLevel(levelData)
     }
   }

   ShopScene {

     Text {
       // in the ShopScene, show the current credits of the player
       text: "Current Credits: " + levelStore.playerCredits
     }

     onPurchaseCurrencyPack: {

       // the ShopScene contains a list of all currencyPacks with a button to buy a Package
       // in this example we only have 1 pack which will increase the playerCredits by 2 after the purchase
       // you can set the amount of € or $ how much this currencyPack should cost in Google Play or iTunes Connect (see the Store plugin for more details)
       levelStore.buyCurrencyPack(money2Pack.itemId)
     }

     // ... shop UI
   }

   // required for user-generated levels
   FelgoGameNetwork {
     id: gameNetwork
   }

   // ... other scenes like the GameScene or OptionsScene

 }

A full example game with a LevelStore, LevelEditor, FelgoGameNetwork, ShopScene & LevelScene is available in the Stack With Friends Demo.

It contains a LevelStore implementation with a shop UI shown in this image:

The level selection in Stack With Friends Demo looks like that:

Dependencies & Build Server Usage

The LevelStore requires a LevelEditor in your game - set it with the levelEditorItem property.

The LevelStore is an extended component of the Store plugin for in-app purchases. In addition to the Store functionality, it adds the possibilty to monetize user-generated levels with in-app purchases with the extended functions buyLevel(), giveCurrency(), resetCurrency() and clearAllBoughtLevels(). To use the LevelStore with Felgo Build Server, add the following line to your config.json file:

 "plugins": [ "store" ]

Testing the LevelStore

For testing the LevelStore on your iOS or Android devices with Felgo Build Server, see the section Set-up & Test In-App Purchases in the Soomla Plugin.

Testing on desktop platforms works with all functions fully supported. Also, the LevelStore persistently saves the playerCredits the user had at exiting the app. The only limitiation on desktop is the following: A call of buyCurrencyPack() increases the playerCredits without real billing as it is done on iOS or Android, because there is no App Store on desktop. Thus to simulate a call of onInsufficientFundsError, you would need to call it yourself.

Property Documentation

costPerLevel : int

The default price for a level. If playerCredits is >= costPerLevel then a call of buyLevel() will succeed, otherwise onInsufficientFundsError will be called.

The default value is 1.

To set a custom price per level, add a price key to the LevelData::levelMetaData.

See also playerCredits and buyLevel().


levelEditorItem : variant

Set this property to the id of LevelEditor.

By default, it will be set to the id levelEditor if one is found in the main qml file, or it is undefined if no such id is found.

Note: It is required that a LevelEditor component exists for the LevelStore component so buyLevel() works.

This is an example how to use it in your main qml file:

 import Felgo 3.0

 GameWindow {

   LevelStore {
     id: levelStore

     levelEditorItem: myLevelEditor
   }

   LevelEditor {
     id: myLevelEditor
   }

   // ... Scenes, FelgoGameNetwork and other items
 }

playerCredits : int

This read-only property contains the player's credits of the currency entered in Store::currencies.

You can increase playerCredits by calling giveCurrency() or buyCurrencyPack(). The playerCredits decrease by calling buyLevel(). To set the playerCredits to 0 call resetCurrency().

Internally, it is bound to the Currency::balance property of the Store plugin.

Use this property for example to display the current player credits in a Text element:

 Text {
   // in the ShopScene, show the current credits of the player
   text: "Current Credits: " + levelStore.playerCredits
 }

See also Store::insufficientFundsError, buyLevel(), buyCurrencyPack(), giveCurrency(), and resetCurrency().


Signal Documentation

levelBoughtSuccessfully(variant = levelData)

This handler is called after a successful purchase with buyLevel(). For a successful purchase, the playerCredits must be >= costPerLevel.

After onLevelBoughtSuccessfully was called, either onLevelDownloadedSuccessfully or onLevelDownloadedError will be called.

Note: The corresponding handler is onLevelBoughtSuccessfully.


levelDownloadedError(variant = levelData, variant = errorData)

This handler is called after buyLevel() and then onLevelBoughtSuccessfully was called and the level then could not be downloaded to LevelEditor::downloadedLevels.

This might happen if the internet connection got lost after the level purchase. So you can notify the user to check the internet connection if this handler is called. The level will be tried to re-download again after the app is closed and then started again.

Note: The corresponding handler is onLevelDownloadedError.

See also levelDownloadedSuccessfully.


levelDownloadedSuccessfully(variant = levelData)

This handler is called after buyLevel() and then onLevelBoughtSuccessfully was called and the level then got successfully downloaded to LevelEditor::downloadedLevels.

You can notify the user after the level was downloaded that it is now available offline and the purchase was successful.

Note: The corresponding handler is onLevelDownloadedSuccessfully.

See also levelDownloadedError.


Method Documentation

buyCurrencyPack(currencyPackItemId)

Purchase new playerCredits with the native in-app purchase dialog for iOS or Android.

On desktop platforms this call will increase the playerCredits without any payment processing, because there is no App Store available.

The currencyPackItemId is the string value of your Store::currencyPacks itemId. See the following example how to use this function:

 import Felgo 3.0

 GameWindow {

   LevelStore {
     id: levelStore

     currencies: [
       Currency { id: moneyCurrency; itemId: "currency_money_id"; name: "money"; }
     ]

     currencyPacks: [
       CurrencyPack {
         id: money2Pack
         itemId: "money_pack_2_id"
         name: "2 Coins"
         currencyId: moneyCurrency.itemId
         currencyAmount: 2
         purchaseType:  StorePurchase { id: money2Purchase; productId: money2Pack.itemId; }
       }
       // you can add multiple packs here, e.g. for 5 coins, 10 coins and 50 coins; define the € or $ price for them in the Google Play Store Backend or iTunes Connect
     ]

     onInsufficientFundsError: {
       // notify the player he has not enough playerCredits to buy this level and to go to the shop
       nativeUtils.displayMessageBox("Not enough Credits to buy this level", "You do not have enough credits to buy this level. You can go to the shop and purchase more levels.")
     }
   }

   ShopScene {

     onPurchaseCurrencyPack: {

       // the ShopScene contains a list of all currencyPacks with a button to buy a Package
       // in this example we only have 1 pack which will increase the playerCredits by 2 after the purchase
       // you can set the amount of € or $ how much this currencyPack should cost in Google Play or iTunes Connect (see the Store plugin for more details)
       levelStore.buyCurrencyPack(money2Pack.itemId)
     }
   }

   // ... other items like LevelEditor & FelgoGameNetwork + other scenes like GameScene or OptionsScene

 }

buyLevel(levelData)

Starts a purchase of the provided levelData. If levelData contains a price key in the JSON map, this price will be deduced from the playerCredits. If no price is entered per level, the default costPerLevel is used.

If playerCredits are greater than the level price, the purchase will be successful and a onLevelBoughtSuccessfully is emitted followed by a onLevelDownloadedSuccessfully or onLevelDownloadedError. If the playerCredits are lower than the level price onInsufficientFundsError is called.

The levelData originates from your LevelSelectionList. This is where you show the available levels in your game. This is an example how to call this function from a LevelSelectionList:

 import Felgo 3.0

 GameWindow {

   LevelStore {
     id: levelStore

     currencies: [
       Currency { id: moneyCurrency; itemId: "currency_money_id"; name: "money"; }
     ]

     currencyPacks: [
       CurrencyPack {
         id: money2Pack
         itemId: "money_pack_2_id"
         name: "2 Coins"
         currencyId: moneyCurrency.itemId
         currencyAmount: 2
         purchaseType:  StorePurchase { id: money2Purchase; productId: money2Pack.itemId; }
       }
       // you can add multiple packs here, e.g. for 5 coins, 10 coins and 50 coins
       // define the € or $ price for them in the Google Play Store Backend or iTunes Connect
     ]

     onInsufficientFundsError: {
       // notify the player he has not enough playerCredits to buy this level and to go to the shop
       nativeUtils.displayMessageBox("Not enough Credits to buy this level", "You do not have enough credits to buy this level. You can go to the shop and purchase more levels.")
     }
   }

   LevelScene {

     onBuyLevelClicked: {

       // the LevelScene contains a list of levels and emits a buyLevelClicked(levelData) signal if one was selected for buying
       // if the player has enough playerCredits, the level is purchased (levelBoughtSuccessfully() is emitted) and downloaded (levelDownloadedSuccessfully() is emitted)
       // if he has not enough playerCredits, insufficientFundsError() is emitted in LevelStore
       levelStore.buyLevel(levelData)
     }
   }

 }

See also playerCredits and giveCurrency().


clearAllBoughtLevels()

Call this function during development to clear all downloaded levels to test the buying process with buyLevel().

Alternatively, you could also call LevelEditor::clearAllDownloadedLevels().


giveCurrency(amount)

Increase the playerCredits by amount. Use this function to award the player for actions in the game like starting the application once a day or reaching an achievement.

For more details see the Felgo Crash Course Lesson 8 about How to Motivate Players to Create Levels.

Note: By calling giveCurrency the user gets more playerCredits without the need to buy them with buyCurrencyPack(). As such you are not earning money and giving the playerCredits away "for free" - so consider wisely how you spend your gifted credits to not ruin your game ecosystem. If it is very easy for players to get playerCredits, they will not buy them.

See also buyLevel(), buyCurrencyPack(), resetCurrency(), and playerCredits.


resetCurrency()

Call this function during development to reset playerCredits to 0. If playerCredits are 0 you can then test the game flow when the player has insufficient funds when calling buyLevel().

See also buyLevel(), giveCurrency(), and Store::insufficientFundsError.


Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded