Learning GDScript
#7
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.

[Image: attachment.php?aid=1501]

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


Attached Files
.png   Shop.png (Size: 252.79 KB / Downloads: 9)
Reply }


Messages In This Thread
Learning GDScript - by MetalRenard - 02-09-2021, 08:55 AM
RE: Learning GDScript - by MetalRenard - 02-09-2021, 10:45 AM
RE: Learning GDScript - by kyonides - 02-10-2021, 05:55 AM
RE: Learning GDScript - by MetalRenard - 02-10-2021, 04:16 PM
RE: Learning GDScript - by MetalRenard - 02-14-2021, 04:24 PM
RE: Learning GDScript - by MetalRenard - 02-19-2021, 08:25 AM
RE: Learning GDScript - by MetalRenard - 02-23-2021, 10:40 AM
RE: Learning GDScript - by MetalRenard - 03-03-2021, 12:54 PM



Users browsing this thread: 1 Guest(s)