Android Question Get coins in the game with B4A

Arta

Member
Hi. I made a game and I want to get 10 coins when the user wins and less than 5 coins when he loses. When he wanted to see an advertisement and get 10 coins. When he downloads the game, he should have 50 coins. How can I fix it with an offline database?
Feel free to create two pages, one main page and one game page. Put two buttons on the game screen, one named and one named lost. If the win button is hit, we will get 10 coins, and if the button is lost, we will lose 5 coins. Put a label and a button on the main page. The number of coins of the user should be shown in the label and advertised when it is hit on the stall. I do not care, when the button is pressed, it must be advertised. Just click on the button to add 10 coins to the coins. I try to advertise myself.
I want my coins to be saved when the user leaves the game. He can not play when the user's coins are finished.
 

Brian Dean

Well-Known Member
Licensed User
Longtime User
How can I fix it with an offline database?

This is a job for a KeyValueStore, which is a simple database. Set it up in the Starter service ...
B4X:
Sub Service_Create
    kvs.Initialize(xui.DefaultFolder, filename)
    If Not(kvs.ContainsKey("coins")) Then        ' Is this the first run?
        kvs.Put("coins", 50)                                    ' Set starting value
    End If
End Sub
On the first run the KVS key "coins" does not exist so it will be created with the value of 50.

When you need to update the "coins" value retrieve the current value from the KVS and change it ...
B4X:
Sub gameWin
    Dim coins As Int = Starter.kvs.Get("coins")
    Starter.kvs.Put("coins", coins + 10) 
End Sub
 
Last edited:
Upvote 0
Top