-
-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathUIPractice.gd
759 lines (584 loc) · 22.7 KB
/
UIPractice.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
tool
class_name UIPractice
extends UINavigatablePage
signal test_student_code_completed
const RUN_AUTOTIMER_DURATION := 5.0
const SLIDE_TRANSITION_DURATION := 0.5
# Maximum allowed iterations in while loops to prevent infinite loops.
#
# Keep this number low to avoid freezing the app if the student calls print() in
# the loop.
const MAX_WHILE_LOOP_ITERATIONS := 100
const PracticeHintScene := preload("screens/practice/PracticeHint.tscn")
const PracticeListPopup := preload("components/popups/PracticeListPopup.gd")
const PracticeDonePopup := preload("components/popups/PracticeDonePopup.gd")
const PracticeLeaveUnfinishedPopup := preload("components/popups/PracticeLeaveUnfinishedPopup.gd")
var REGEX_DIVSION_BY_ZERO := RegEx.new()
export var test_practice: Resource
var _practice: Practice
var _practice_completed := false
var _practice_solution_used := false
var _script_slice: SliceProperties setget _set_script_slice
var _tester: PracticeTester
# If `true`, the text changed but was not saved.
var _code_editor_is_dirty := false
# Set to true when running the test scene, then to false the moment Events.practice_run_completed emits.
var _run_tests_requested := false
# Auto-timer forces Events.practice_run_completed after a fixed duration to fallback
# if practice code doesn't emit on its own
var _run_autotimer: Timer
var _is_info_panel_open := true
var _is_solution_panel_open := false
var _current_scene: Node
# Used to automate resetting transform and visibility to default in case the
# student calls hide(), changes transform, etc. in their practice code.
var _current_scene_reset_values := {
visible = null,
transform = null,
}
onready var _layout_container := $Margin/Layout as Control
onready var _output_container := find_node("Output") as Control
onready var _game_container := find_node("GameContainer") as Container
onready var _game_view := _output_container.find_node("GameView") as GameView
onready var _output_console := _output_container.find_node("Console") as OutputConsole
onready var _output_anchors := $Margin/Layout/OutputAnchors as Control
onready var _solution_panel := find_node("SolutionContainer") as Control
onready var _solution_editor := _solution_panel.find_node("SliceEditor") as SliceEditor
onready var _use_solution_button := _solution_panel.find_node("UseSolutionButton") as Button
onready var _info_panel_anchors := $Margin/Layout/InfoPanelAnchors as Control
onready var _info_panel := find_node("PracticeInfoPanel") as PracticeInfoPanel
onready var _documentation_panel := find_node("DocumentationPanel") as RichTextLabel
onready var _hints_container := _info_panel.hints_container as Revealer
onready var _practice_list := find_node("PracticeListPopup") as PracticeListPopup
onready var _practice_done_popup := find_node("PracticeDonePopup") as PracticeDonePopup
onready var _practice_leave_unfinished_popup := find_node("PracticeLeaveUnfinishedPopup") as PracticeLeaveUnfinishedPopup
onready var _code_editor := find_node("CodeEditor") as CodeEditor
onready var _tween := $Tween as Tween
func _init():
REGEX_DIVSION_BY_ZERO.compile("[/%] *0")
_run_autotimer = Timer.new()
_run_autotimer.one_shot = true
_run_autotimer.wait_time = RUN_AUTOTIMER_DURATION
add_child(_run_autotimer)
_run_autotimer.connect("timeout", self, "_on_autotimer_timeout")
_on_init_set_javascript()
func _ready() -> void:
randomize()
if Engine.editor_hint:
return
_code_editor.connect("action_taken", self, "_on_code_editor_action_taken")
_code_editor.connect("text_changed", self, "_on_code_editor_text_changed")
_code_editor.connect("console_toggled", self, "_on_console_toggled")
_output_console.connect("reference_clicked", self, "_on_code_reference_clicked")
_use_solution_button.connect("pressed", self, "_on_use_solution_pressed")
_info_panel.connect("list_requested", self, "_on_list_requested")
_practice_done_popup.connect("accepted", self, "_on_next_requested")
_practice_leave_unfinished_popup.connect("confirmed", self, "_accept_unload")
_practice_leave_unfinished_popup.connect("denied", self, "_deny_unload")
Events.connect("practice_run_completed", self, "_test_student_code")
_update_slidable_panels()
_layout_container.connect("resized", self, "_update_slidable_panels")
_solution_panel.modulate.a = 0.0
_solution_panel.margin_left = _output_anchors.rect_size.x
if test_practice and get_parent() == get_tree().root:
setup(test_practice, null, null)
func _notification(what: int) -> void:
if what == NOTIFICATION_TRANSLATION_CHANGED:
_update_slidable_panels()
_update_labels()
func _gui_input(event: InputEvent) -> void:
var mb := event as InputEventMouseButton
if mb and mb.button_index == BUTTON_LEFT and mb.pressed and get_focus_owner():
# Makes clicks on the empty area to remove focus from various controls, specifically
# the code editor.
get_focus_owner().release_focus()
func setup(practice: Practice, lesson: Lesson, course: Course) -> void:
if not is_inside_tree():
yield(self, "ready")
_practice = practice
_practice_completed = false
_practice_solution_used = false
_info_panel.title_label.text = tr(practice.title).capitalize()
# FIXME: Some weird Windows issue, replace before translating so matching works.
_info_panel.goal_rich_text_label.bbcode_text = TextUtils.bbcode_add_code_color(
tr(practice.goal.replace("\r\n", "\n"))
)
_code_editor.text = practice.starting_code
_code_editor.update_cursor_position(practice.cursor_line, practice.cursor_column)
_hints_container.visible = not practice.hints.empty()
var index := 0
for hint in practice.hints:
var practice_hint: PracticeHint = PracticeHintScene.instance()
practice_hint.title = tr("Hint %s") % [String(index + 1).pad_zeros(1)]
practice_hint.bbcode_text = tr(hint)
_hints_container.add_child(practice_hint)
index += 1
# TODO: Should probably avoid relying on content ID for getting paths.
var base_directory := practice.practice_id.get_base_dir()
var slice_path := practice.script_slice_path
if slice_path.is_rel_path():
slice_path = base_directory.plus_file(slice_path)
_set_script_slice(load(slice_path))
_code_editor.slice_editor.setup(_script_slice)
_code_editor.set_continue_allowed(false)
_solution_editor.text = _script_slice.slice_text
var validator_path := practice.validator_script_path
if validator_path.is_rel_path():
validator_path = base_directory.plus_file(validator_path)
_tester = (load(validator_path) as GDScript).new()
_tester.setup(_game_view.get_viewport(), _script_slice)
var documentation_reference := _practice.get_documentation_raw()
if documentation_reference.is_empty():
_info_panel.clear_documentation()
else:
_info_panel.set_documentation(documentation_reference)
_info_panel.display_tests(_tester.get_test_names())
_game_view.use_scene(_current_scene, _script_slice.get_scene_properties().viewport_size)
# In case we directly test a practice from the editor, we don't have access to the lesson.
if lesson and course:
_practice_list.clear_items()
for practice_data in lesson.practices:
_practice_list.add_item(practice_data, lesson, course, practice_data == practice)
var user_profile := UserProfiles.get_profile()
var completed_before = user_profile.is_lesson_practice_completed(
course.resource_path, lesson.resource_path, practice.practice_id
)
if completed_before:
_info_panel.set_status_icon(_info_panel.Status.COMPLETED_BEFORE)
# Turns off animations to run integration tests faster.
func turn_on_test_mode() -> void:
_info_panel.skip_animations = true
func _update_labels() -> void:
if not _practice:
return
_info_panel.title_label.text = tr(_practice.title).capitalize()
# FIXME: Some weird Windows issue, replace before translating so matching works.
_info_panel.goal_rich_text_label.bbcode_text = TextUtils.bbcode_add_code_color(
tr(_practice.goal.replace("\r\n", "\n"))
)
var index := 0
for child_node in _hints_container.get_children():
var practice_hint = child_node as PracticeHint
if not practice_hint:
continue
practice_hint.title = tr("Hint %s") % [String(index + 1).pad_zeros(1)]
practice_hint.bbcode_text = tr(_practice.hints[index])
index += 1
_info_panel.display_tests(_tester.get_test_names())
func get_screen_resource() -> Practice:
return _practice
func _set_script_slice(new_slice: SliceProperties) -> void:
if new_slice == _script_slice:
return
_script_slice = new_slice
_output_console.setup(_script_slice)
_current_scene = _script_slice.get_scene_properties().scene.instance()
_current_scene_reset_values.visible = _current_scene.get("visible")
_current_scene_reset_values.transform = _current_scene.get("transform")
func _validate_and_run_student_code() -> void:
# Prepare everything for testing user code.
_game_view.paused = false
_code_editor.lock_editor()
_code_editor.set_pause_button_pressed(false)
_code_editor.set_locked_message(tr("Validating Your Code..."))
_hide_solution_panel()
_code_editor.set_solution_button_pressed(false)
_output_console.clear_messages()
_info_panel.reset_tests_status()
_disable_distraction_free_mode()
# Complete the script from the slice and the base script.
_script_slice.current_text = _code_editor.get_text()
var script_text := _script_slice.current_full_text
var script_file_name := _script_slice.get_script_properties().file_name
var script_file_path := _script_slice.get_script_properties().file_path.lstrip("res://")
# Do local sanity checks for the script.
var recursive_function := MiniGDScriptTokenizer.new(script_text).find_any_recursive_function()
if recursive_function != "":
var error := ScriptError.new()
error.message = (
tr("The function `%s` calls itself, this creates an infinite loop")
% [recursive_function]
)
error.severity = 1
error.code = GDQuestCodes.ErrorCode.RECURSIVE_FUNCTION
MessageBus.print_script_error(error, script_file_name)
_code_editor.unlock_editor()
return
var verifier := OfflineScriptVerifier.new(script_text)
verifier.test()
var errors := verifier.errors
if not errors.empty():
_code_editor.slice_editor.errors = errors
for index in errors.size():
var error: ScriptError = errors[index]
MessageBus.print_script_error(error, script_file_name)
var is_missing_parser_error: bool = (
errors.size() == 1
and OfflineScriptVerifier.check_error_is_missing_parser_error(errors[0])
)
if not is_missing_parser_error:
_code_editor.unlock_editor()
return
# Run student code
var nodes_paths := _script_slice.get_script_properties().nodes_paths
# Generate a runnable script, check for uncaught errors.
_code_editor.set_locked_message(tr("Running Your Code..."))
yield(get_tree(), "idle_frame")
script_text = MessageBus.replace_print_calls_in_script(script_file_name, script_text)
# Guard against infinite while loops
if "while " in script_text:
var modified_code := PoolStringArray()
var guard_counter = 0
for line in script_text.split("\n"):
if "while " in line and not line.strip_edges(true, false).begins_with("#"):
var indent := 0
while line[indent] == "\t":
indent += 1
var tabs := "\t".repeat(indent)
var guard_counter_varname = "__guard_counter" + str(guard_counter)
guard_counter += 1
modified_code.append(tabs + "var " + guard_counter_varname + " := 0")
modified_code.append(line)
modified_code.append(tabs + "\t" + guard_counter_varname + " += 1")
modified_code.append(
tabs + "\t" + "if " + guard_counter_varname + " > %s:" % MAX_WHILE_LOOP_ITERATIONS
)
modified_code.append(tabs + "\t\t" + "break")
else:
modified_code.append(line)
script_text = modified_code.join("\n")
elif REGEX_DIVSION_BY_ZERO.search(script_text):
var error := ScriptError.new()
error.message = tr(
'There is a division by zero in your code. You cannot divide by zero in code. Please ensure you have no "/ 0" or "% 0" in your code.'
)
error.severity = 1
error.code = GDQuestCodes.ErrorCode.INVALID_NO_CATCH
MessageBus.print_script_error(error, script_file_name)
_code_editor.unlock_editor()
return
var script = GDScript.new()
script.source_code = script_text
var script_is_valid = script.reload()
if script_is_valid != OK:
var error := ScriptError.new()
error.message = tr(
"Oh no! The script has an error, but the Script Verifier did not catch it"
)
error.severity = 1
error.code = GDQuestCodes.ErrorCode.INVALID_NO_CATCH
MessageBus.print_script_error(error, script_file_name)
_code_editor.unlock_editor()
return
_code_editor_is_dirty = false
# Start the auto-timer used to perform tests in case the scene doesn't emit the required signal.
_run_tests_requested = true
_run_autotimer.start()
# Reset and run the test scene.
_update_nodes(script, nodes_paths)
func _test_student_code() -> void:
if not _run_tests_requested:
return
_run_tests_requested = false
var script_file_name := _script_slice.get_script_properties().file_name
# Run tests on the scene.
_code_editor.set_locked_message(tr("Running Tests..."))
_info_panel.set_tests_pending()
var result := _tester.run_tests()
_info_panel.set_tests_status(result, script_file_name)
yield(_info_panel, "tests_updated")
# Show the end of practice popup.
if not _practice_completed and result.is_success():
_practice_completed = true
if not _practice_solution_used:
Events.emit_signal("practice_completed", _practice)
_practice_done_popup.show()
_practice_done_popup.fade_in(_game_container)
# Clean-up.
_code_editor.unlock_editor()
if _practice_completed:
_code_editor.set_continue_allowed(true)
emit_signal("test_student_code_completed")
func _reset_practice() -> void:
# Code is already reset by the slice editor.
_info_panel.reset_tests_status()
_code_editor.slice_editor.errors = []
_output_console.clear_messages()
if _current_scene.has_method("reset"):
_current_scene.call("reset")
for property in _current_scene_reset_values:
_current_scene.set(property, _current_scene_reset_values[property])
func _update_slidable_panels() -> void:
# Wait a frame to make sure the new size has been applied.
yield(get_tree(), "idle_frame")
# We use _output_anchors for reference because it never leaves the screen, so we can rely on it
# to always report the target size for one third of the hbox.
# Update info panel.
_info_panel.rect_min_size = Vector2(_output_anchors.rect_size.x, 0)
_info_panel.set_anchors_and_margins_preset(Control.PRESET_WIDE, Control.PRESET_MODE_MINSIZE)
# Update solution panel.
_solution_panel.rect_min_size = Vector2(_output_anchors.rect_size.x, 0)
_solution_panel.set_anchors_and_margins_preset(Control.PRESET_WIDE, Control.PRESET_MODE_MINSIZE)
if not _is_solution_panel_open:
_solution_panel.margin_left = _output_anchors.rect_size.x
func _toggle_distraction_free_mode() -> void:
if _is_info_panel_open:
_enable_distraction_free_mode()
else:
_disable_distraction_free_mode()
func _disable_distraction_free_mode() -> void:
_is_info_panel_open = true
_tween.remove_all()
_tween.interpolate_property(
_info_panel_anchors,
"size_flags_stretch_ratio",
_info_panel_anchors.size_flags_stretch_ratio,
1.0,
SLIDE_TRANSITION_DURATION,
Tween.TRANS_SINE,
Tween.EASE_IN_OUT
)
_tween.interpolate_property(
_code_editor,
"size_flags_stretch_ratio",
_code_editor.size_flags_stretch_ratio,
1.0,
SLIDE_TRANSITION_DURATION,
Tween.TRANS_SINE,
Tween.EASE_IN_OUT
)
_tween.interpolate_property(
_info_panel_anchors,
"modulate:a",
_info_panel_anchors.modulate.a,
1.0,
SLIDE_TRANSITION_DURATION,
Tween.TRANS_LINEAR,
Tween.EASE_IN
)
_tween.start()
_code_editor.set_distraction_free_state(not _is_info_panel_open)
func _enable_distraction_free_mode() -> void:
_update_slidable_panels()
_is_info_panel_open = false
_tween.remove_all()
_tween.interpolate_property(
_info_panel_anchors,
"size_flags_stretch_ratio",
_info_panel_anchors.size_flags_stretch_ratio,
0.0,
SLIDE_TRANSITION_DURATION,
Tween.TRANS_SINE,
Tween.EASE_IN_OUT
)
_tween.interpolate_property(
_code_editor,
"size_flags_stretch_ratio",
_code_editor.size_flags_stretch_ratio,
2.0,
SLIDE_TRANSITION_DURATION,
Tween.TRANS_SINE,
Tween.EASE_IN_OUT
)
_tween.interpolate_property(
_info_panel_anchors,
"modulate:a",
_info_panel_anchors.modulate.a,
0.0,
SLIDE_TRANSITION_DURATION - 0.25,
Tween.TRANS_LINEAR,
Tween.EASE_IN,
0.15
)
_tween.start()
_code_editor.set_distraction_free_state(not _is_info_panel_open)
func _toggle_solution_panel() -> void:
if _is_solution_panel_open:
_hide_solution_panel()
else:
_show_solution_panel()
func _show_solution_panel() -> void:
_update_slidable_panels()
_is_solution_panel_open = true
_tween.remove_all()
_tween.interpolate_property(
_solution_panel,
"margin_left",
_solution_panel.margin_left,
0.0,
SLIDE_TRANSITION_DURATION,
Tween.TRANS_SINE,
Tween.EASE_IN_OUT
)
_tween.interpolate_property(
_solution_panel,
"modulate:a",
_solution_panel.modulate.a,
1.0,
SLIDE_TRANSITION_DURATION,
Tween.TRANS_LINEAR,
Tween.EASE_IN
)
_tween.start()
func _hide_solution_panel() -> void:
_update_slidable_panels()
_is_solution_panel_open = false
_tween.remove_all()
_tween.interpolate_property(
_solution_panel,
"margin_left",
_solution_panel.margin_left,
_output_anchors.rect_size.x,
SLIDE_TRANSITION_DURATION,
Tween.TRANS_SINE,
Tween.EASE_IN_OUT
)
_tween.interpolate_property(
_solution_panel,
"modulate:a",
_solution_panel.modulate.a,
0.0,
SLIDE_TRANSITION_DURATION - 0.25,
Tween.TRANS_LINEAR,
Tween.EASE_IN,
0.15
)
_tween.start()
func _on_use_solution_pressed() -> void:
_code_editor.slice_editor.sync_text_with_slice()
_practice_solution_used = true
_info_panel.set_status_icon(_info_panel.Status.SOLUTION_USED)
_hide_solution_panel()
_code_editor.set_solution_button_pressed(false)
_code_editor.set_restore_allowed(true)
_update_game_paused()
func _update_game_paused() -> void:
_game_view.paused = (
_code_editor.is_pause_button_pressed()
|| _code_editor.is_solution_button_pressed()
)
func _on_code_editor_text_changed(_text: String) -> void:
_code_editor_is_dirty = true
func _on_code_editor_action_taken(which: String) -> void:
match which:
_code_editor.ACTIONS.RUN:
_validate_and_run_student_code()
_code_editor.ACTIONS.PAUSE:
_update_game_paused()
_code_editor.ACTIONS.DFMODE:
_toggle_distraction_free_mode()
_code_editor.ACTIONS.RESTORE:
_reset_practice()
_code_editor.ACTIONS.SOLUTION:
_update_game_paused()
_toggle_solution_panel()
_code_editor.ACTIONS.CONTINUE:
_on_next_requested()
func _on_console_toggled() -> void:
_output_console.visible = not _output_console.visible
func _on_code_reference_clicked(_file_name: String, line: int, character: int) -> void:
_code_editor.slice_editor.line_highlight_requested(line, character)
func _on_previous_requested() -> void:
if not _practice:
return
Events.emit_signal("practice_previous_requested", _practice)
func _on_next_requested() -> void:
if not _practice:
return
Events.emit_signal("practice_next_requested", _practice)
func _on_list_requested() -> void:
_practice_list.show()
func _on_autotimer_timeout() -> void:
if _run_tests_requested:
Events.emit_signal("practice_run_completed")
func _on_current_screen_unload_requested() -> void:
if not _code_editor_is_dirty:
_accept_unload()
return
_practice_leave_unfinished_popup.popup()
# Updates all nodes with the given script. If a node path isn't valid, the node
# will be silently skipped.
func _update_nodes(script: GDScript, node_paths: Array) -> void:
for node_path in node_paths:
if node_path is NodePath or node_path is String:
var node = (
_current_scene.get_node_or_null(node_path)
if node_path != ""
else _current_scene
)
if node:
try_validate_and_replace_script(node, script)
# If a script is valid, sets in the node. Optionally restores variables and
# optionally calls _run()
# @param node Node any valid node
# @param script GDScript A GDScript instance
# @param props_backup Dictionary[string, *] An optional dictionary of properties
# to restore before calling `_run()`
static func try_validate_and_replace_script(node: Node, script: GDScript, props_backup := {}) -> void:
if not script.can_instance():
var error_code := script.reload()
if not script.can_instance():
print("Script errored out (code %s); skipping replacement" % [error_code])
return
var parent = node.get_parent()
parent.remove_child(node)
node.request_ready()
node.set_script(script)
parent.add_child(node)
# props_backup = backup_node_properties(node)
if props_backup.size() > 0:
set_node_properties(node, props_backup)
if node.has_method("_run"):
# warning-ignore:unsafe_method_access
node._run()
# Saves all the node's properties in a dictionary. Useful in case you want to
# change back properties of a node after setting a script
# You can optionally pass an array of property names you want (any property you
# do not specify will be left out)
# @param node Node any valid node
# @param select_properties PoolStringArray an array of property names. Leave it
# out (or pass an empty array) to select
# all properties
static func backup_node_properties(node: Node, select_properties := PoolStringArray()) -> Dictionary:
var props_backup := {}
var node_original_props := node.get_property_list()
var select_all := select_properties.size() == 0
var select_cache := {}
for prop_name in select_properties:
select_cache[prop_name] = true
for prop in node_original_props:
var prop_name: String = prop.name
if prop_name == "script":
continue
if select_all or (prop_name in select_cache):
props_backup[prop_name] = node.get(prop_name)
return props_backup
# Sets a node's properties from a dictionary. Intended to be used after having
# saved them prior to changing the script
static func set_node_properties(node: Node, props_backup: Dictionary) -> void:
for prop_name in props_backup:
if not (prop_name in node): # In case a property is removed
continue
var current_value = node.get(prop_name)
var backed_up_value = props_backup[prop_name]
if current_value == backed_up_value:
continue
node.set(prop_name, backed_up_value)
###############################################################################
#
# JS INTERFACE
# the below intends to listen to the `RECURSIVE` error event dispatched back
# from JS and react to it
#
# JS error event listener
var _on_js_error_feedback_ref = JavaScript.create_callback(self, "_on_js_error_feedback")
# This will be called from Javascript
func _on_js_error_feedback(args):
var code = args[0]
if code is String and code == "RECURSIVE":
print("recursive code")
# Set the event listener
func _on_init_set_javascript() -> void:
if OS.has_feature("JavaScript"):
var GDQUEST = JavaScript.get_interface("GDQUEST")
GDQUEST.events.onError.connect(_on_js_error_feedback_ref)