hi, I need help to create coin system in game, every coin I collect keeps counting even after losing and restarting level and ability to buy item from shop with coin and making each type of Coins with different value, Thanks.
If I were doing this I would firstly have a separate coin scene. That would control the value of the coin, queue_free when collected, the spinning etc or whatever you are doing with your coins. These I would add to my game scene appropriately.
I would also have a coin_manager autoload (you may already have a player_manager or game_manager that you could use insead).
The coin manager would have the coin amount at the beginning of the level, and the current coin amount for adding coins collected in the game.
For instance:
var total_collected_coins: int = 0
var accumulated_coins: int = 0
In the coin scene, when your animation (or whatever) is completed for a collected coin, increment the accumulated_coins total. If the level is completed, and only then, add the accumulated coins to the total collected coins. If the level is not completed, then you will restart the level with your original coin total only.
If the coins have different values, then you would not count the coins, but add the value of the coins. So I suppose your variables might be better named something like:
var bank_balance: int = 0
var earned_this_level: int = 0
thx but the thing is i didnt make any code for counting coins heres my Coin and gamemanager script
it will mean alot if u give me the whole code xD thx
Do you know about singletons (or autoloads)?
Create a script, not in the tree or in a node, but in filesystem itself and have it extend Node. Then you can add some variables in there like bank balance and coins earned.
In the Editor, go to project settings and click on the tab called ‘Globals’. These are loaded before any nodes are loaded and are available globally. Because they do not sit on any particular node, when you change scenes the autoloads remain. You can include logic in them but not _ready or _process.
So lets say you made one called player_wallet.gd, and gave it a name of PlayerWallet. In it you had a variable called bank_balance. Then to access the bank balance from any node, any script anywhere in your game you would just use PlayerWallet.bank_balance and this applies to anything you set up in the script. Here is an example for you:
Script: player_wallet.gd
extends Node
# Manages players wallet
var bank_balance: int = 0
var coins_total_value_earned_this_level: int = 0
# Call this when a coin is collected
func coin_collected(coin_value: int) -> void:
coins_total_value_earned_this_level += coin_value
# Called when level started or restarted
func reset_coins_earned() -> void:
coins_total_value_earned_this_level = 0
# Called when level completed
func consolidate_bank_balance():
bank_balance += coins_total_value_earned_this_level
reset_coins_earned()
# Call whenever you need coins earned this level (perhaps in your UI)
func get_coins_earned_this_level() -> int:
return coins_total_value_earned_this_level
# Call whenever you need the bank balance, say in your shop, on a menu screen...
func get_bank_balance() -> int:
return bank_balance
# etc etc. Add as much functionality as you need
Then anywhere you call these like this:
var current_balance = PlayerWallet.get_bank_balance()
PlayerWallet.coin_collected(5)
etc
Hope that helps. Also, have a read of this in the Docs too:
Good luck with your game
Thanks man, it will be a good practice for me