I am using Godot 3.5
Hi I am working on a card purchase and select system.
The idea is that if I purchase a card it should show “selected” if there are no other cards purchased. And if there is any card already purchased than it should show “select”. So I can click on the “select” so it will become “selected” and any other cards which was “selected” will get deselected and should show “select”.
This is the best I can explain about it. Idk how to achieve this. I tried using chat gpt. And it works too but there is a little problem with the code. If there is a card already “selected” and if I purchase and card than if I click “select” it freeze my game.
I tried again and again to generate best response from chat gpt but nothing changed. Can you please help me .
This is the code I got from chat gpt :-
extends Node
onready var scroll_container = $ScrollContainer
onready var button = $Button
onready var popup = $Popup
onready var coin_manager = get_node(“/root/CoinManager”)
var card_prices = [600, 200, 300, 400]
var purchased_cards =
var selected_card = -1
func _ready():
update_button_text()
popup.get_node(“Panel/OK”).connect(“pressed”, self, “_on_OK_pressed”)
button.connect(“pressed”, self, “_on_Button_pressed”)
func _on_OK_pressed():
popup.visible = false
func _on_Button_pressed():
if button.text == “Select”:
select_card()
elif button.text.begins_with("Select - "):
var price = int(button.text.replace("Select - ", “”))
if coin_manager.coins >= price:
coin_manager.remove_coins(price)
add_to_purchased_cards()
else:
show_popup(“Not enough coins”)
func show_popup(message):
popup.get_node(“Panel/Label”).text = message
popup.popup_centered()
func _process(delta):
update_button_text()
func update_button_text():
var largest_card_index = get_largest_card_index()
if largest_card_index != -1:
if largest_card_index in purchased_cards:
if largest_card_index == selected_card:
button.text = “Selected”
else:
button.text = “Select”
else:
if card_prices[largest_card_index] == 0:
button.text = “Select”
else:
button.text = "Select - " + str(card_prices[largest_card_index])
func select_card():
var largest_card_index = get_largest_card_index()
if largest_card_index != -1 and largest_card_index in purchased_cards:
Deselect the previously selected card, if any
if selected_card != -1 and selected_card != largest_card_index:
deselect_card(selected_card)
Select the new card
selected_card = largest_card_index
button.text = “Selected”
update_button_text()
func deselect_card(card_index):
scroll_container.card_nodes[card_index].text = “Select”
func get_largest_card_index() → int:
var largest_card_index = -1
var largest_card_scale = 0
for i in range(scroll_container.card_nodes.size()):
var card_scale = scroll_container.card_nodes[i].rect_scale.x
if card_scale > largest_card_scale:
largest_card_scale = card_scale
largest_card_index = i
return largest_card_index
func add_to_purchased_cards():
var largest_card_index = get_largest_card_index()
if largest_card_index != -1:
purchased_cards.append(largest_card_index)
card_prices[largest_card_index] = 0
Automatically select the card if no other cards are purchased
if purchased_cards.size() == 1:
selected_card = largest_card_index
button.text = “Selected”
else:
button.text = “Select”
update_button_text()