While loop freezes the game

Godot Version

v4.3.stable.arch_linux

Question

I have a menu with 2 cards that can have a random upgrade attached. I added a function that randomizes the upgrade that each card has if they have the same upgrade value (so the cards are not equal) and added it in the “_ready()” function, with an “if” statement so that the function is only executed if the cards have the same value.

This is the code:

func shuflle_cards() -> void:
	while %Upgrade_Card_01.chosen_upgrade == %Upgrade_Card_02.chosen_upgrade:
			%Upgrade_Card_01.shuffle_card()
			%Upgrade_Card_02.shuffle_card()

func _ready() -> void:
	if %Upgrade_Card_01.chosen_upgrade == %Upgrade_Card_02.chosen_upgrade:
		print("EQUAL CARDS")
		shuflle_cards()
	print("shuffled!")

Heres also the “shuffle_card()” function in the cards:

func shuffle_card() -> void:
	chosen_upgrade = posible_upgrades.pick_random()

When the cards are equal, the game somehow freezes, I suspect it has something to do with the “while” loop in the “shuffle_cards()” function is looping infinitely.

Is there any way to stop the infinite looping or convert it into a much safer “for” loop? I appreciate any help.

PD: I am not a native English speaker, I apologize in advance for any misspelt word or weird sentence

how many possible_upgrades are there? Does the game freeze forever?

There are 4 possible_upgrades, it is an array that stores the upgrades that are enabled, being able to add more upgrades or eliminate upgrades

I have never seen the game unfreezing so it technically freezes forever.

Is there any chance it’s not actually frozen and there is a stack trace in the editor? Does it point to a line in a script?

Could you only shuffle one of the two cards? I would be surprised if this helped, but it wouldn’t hurt.

func shuflle_cards() -> void:
	while %Upgrade_Card_01.chosen_upgrade == %Upgrade_Card_02.chosen_upgrade:
		%Upgrade_Card_01.shuffle_card()
		## %Upgrade_Card_02.shuffle_card() ## delete this?

What it looks like, is you shuffle is failing you. They are always equal after each shuffle.

Making pick_random() is not working correctly. Always picking the same random card.

There are no stack traces that I could find

You can stick a program counter in the while loop, to break it after certain number of tries. Which I would recommend even after you fix it.

Could you paste your card’s entire script?

Did it. It stopped the looping but the cards were still equal

Are there any alternatives to “pick_random()”?

Fixed it!, i just changed the “shuffle_card():” function to this:

func shuffle_card() -> void:
	posible_upgrades.shuffle()
	chosen_upgrade = posible_upgrades[1]

Well done on fixing your problem :slight_smile: .
You may consider changing the last line to
chosen_upgrade = posible_upgrades.front()
as this returns the first element of an array or a null value if it’s empty.
Array.front()
In your code, the “1” is an index of an array, which points to the 2nd element of an array (arrays start counting indices from 0). If you ever have less than 2 elements in the array, that line would throw you an out of bounds error. My suggested change keeps it a little safer.

1 Like

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