From 7177d6034a2ef0c08f9ce7bb421813b6e9a7e2d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Mikrut?= <41945903+qarmin@users.noreply.github.com> Date: Wed, 28 Apr 2021 13:26:20 +0200 Subject: [PATCH] Add debug prints (#48) --- Nodes/Nodes.gd | 105 ++++++++++------ ReparentingDeleting/ReparentingDeleting.gd | 134 ++++++++++++--------- 2 files changed, 146 insertions(+), 93 deletions(-) diff --git a/Nodes/Nodes.gd b/Nodes/Nodes.gd index bf356cf..a755ac0 100644 --- a/Nodes/Nodes.gd +++ b/Nodes/Nodes.gd @@ -1,39 +1,67 @@ extends Node -var TIME_TO_DELETE : float = 1.0 -var time_to_delete : float = TIME_TO_DELETE - -var disabled_classes : Array = [] - -func _populate() -> void: - for _i in range(2): # Number of created - for name_of_class in ClassDB.get_class_list(): - if name_of_class in disabled_classes: - continue - if !ClassDB.can_instance(name_of_class): - continue - - - if ClassDB.is_parent_class(name_of_class,"Control"): - add_child(ClassDB.instance(name_of_class)) - continue - if ClassDB.is_parent_class(name_of_class,"Spatial"): - add_child(ClassDB.instance(name_of_class)) - continue - if ClassDB.is_parent_class(name_of_class,"Node2D"): - add_child(ClassDB.instance(name_of_class)) - continue - if ClassDB.is_parent_class(name_of_class,"Node"): - add_child(ClassDB.instance(name_of_class)) - continue - - -# Populate at start -func _ready() -> void: - _populate() +# This script adds nodes to scene tree and removes them after certain amount +# of time + +# Counters which are used to delete and adds nodes in loop +var TIME_TO_DELETE: float = 3.0 +var time_to_delete: float = TIME_TO_DELETE + +# List of disabled classes +var disabled_classes: Array = [] +# List of all collected nodes which +var classes: Array = [] + +var debug_enabled: bool = false + + +# Collects all classes which will be used +func collect() -> void: + for name_of_class in ClassDB.get_class_list(): + if name_of_class in disabled_classes: + continue + if !ClassDB.can_instance(name_of_class): + continue + + if ClassDB.is_parent_class(name_of_class, "Control"): + classes.append(name_of_class) + continue + if ClassDB.is_parent_class(name_of_class, "Spatial"): + classes.append(name_of_class) + continue + if ClassDB.is_parent_class(name_of_class, "Node2D"): + classes.append(name_of_class) + continue + if ClassDB.get_parent_class(name_of_class) == "Node": + classes.append(name_of_class) + continue + + classes.sort() + if debug_enabled: + var to_print: String = "DEBUG: List of classes used in Nodes scene:\n" + to_print += "DEBUG: [" + for index in range(classes.size()): + to_print += "\"" + classes[index] + "\"" + if index != classes.size() - 1: + to_print += ", " + print(to_print) + + +# Adds nodes to scenes +func populate() -> void: + for _i in range(2): # Number of created instances of object + for name_of_class in classes: + add_child(ClassDB.instance(name_of_class)) + + +# Populate nodes at start +func _ready() -> void: + collect() + populate() + -# Move nodes a little and delete and readd them later func _process(delta: float) -> void: + # Moves nodes a little for i in get_children(): if i is Control: i._set_size(Vector2(200 * randf() - 100, 200 * randf() - 100)) @@ -43,12 +71,17 @@ func _process(delta: float) -> void: if i.get_name() != "Camera": i.set_scale(Vector3(delta + 1, delta + 1, delta + 1)) i.set_translation(Vector3(10 * randf(), 10 * randf(), 10 * randf())) - + time_to_delete -= delta + # Delete and adds later nodes if time_to_delete < 0: + if debug_enabled: + print("DEBUG: Deleting nodes") time_to_delete += TIME_TO_DELETE - + for i in get_children(): i.queue_free() - - _populate() + + if debug_enabled: + print("DEBUG: Adding nodes") + populate() diff --git a/ReparentingDeleting/ReparentingDeleting.gd b/ReparentingDeleting/ReparentingDeleting.gd index 0f1732a..9603efc 100644 --- a/ReparentingDeleting/ReparentingDeleting.gd +++ b/ReparentingDeleting/ReparentingDeleting.gd @@ -1,88 +1,108 @@ extends Node -var number_of_nodes : int = 0 +# Script first adds nodes to scene, then choose some random nodes and reparents +# them or delete and replace with new ones -var collected_nodes : Array = [] -var disabled_classes : Array = [ - "ReflectionProbe", # Cause errors, not sure about it -] # Just add name of any class if cause problems - -func collect() -> void: - var classes : Array = ClassDB.get_class_list() - classes.sort() - for name_of_class in classes: - if ClassDB.is_parent_class(name_of_class,"Node"): - if name_of_class.find("Editor") != -1: # We don't want to test editor nodes - continue - if disabled_classes.has(name_of_class): # Class is disabled - continue - if ClassDB.can_instance(name_of_class): # Only instantable nodes can be used - collected_nodes.append(name_of_class) - -func _ready() -> void: - seed(405) - collect() - number_of_nodes = max(collected_nodes.size(),200) # Use at least all nodes, or more if you want(168 is probably number nodes) - for i in range(number_of_nodes): - var index = i - if i >= collected_nodes.size(): # Wrap values - index = i % collected_nodes.size() - - var child : Node = ClassDB.instance(collected_nodes[index]) - child.set_name("Special Node " + str(i)) - add_child(child) - -## It is quite easy algorithm to reparent and delete items +## Algorithm # - Add multiple nodes to scene # - Set name to each -# - In process +# - In _process # - Get random node -# - Remove its parent -# - Play with a russian roulette -# - If node will be deleted, be sure to get list of its all children and then -# replace all with new nodes(change also name) and old remove with queue_free() +# - Detach it from its parent +# - Play with a russian roulette: +# - If node will be deleted, be sure to get list of its all children and then +# replace all with new nodes(change also name) and old remove with queue_free() # - Get another random node # - If nodes are the same, add node to root one(cannot set self as self parent) and repeat steps # - If second node is child of first, add first node to root one(prevents from memory leaks due invalid reparenting) # - At the end add first random node as child of second +var number_of_nodes: int = 0 + +# Collected nodes +var collected_nodes: Array = [] +# Disabled nodes which won't be used +var disabled_classes: Array = [ + "ReflectionProbe", #GH 45460 +] + +var debug_enabled: bool = false + + +func collect() -> void: + var classes: Array = ClassDB.get_class_list() + classes.sort() + for name_of_class in classes: + if ClassDB.is_parent_class(name_of_class, "Node"): + if name_of_class.find("Editor") != -1: # We don't want to test editor nodes + continue + if disabled_classes.has(name_of_class): # Class is disabled + continue + if ClassDB.can_instance(name_of_class): # Only instantable nodes can be used + collected_nodes.append(name_of_class) + + if debug_enabled: + var to_print: String = "DEBUG: List of classes used in ReparentingDeleting scene:\n" + to_print += "DEBUG: [" + for index in range(classes.size()): + to_print += "\"" + classes[index] + "\"" + if index != classes.size() - 1: + to_print += ", " + print(to_print) + + +func _ready() -> void: + seed(405) + collect() + number_of_nodes = max(collected_nodes.size(), 200) # Use at least all nodes, or more if you want(168 is probably number of all nodes) + for i in range(number_of_nodes): + var index = i + if i >= collected_nodes.size(): # Wrap values + index = i % collected_nodes.size() + + var child: Node = ClassDB.instance(collected_nodes[index]) + child.set_name("Special Node " + str(i)) + add_child(child) + + func _process(delta: float) -> void: -# assert(Performance.get_monitor(Performance.OBJECT_ORPHAN_NODE_COUNT) == 0) # Don't work good with running more than 1 this scene - - var choosen_node : Node - var parent_of_node : Node +# assert(Performance.get_monitor(Performance.OBJECT_ORPHAN_NODE_COUNT) == 0) # Don't work good when instancing more than 1 scene, because node queued for deleting + + var choosen_node: Node + var parent_of_node: Node for i in range(5): - var number : String = "Special Node " + str(randi() % number_of_nodes) - choosen_node = find_node(number,true,false) + var number: String = "Special Node " + str(randi() % number_of_nodes) + choosen_node = find_node(number, true, false) parent_of_node = choosen_node.get_parent() - - var random_node = find_node("Special Node " + str(randi() % number_of_nodes),true,false) + + var random_node = find_node("Special Node " + str(randi() % number_of_nodes), true, false) parent_of_node.remove_child(choosen_node) - - if randi() % 6 == 0: # 16% chance to remove node with children - var names_to_remove : Array = find_all_special_children_names(choosen_node) + + if randi() % 6 == 0: # 16% chance to remove node with children + var names_to_remove: Array = find_all_special_children_names(choosen_node) for name_to_remove in names_to_remove: - var node : Node = ClassDB.instance(collected_nodes[randi() % collected_nodes.size()]) + var node: Node = ClassDB.instance(collected_nodes[randi() % collected_nodes.size()]) node.set_name(name_to_remove) add_child(node) choosen_node.queue_free() continue - - - if choosen_node.find_node(random_node.get_name(),true,false) != null: # Cannot set as node parent one of its child + + if choosen_node.find_node(random_node.get_name(), true, false) != null: # Cannot set as node parent one of its child add_child(choosen_node) continue - if choosen_node == random_node: # Do not reparent node to self + if choosen_node == random_node: # Do not reparent node to self add_child(choosen_node) continue random_node.add_child(choosen_node) -# Finds recusivelly all child nodes which are not internal -func find_all_special_children_names(node : Node) -> Array: - var array : Array = [] + +# Finds recusivelly all child nodes which will be also removed to be able to add +# exactly same number of nodes in replacement. +func find_all_special_children_names(node: Node) -> Array: + var array: Array = [] array.append(node.get_name()) for child in node.get_children(): if child.get_name().begins_with("Special Node"): array.append_array(find_all_special_children_names(child)) - + return array