I dont think youd need a while loop. in your _ready() function, youd spawn cards and give it to the player. The player would then have two options, hit or stand
if they hit, you draw a card and check to see if its under 21. if it isnt, its the dealer's turn to drawn
if they stand, the enemy would do whatever they need to,
When the cards are dealt, you could calculate the max card value that the player would be allowed to have without busting
this would be the equivalent of smth like this (pseudocode ahead)
var max_player_card_value: int = 21
var max_dealer_card_value: int = 21
var player_cards: Array
var dealer_cards: Array
func _ready() -> void:
deal_cards()
max_player_card_value -= player_cards[0]
max_dealer_card_value -= dealer_cards[0]
play_player_round()
func _on_hit_button_pressed() -> void:
new_card = spawn_new_card
if new_card > max_player_card_value:
game_over()
player_cards.append(new_card)
max_player_card_value -= new_card value
play_dealer_round()
func _on_stand_button_pressed() -> void:
play_dealer_round()
func deal_cards() -> void:
new_card = spawn_new_card()
player_cards.append(new_card)
# Spawn a new card, i reuse the new card variable cus im lazy but also i dont think there needs to be a good reason to have two seperate values
new_card = spawn_new_card()
dealer_cards.append(new_card)
func spawn_new_card() -> void:
# Idk what your spawn logic is
func play_dealer_round() -> void:
# Idk what your dealer logic is, you could just have them keep spawning new cards, subtract it from the max_dealer_card_value then transition to the player round
func play_player_round() -> void:
# Show the player buttons
the reason you dont need a while loop is because the game is progressed by the user pressing a max of 2 buttons. The game state only changes when they press these buttons so adding a while loop to wait for it would be unnecesary.