instance (#56)

This commit is contained in:
Rafał Mikrut 2021-06-18 18:38:38 +02:00 committed by GitHub
parent 32f9b5e1c9
commit 87cf21e0e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 31 deletions

View File

@ -12,8 +12,7 @@ var time_to_show: int = 25 * 1000 # How long test works in miliseconds
var time_for_each_step : int = -1 var time_for_each_step : int = -1
var os var os
var time
# Each scene runs alone # Each scene runs alone
const alone_steps : Array = [ const alone_steps : Array = [
@ -26,18 +25,11 @@ const alone_steps : Array = [
func _init(): func _init():
if ClassDB.class_exists("_OS"): if ClassDB.class_exists("_OS"):
os = ClassDB.instance("_OS") os = get_instance_from_name("_OS")
else: else:
os = ClassDB.instance("_Platform") os = get_instance_from_name("_Platform")
if ClassDB.class_exists("Time"): start_time = Time.get_ticks_msec()
time = ClassDB.instance("Time")
elif ClassDB.class_exists("_OS"):
time = ClassDB.instance("_OS")
else:
time = ClassDB.instance("_Platform")
start_time = time.get_ticks_msec()
# In case when user doesn't provide time # In case when user doesn't provide time
time_for_each_step = time_to_show / (alone_steps.size()) time_for_each_step = time_to_show / (alone_steps.size())
@ -51,7 +43,7 @@ func _init():
func _process(delta: float) -> void: func _process(delta: float) -> void:
var current_run_time : int = time.get_ticks_msec() - start_time var current_run_time : int = Time.get_ticks_msec() - start_time
# While loop instead if, will allow to properly flush results under heavy operations(e.g. Thread sanitizer) # While loop instead if, will allow to properly flush results under heavy operations(e.g. Thread sanitizer)
while current_run_time > time_to_print_next_time: while current_run_time > time_to_print_next_time:
@ -62,7 +54,12 @@ func _process(delta: float) -> void:
print("######################## Ending test ########################") print("######################## Ending test ########################")
get_tree().quit() get_tree().quit()
func get_instance_from_name(method: String):
if ClassDB.class_has_method("_ClassDB","instance"):
return ClassDB.call("instance",method)
else:
return ClassDB.call("instantiate",method)
func _exit_tree(): func _exit_tree():
os.free() os.free()
time.free()

View File

@ -40,7 +40,7 @@ func tests_all_functions() -> void:
if debug_print: if debug_print:
print("\n#################### " + name_of_class + " ####################") print("\n#################### " + name_of_class + " ####################")
var object: Object = ClassDB.instance(name_of_class) var object: Object = Autoload.get_instance_from_name(name_of_class)
assert(object != null, "Object must be instantable") assert(object != null, "Object must be instantable")
if add_to_tree: if add_to_tree:
if object is Node: if object is Node:
@ -90,7 +90,7 @@ func tests_all_functions() -> void:
elif object is Object && !(obj_is_reference(object.get_class())): elif object is Object && !(obj_is_reference(object.get_class())):
object.free() object.free()
object = ClassDB.instance(name_of_class) object = Autoload.get_instance_from_name(name_of_class)
if add_to_tree: if add_to_tree:
if object is Node: if object is Node:
add_child(object) add_child(object)

View File

@ -283,7 +283,7 @@ func get_object(object_name: String) -> Object:
&& (ClassDB.is_parent_class(choosen_class, "Node") || obj_is_reference(choosen_class)) && (ClassDB.is_parent_class(choosen_class, "Node") || obj_is_reference(choosen_class))
&& !(choosen_class in BasicData.disabled_classes) && !(choosen_class in BasicData.disabled_classes)
): ):
return ClassDB.instance(choosen_class) return Autoload.get_instance_from_name(choosen_class)
if ClassDB.is_parent_class(object_name, "Node") || obj_is_reference(object_name): if ClassDB.is_parent_class(object_name, "Node") || obj_is_reference(object_name):
if should_be_always_valid: if should_be_always_valid:
@ -300,7 +300,7 @@ func get_object(object_name: String) -> Object:
assert(false, "Cannot find proper instantable child for ") assert(false, "Cannot find proper instantable child for ")
var choosen_class: String = to_use_classes[randi() % to_use_classes.size()] var choosen_class: String = to_use_classes[randi() % to_use_classes.size()]
if ClassDB.can_instance(choosen_class) && !(choosen_class in BasicData.disabled_classes): if ClassDB.can_instance(choosen_class) && !(choosen_class in BasicData.disabled_classes):
return ClassDB.instance(choosen_class) return Autoload.get_instance_from_name(choosen_class)
else: else:
while true: while true:
a += 1 a += 1
@ -308,7 +308,7 @@ func get_object(object_name: String) -> Object:
assert(false, "Cannot find proper instantable child for ") assert(false, "Cannot find proper instantable child for ")
var choosen_class: String = classes[randi() % classes.size()] var choosen_class: String = classes[randi() % classes.size()]
if ClassDB.can_instance(choosen_class) && !ClassDB.is_parent_class(choosen_class, object_name) && !(choosen_class in BasicData.disabled_classes): if ClassDB.can_instance(choosen_class) && !ClassDB.is_parent_class(choosen_class, object_name) && !(choosen_class in BasicData.disabled_classes):
return ClassDB.instance(choosen_class) return Autoload.get_instance_from_name(choosen_class)
# Non Node/Resource object # Non Node/Resource object
var to_use_classes = ClassDB.get_inheriters_from_class(object_name) var to_use_classes = ClassDB.get_inheriters_from_class(object_name)
@ -324,17 +324,17 @@ func get_object(object_name: String) -> Object:
assert(false, "Cannot find proper instantable child for ") assert(false, "Cannot find proper instantable child for ")
var choosen_class: String = to_use_classes[randi() % to_use_classes.size()] var choosen_class: String = to_use_classes[randi() % to_use_classes.size()]
if ClassDB.can_instance(choosen_class) && !(choosen_class in BasicData.disabled_classes): if ClassDB.can_instance(choosen_class) && !(choosen_class in BasicData.disabled_classes):
return ClassDB.instance(choosen_class) return Autoload.get_instance_from_name(choosen_class)
else: else:
if ClassDB.can_instance(object_name): # E.g. Texture is not instantable or shouldn't be, but LargeTexture is if ClassDB.can_instance(object_name): # E.g. Texture is not instantable or shouldn't be, but LargeTexture is
return ClassDB.instance(object_name) return Autoload.get_instance_from_name(object_name)
else: # Found child of non instantable object else: # Found child of non instantable object
var list_of_class = ClassDB.get_inheriters_from_class(object_name) var list_of_class = ClassDB.get_inheriters_from_class(object_name)
assert(list_of_class.size() > 0, "Cannot find proper instantable child for ") # Number of inherited class of non instantable class must be greater than 0, otherwise this function would be useless assert(list_of_class.size() > 0, "Cannot find proper instantable child for ") # Number of inherited class of non instantable class must be greater than 0, otherwise this function would be useless
for i in list_of_class: for i in list_of_class:
if ClassDB.can_instance(i) && (ClassDB.is_parent_class(i, "Node") || obj_is_reference(i)): if ClassDB.can_instance(i) && (ClassDB.is_parent_class(i, "Node") || obj_is_reference(i)):
return ClassDB.instance(i) return Autoload.get_instance_from_name(i)
assert(false, "Cannot find proper instantable child for ") assert(false, "Cannot find proper instantable child for ")
assert(false, "Cannot find proper instantable child for ") assert(false, "Cannot find proper instantable child for ")

View File

@ -22,16 +22,16 @@ func _populate() -> void:
if ClassDB.is_parent_class(name_of_class,"Control"): if ClassDB.is_parent_class(name_of_class,"Control"):
add_child(ClassDB.instance(name_of_class)) add_child(Autoload.get_instance_from_name(name_of_class))
continue continue
if ClassDB.is_parent_class(name_of_class,"Node3D"): if ClassDB.is_parent_class(name_of_class,"Node3D"):
add_child(ClassDB.instance(name_of_class)) add_child(Autoload.get_instance_from_name(name_of_class))
continue continue
if ClassDB.is_parent_class(name_of_class,"Node2D"): if ClassDB.is_parent_class(name_of_class,"Node2D"):
add_child(ClassDB.instance(name_of_class)) add_child(Autoload.get_instance_from_name(name_of_class))
continue continue
if ClassDB.is_parent_class(name_of_class,"Node"): if ClassDB.is_parent_class(name_of_class,"Node"):
add_child(ClassDB.instance(name_of_class)) add_child(Autoload.get_instance_from_name(name_of_class))
continue continue

View File

@ -36,7 +36,7 @@ func _ready() -> void:
if i >= collected_nodes.size(): # Wrap values if i >= collected_nodes.size(): # Wrap values
index = i % collected_nodes.size() index = i % collected_nodes.size()
var child : Node = ClassDB.instance(collected_nodes[index]) var child : Node = Autoload.get_instance_from_name(collected_nodes[index])
child.set_name("Special Node " + str(i)) child.set_name("Special Node " + str(i))
add_child(child) add_child(child)
@ -70,7 +70,7 @@ func _process(delta: float) -> void:
# if randi() % 6 == 0: # 16% chance to remove node with children # if randi() % 6 == 0: # 16% chance to remove node with children
# var names_to_remove : Array = find_all_special_children_names(choosen_node) # var names_to_remove : Array = find_all_special_children_names(choosen_node)
# for name_to_remove in names_to_remove: # for name_to_remove in names_to_remove:
# var node : Node = ClassDB.instance(collected_nodes[randi() % collected_nodes.size()]) # var node : Node = Autoload.get_instance_from_name(collected_nodes[randi() % collected_nodes.size()])
# node.set_name(name_to_remove) # node.set_name(name_to_remove)
# add_child(node) # add_child(node)
# choosen_node.queue_free() # choosen_node.queue_free()

View File

@ -9,11 +9,11 @@ var array_with_time_to_change: Array = []
func _ready(): func _ready():
for i in Autoload.alone_steps.size() + 1: for i in Autoload.alone_steps.size() + 1:
array_with_time_to_change.append(Autoload.time.get_ticks_msec() + i * Autoload.time_for_each_step) array_with_time_to_change.append(Time.get_ticks_msec() + i * Autoload.time_for_each_step)
func _process(_delta): func _process(_delta):
if current_scene < Autoload.alone_steps.size() - 1 && Autoload.time.get_ticks_msec() > array_with_time_to_change[current_scene + 1]: if current_scene < Autoload.alone_steps.size() - 1 && Time.get_ticks_msec() > array_with_time_to_change[current_scene + 1]:
current_scene += 1 current_scene += 1
for child in get_children(): for child in get_children():