Skip to content
  • Sponsor GDQuest/learn-gdscript

  • Notifications You must be signed in to change notification settings
  • Fork 182

Commit a4f695a

Browse files
authoredOct 21, 2024
fix: __guard_counter already defined in the scope (#999)
* fix: __guard_counter already defined in the scope * fix: add suffix to __guard_counter
1 parent 221298b commit a4f695a

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed
 

‎ui/UIPractice.gd

+6-3
Original file line numberDiff line numberDiff line change
@@ -306,18 +306,21 @@ func _validate_and_run_student_code() -> void:
306306
# Guard against infinite while loops
307307
if "while " in script_text:
308308
var modified_code := PoolStringArray()
309+
var guard_counter = 0
309310
for line in script_text.split("\n"):
310311
if "while " in line and not line.strip_edges(true, false).begins_with("#"):
311312
var indent := 0
312313
while line[indent] == "\t":
313314
indent += 1
314315

315316
var tabs := "\t".repeat(indent)
316-
modified_code.append(tabs + "var __guard_counter := 0")
317+
var guard_counter_varname = "__guard_counter" + str(guard_counter)
318+
guard_counter += 1
319+
modified_code.append(tabs + "var " + guard_counter_varname + " := 0")
317320
modified_code.append(line)
318-
modified_code.append(tabs + "\t" + "__guard_counter += 1")
321+
modified_code.append(tabs + "\t" + guard_counter_varname + " += 1")
319322
modified_code.append(
320-
tabs + "\t" + "if __guard_counter > %s:" % MAX_WHILE_LOOP_ITERATIONS
323+
tabs + "\t" + "if " + guard_counter_varname + " > %s:" % MAX_WHILE_LOOP_ITERATIONS
321324
)
322325
modified_code.append(tabs + "\t\t" + "break")
323326
else:

0 commit comments

Comments
 (0)