fix: __guard_counter already defined in the scope
Please check if the PR fulfills these requirements:
- [x] The commit message follows our guidelines.
- For bug fixes and features:
- [x] You tested the changes.
Related issue (if applicable): #961
New feature or change
What is the current behavior?
Currently the app will crash if you have more then one while loop in the script_text due to:
The generate code (script_text) looked like this:
func move_to_bottom():
var __guard_counter := 0
while cell.x < board_size.x - 1:
__guard_counter += 1
if __guard_counter > 100:
break
cell.x += 1
var __guard_counter := 0
while cell.y < board_size.y -1:
__guard_counter += 1
if __guard_counter > 100:
break
cell.y += 1
What is the new behavior?
Change will define the variable __guard_counter just once and for other while loops it will be set back to 0.
Now the code looks like:
func move_to_bottom():
var __guard_counter := 0
while cell.x < board_size.x - 1:
__guard_counter += 1
if __guard_counter > 100:
break
cell.x += 1
__guard_counter = 0
while cell.y < board_size.y -1:
__guard_counter += 1
if __guard_counter > 100:
break
cell.y += 1
Other information
I have thought about this again, I think that with nested while loops this will not work.
Probably it would be better to add an counter/prefix to the __guard_counter to have a separate variable for each while loop.
__guard_counter1, __guard_counter2 and so on.
I will provide a commit with this change later during the day.
Thank you very much for taking the time to fix this issue! Adding a suffix or a prefix to the variable sounds like the perfect way to go about it.
Yes, without the suffix it would look like:
func move_to_bottom():
var __guard_counter := 0
while cell.x < board_size.x - 1:
__guard_counter += 1
if __guard_counter > 100:
break
cell.x += 1
__guard_counter = 0
while cell.y < board_size.y -1:
__guard_counter += 1
if __guard_counter > 100:
break
cell.y += 1
With the suffix (https://github.com/GDQuest/learn-gdscript/pull/999/commits/809408e3d90e55bcc7eb960feb1659ee4b0b4b42) :
func move_to_bottom():
var __guard_counter0 := 0
while cell.x < board_size.x - 1:
__guard_counter0 += 1
if __guard_counter0 > 100:
break
cell.x += 1
var __guard_counter1 := 0
while cell.y < board_size.y -1:
__guard_counter1 += 1
if __guard_counter1 > 100:
break
cell.y += 1