How to make global value change easier

Godot Version

4.2.1

Question

so i kinda just made this because i needed something and i didnt want to look up a tutorial
var weapon = 0
var weapon1 = “weapon1”
var weapon2 = “weapon2”
var weapon3 = “weapon3”
var weapon4 = “weapon4”
var weapon5 = “weapon5”
var weaponsCommon = [weapon1,weapon2,weapon3,weapon4,weapon5]
var weapon6 = “weapon6”
var weapon7 = “weapon7”
var weapon8 = “weapon8”
var weaponsRare = [weapon6, weapon7,weapon8]
var weapon9 = “weapon9”
var weapon10 = “weapon10”
var weaponsEpic = [weapon9]
var rarity = 0

func _on_back_pressed():
get_tree().change_scene_to_file(“res://main_menu/menu.tscn”)

func _on_buy_1_card_pressed():
rarity = randi_range(0, 100)
print(rarity)

if rarity in range(0,74) :
	print("common")
	weapon = weaponsCommon.pick_random()
	add_weapon(weapon)
elif rarity in range (74,94):
	print("rare")
	weapon = weaponsRare.pick_random()
	add_weapon(weapon)
elif rarity in range(95,100):
	print("epic")
	weapon = weaponsEpic.pick_random()
	add_weapon(weapon)

func add_weapon(weapon):
print(weapon)

if weapon == "weapon1":
	Global.weapon1+= 1
if weapon == "weapon2":
	Global.weapon2+= 1
if weapon == "weapon3":
	Global.weapon3+= 1
if weapon == "weapon4":
	Global.weapon4+= 1
if weapon == "weapon5":
	Global.weapon5+= 1
if weapon == "weapon6":
	Global.weapon6+= 1
if weapon == "weapon7":
	Global.weapon7+= 1
if weapon == "weapon8":
	Global.weapon8+= 1
if weapon == "weapon9":
	Global.weapon9+= 1
if weapon == "weapon10":
	Global.weapon10+= 1

how can i make the global value change easier and not need a specific if statement for each little thing. and if theres a better approach then any input will help. basically the purpose of the code is to add a card. the cards value is stored in a global thing because the cards value needs to be accessed to increase the damage of other stuff. everytime you get a card the number of cards or the variable just goes up by 1

In Global script:

var weapons: Array[int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

In the other script:

func _on_buy_1_card_pressed():
    var rarity = randi() % 100
    if rarity < 74:
        add_weapon(randi() % 5)
    elif rarity < 94:
        add_weapon(randi_range(5, 7))
    else:
        add_weapon(8)

func add_weapon(weapon_index):
    Global.weapons[weapon_index] += 1
1 Like

thanks alot worked well. any idea how to fix this aswell?
if Global.weapons[0] <= 0:
$Button.visible = 0
else:
$Button.visible = 1
$Button.text = str(Global.weapons[0])
if Global.weapon2 <= 0:
$Button2.visible = 0
else:
$Button2.visible = 1
$Button2.text = str(Global.weapon2)
if Global.weapon3 <= 0:
$Button3.visible = 0
else:
$Button3.visible = 1
$Button3.text = str(Global.weapon3)
if Global.weapon4 <= 0:
$Button4.visible = 0
else:
$Button4.visible = 1
$Button4.text = str(Global.weapon4)
if Global.weapon5 <= 0:
$Button5.visible = 0
else:
$Button5.visible = 1
$Button5.text = str(Global.weapon5)
if Global.weapon6 <= 0:
$Button6.visible = 0
else:
$Button6.visible = 1
$Button6.text = str(Global.weapon6)
if Global.weapon7 <= 0:
$Button7.visible = 0
else:
$Button7.visible = 1
$Button7.text = str(Global.weapon7)
if Global.weapon8 <= 0:
$Button8.visible = 0
else:
$Button8.visible = 1
$Button8.text = str(Global.weapon8)
if Global.weapon9 <= 0:
$Button9.visible = 0
else:
$Button9.visible = 1
$Button9.text = str(Global.weapon9)
if Global.weapon10 <= 0:
$Button10.visible = 0
else:
$Button10.visible = 1
$Button10.text = str(Global.weapon10)

i think something like a dictionary would work but ive never really used one

If the buttons are the only direct child nodes, this should work:

for i in range(Global.weapons.size()):
    var button = get_child(i)
    button.visible = Global.weapons[i] > 0
    button.text = str(Global.weapons[i])

You could also make an array for the buttons.

@onready var buttons = [$Button, $Button2, $Button3, $Button4
$Button5, $Button6, $Button7, $Button8, $Button9, $Button10]

func something():
    for i in range(buttons.size()):
        buttons[i].visible = Global.weapons[i] > 0
        buttons[i].text = str(Global.weapons[i])
1 Like

wow thanks alot i used the first one and it worked fine but with way less code. i think it makes alot of sense and i can try and do those things on my own now. i need to get better with for loops and stuff because this is much simpler code. thanks alot I appreciate your help.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.