02-23-2021, 10:40 AM
(This post was last modified: 03-01-2021, 02:25 AM by DerVVulfman.)
Today's challenge: Learn how to make menus
Task: Make a simple shop menu for the player to spend their cash on upgrades. Make the buttons clickable and, when clicked, check if they have enough cash then level up the appropriate stat.
First, I set up the buttons, their tags and all that. Here's the code I wrote to make 2 of them function:
Task: Make a simple shop menu for the player to spend their cash on upgrades. Make the buttons clickable and, when clicked, check if they have enough cash then level up the appropriate stat.
First, I set up the buttons, their tags and all that. Here's the code I wrote to make 2 of them function:
Code:
extends Control
var cost = 100
var cost_factor = 0.8
var bcost = 0.0
var tcost = 0.0
func _process(delta):
# Set level display
$MCon/VBCon/ThrustCon/TLevel.text = str(GlobalPlayer.speed_lvl)
$MCon/VBCon/BoostCon/BLevel.text = str(GlobalPlayer.boost_lvl)
$MCon/VBCon/MoneyCon/MALabel.text = str(GlobalPlayer.money)
# Set price display
bcost = GlobalPlayer.boost_lvl * cost * cost_factor
tcost = GlobalPlayer.speed_lvl * cost * cost_factor
$MCon/VBCon/ThrustCon/TCost.text = " Upgrade cost: " + str(tcost)
$MCon/VBCon/BoostCon/BCost.text = " Upgrade cost: " + str(bcost)
# testing purposes only, to add 100 money to player's bank
func _on_DMoney_pressed():
GlobalPlayer.money += 100
# Booster, check cost, if capable, upgrade level and take away money
func _on_BButton_pressed():
if GlobalPlayer.money > bcost:
GlobalPlayer.boost_lvl += 1
GlobalPlayer.money -= bcost
#thruster
func _on_TButton_pressed():
if GlobalPlayer.money > tcost:
GlobalPlayer.speed_lvl += 1
GlobalPlayer.accel_lvl += 1
GlobalPlayer.money -= tcost