First Android Game - Part 9 - Collect 2+ Coins July 25, 2023
This is part 9 of making my first Android Game. If you missed part8, you can find it here There appears to be issues when collecting 2+ coins. You can see the example below the when the player collides with two coins - $25 and $100 - there are two coin text that gets spawned. However, only the $25 got added to the coin amount. Also another problem is the treasure ($100) and triple coin ($25) have layering issues. Let's fix the layering issue first. All 3 types of coins had layering of -3. I changed the triple coin to -4 and treasure to -5 The layering issue is fixed. You can see the triple coin is on top of the treasure chest I put Debug Logs to see if the coins are being sent properly From both the game and Debug Logs I can see that both coins were registered and even added the $25 coin first and then reverted back to the $5 coin I placed some breakpoints at where SetCustomProperties is called and where OnPlayerPropertiesUpdate is called.
What I thought the logic was when colliding with the coin is. Say current amount is $180

1.Collide with 1st Coin

2.SetCustomProperties - add the 1st coin say it's $25

3.OnPlayerPropertiesUpdate() - updates scoreboard - add 1st coin value to current amount. $180+ $25 =$205

4.Collide with 2nd Coin

5.SetCustomProperties - add the 2nd coin say it's $5

6.OnPlayerPropertiesUpdate() - updates scoreboard - add 2nd coin value to current amount. $205 + $5 = $210

7.Result is $210

What is actually happening

1.Collide with 1st Coin

2.SetCustomProperties - add the 1st coin say it's $25

3.Collide with 2nd Coin

4.SetCustomProperties - add the 2nd coin say it's $5

5.OnPlayerPropertiesUpdate() - updates scoreboard - add 1st coin value to current amount $180 + $25 = $205

6.OnPlayerPropertiesUpdate() - updates scoreboard - add 2nd coin value to current amount $180 + $5 = $185

7.Result is $185

So it seems like there is a delay between SetCustomProperty and OnPlayerPropertiesUpdate()
I did a test where in the GetCoin() method of PlayerInformation.cs. I replaced that code by returning a static int variable called testcoin In AddCoin(), I would use the testcoin variable to add the coin result This would add the coin correctly for a player. However, it would add the same amount of coin to the other player. The actual coin amount is not sent across the network. This is expected because of the static variable, so when the other player collides with the coin, it will send the coin amount across but because of the static variable, the local player will think their partner has the same coin amoun. However, doing this shows me that CustomProperties was delayed in getting the coin result. So I need to find a way to have GetCoin() return the most update to date result and send the coin amount across the network when the player collides with 2+ coins Since GetCoin() is causing trouble. I thought I can create my own add coin method. In ScoreManager.cs, I would create an AddCoinToPlayer() method that takes in the new coin amount to to display on the scoreboard and the Player In Player.cs, the CollectCoin() method would check if view.IsMine, then add new amount of coins to current coins and call AddCoinToPlayer(). However, this only added the coin amount to view.IsMine player, because AddCoinToPlayer() method is local. If I moved that part of the code out of view.IsMine, the the LocalPlayer would be whoever was playing on that device so the coin amount would be added to each player locally It then occurred to me. When AddCoin() calls GetCoin(), that's when the issue occurs. Why don't I just call SetCoin() instead? Voila, the coins are updating correctly when colliding with 2+ coins This is part 9. Checkout my next blog part 10 here
Recent blogs See all blogs