Changing the value of an export variable from the script

Godot Version

4.6.3

Question

extends Node2D
class_name Card

@export var cost: int
@export var click_value_change: int = 0
@export var graphic: Texture2D
@export var guh_value_change: int = 0
@export var double_guh: bool = false

var coverage: bool

func _ready() → void:
$Sprite2D.texture = graphic

func _on_area_2d_input_event(viewport: Node, event: InputEvent, shape_idx: int) → void:
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT:
if coverage == true:
if event.pressed:
if Wallet.money >= cost:
purchase_card()
update_cost()
card_removal()

func _on_area_2d_mouse_entered() → void:
coverage = true

func _on_area_2d_mouse_exited() → void:
coverage = false

func card_removal() → void:
queue_free()
Carddealer.card_count -= 1

func purchase_card() → void:
Wallet.money -= cost
if double_guh == true:
Wallet.click_value *= 2
Wallet.click_value += click_value_change
Wallet.guh_value += guh_value_change

func update_cost() → void:
cost *= 2

So basically I have this script that other cards in my game just inherit from, the cost variable is an export variable, and my idea is that I want the cost of the cards to double everytime they are purchased, so next time the player has to spend more money. But it doesnt seem to be working, im not getting an error or anything, its just simply not changing the cost at all.

The script is difficult to read without the GDScript formatting, so I may be wrong, but I don’t see, in that script, anywhere that gives ‘cost’ a value at all. Deducting ‘cost’ from ‘wallet’ (ie: zero…), then doubling ‘cost’ (still zero…) looks to be what the script is doing. Does this help..?

oh, im setting the specific value of cost for each card, unless thats not actually giving it a value

The cost of this specific card is doubled, then this specific card is deleted (queue_free inside of card_removal). The next card you spawn in will use what ever value the scene is assigned, not an old deleted card’s value.

If you shared a resource between cards you could update that resource without destroying it, and generally having a lot of export variables is a good sign you may want to move them into a resource.

The first thing I would do is put a print statement in the update cost function. Then you know if it got called and if it is reading the right value.

I did do that, i think the person above is correct, its calling the function and updating the cost, but the card removal function is sing queue_free() which destroys the card and the updated cost