NayNay_0204 To make things more practical, here's how this should be handled properly. Note that code is not for you to copy paste, it's to study and understand the approach.
class_name Resources
var coins = 0
var iron = 0
var gold = 0
func _init(coins: int, iron: int, gold: int):
self.coins = coins
self.iron = iron
self.gold = gold
func can_afford(amount: Resources):
return self.coins >= amount.coins and self.iron >= amount.iron and self.gold >= amount.gold
func spend(amount: Resources):
self.coins -= amount.coins
self.iron -= amount.iron
self.gold -= amount.gold
var player_resources = Resources.new(1000, 100, 10)
var bettery_level = 0
var battery_cost = [
Resources.new(100, 10, 2),
Resources.new(200, 15, 3),
Resources.new(400, 25, 4),
# etc
]
func _on_battery_button_pressed():
var battery_level_max = battery_cost.size()-1
if bettery_level+1 <= battery_level_max:
var upgrade_cost = battery_cost[bettery_level+1]
if player_resources.can_afford(upgrade_cost):
player_resources.spend(upgrade_cost)
bettery_level += 1
And poof, just like that, 500 lines of code gone, replaced by two dozen lines. Much easier to read and debug, and more importantly, makes tweaking the numbers that affect the gameplay actually quite painless, as opposed to your initial code when such a task becomes a nightmare.
Now adding a new battery level requires merely appending a single line of data to battery_cost
array. Likewise, changing the cost for a particular level can be done simply by altering the three numbers for that level.
The rest is "automatically" taken care of by a few lines of code that contain no repetition whatsoever and read like a short story... courtesy of appropriately structured data ๐