From 862be13435c91be4dabc50da7fac7c45c9b23df5 Mon Sep 17 00:00:00 2001 From: jaysunxiao Date: Wed, 20 Oct 2021 11:52:25 +0800 Subject: [PATCH] doc --- project.godot | 2 +- scene/demo02_base/{Base.tscn => Base1.tscn} | 2 +- scene/demo02_base/Base2.tscn | 6 +++ scene/demo02_base/Base3.tscn | 6 +++ script/demo02_base/A.gd | 4 ++ script/demo02_base/B.gd | 4 ++ script/demo02_base/CNode.gd | 4 ++ script/demo02_base/CReference.gd | 5 +++ script/demo02_base/MyClass.gd | 4 +- script/demo02_base/{base.gd => base1.gd} | 23 +---------- script/demo02_base/base2.gd | 46 +++++++++++++++++++++ script/demo02_base/base3.gd | 39 +++++++++++++++++ 12 files changed, 120 insertions(+), 25 deletions(-) rename scene/demo02_base/{Base.tscn => Base1.tscn} (57%) create mode 100644 scene/demo02_base/Base2.tscn create mode 100644 scene/demo02_base/Base3.tscn create mode 100644 script/demo02_base/A.gd create mode 100644 script/demo02_base/B.gd create mode 100644 script/demo02_base/CNode.gd create mode 100644 script/demo02_base/CReference.gd rename script/demo02_base/{base.gd => base1.gd} (60%) create mode 100644 script/demo02_base/base2.gd create mode 100644 script/demo02_base/base3.gd diff --git a/project.godot b/project.godot index c6c3da7..16f900f 100644 --- a/project.godot +++ b/project.godot @@ -9,7 +9,7 @@ config_version=4 _global_script_classes=[ { -"base": "Reference", +"base": "Node2D", "class": "MyClass", "language": "GDScript", "path": "res://script/demo02_base/MyClass.gd" diff --git a/scene/demo02_base/Base.tscn b/scene/demo02_base/Base1.tscn similarity index 57% rename from scene/demo02_base/Base.tscn rename to scene/demo02_base/Base1.tscn index 8d9705f..4d8b651 100644 --- a/scene/demo02_base/Base.tscn +++ b/scene/demo02_base/Base1.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=2 format=2] -[ext_resource path="res://script/demo02_base/base.gd" type="Script" id=1] +[ext_resource path="res://script/demo02_base/base1.gd" type="Script" id=1] [node name="Node2D" type="Node2D"] script = ExtResource( 1 ) diff --git a/scene/demo02_base/Base2.tscn b/scene/demo02_base/Base2.tscn new file mode 100644 index 0000000..c624568 --- /dev/null +++ b/scene/demo02_base/Base2.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://script/demo02_base/base2.gd" type="Script" id=1] + +[node name="Node2D" type="Node2D"] +script = ExtResource( 1 ) diff --git a/scene/demo02_base/Base3.tscn b/scene/demo02_base/Base3.tscn new file mode 100644 index 0000000..535c212 --- /dev/null +++ b/scene/demo02_base/Base3.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://script/demo02_base/base3.gd" type="Script" id=1] + +[node name="Node2D" type="Node2D"] +script = ExtResource( 1 ) diff --git a/script/demo02_base/A.gd b/script/demo02_base/A.gd new file mode 100644 index 0000000..85d4571 --- /dev/null +++ b/script/demo02_base/A.gd @@ -0,0 +1,4 @@ +extends Reference + +# 表示B对象 +var b diff --git a/script/demo02_base/B.gd b/script/demo02_base/B.gd new file mode 100644 index 0000000..ecab3e4 --- /dev/null +++ b/script/demo02_base/B.gd @@ -0,0 +1,4 @@ +extends Reference + +# 表示A对象 +var a diff --git a/script/demo02_base/CNode.gd b/script/demo02_base/CNode.gd new file mode 100644 index 0000000..06d59ed --- /dev/null +++ b/script/demo02_base/CNode.gd @@ -0,0 +1,4 @@ +extends Node2D + +func _to_string(): + print("This is C Node2!") diff --git a/script/demo02_base/CReference.gd b/script/demo02_base/CReference.gd new file mode 100644 index 0000000..be3e7ae --- /dev/null +++ b/script/demo02_base/CReference.gd @@ -0,0 +1,5 @@ +# 如果没有继承任何类,则默认继承Reference +extends Reference + +func _to_string(): + print("This is C Reference!") diff --git a/script/demo02_base/MyClass.gd b/script/demo02_base/MyClass.gd index f1d8d87..8349c68 100644 --- a/script/demo02_base/MyClass.gd +++ b/script/demo02_base/MyClass.gd @@ -1,4 +1,4 @@ -# extends Node2D +extends Node2D const StringUtils = preload("res://zfoo/util/StringUtils.gd") @@ -60,7 +60,7 @@ func typeInfo(): print(StringUtils.format(template, [TYPE_DICTIONARY, typeof(dict), dict])) print(StringUtils.format(template, [TYPE_DICTIONARY, typeof(FOOD), FOOD])) print(StringUtils.format(template, [TYPE_DICTIONARY, typeof(FOOD.GOOD), FOOD.GOOD])) - print(self is Reference) + print(self is Node) print(a is Dog) # 类似于instanceof func some_function(param1, param2): diff --git a/script/demo02_base/base.gd b/script/demo02_base/base1.gd similarity index 60% rename from script/demo02_base/base.gd rename to script/demo02_base/base1.gd index 6af820a..787fcba 100644 --- a/script/demo02_base/base.gd +++ b/script/demo02_base/base1.gd @@ -1,5 +1,6 @@ extends Node2D + # Declare member variables here. Examples: export var a = 1 export var b:NodePath @@ -8,7 +9,7 @@ export(String, FILE) var e export(String, FILE, "*.txt") var d export(Resource) var f export(Color, RGB) var g -var arr = [] + # Called when the node enters the scene tree for the first time. func _ready(): print("new一个对象--------------------------------") @@ -36,24 +37,4 @@ func _ready(): print("内部类测试--------------------------------") myClass.innerClassTest() - print("垃圾回收--------------------------------") - # 如果一个类没有指明继承哪个类,则默认继承Reference(可以被自动的垃圾回收,类似其他语言的那种垃圾回收),非常重要的知识点 - for i in range(10000): - var instance = MyClass.new() - arr.append(instance) - # instance.unreference() - # 立即释放对象 - # instance.free() - # 放在队列里,等系统统一释放对象,推荐 - # instance.queue_free() - print("立即输出数组的第一位内容:") - var firstElement = arr[0] - print(arr[0]) -var frame = 0 -func _process(delta): - frame = frame + 1 - if frame == 300: - print("等待一会数组的第一位内容") - arr[0].typeInfo() - print(typeof(arr[0])) diff --git a/script/demo02_base/base2.gd b/script/demo02_base/base2.gd new file mode 100644 index 0000000..28bc8f1 --- /dev/null +++ b/script/demo02_base/base2.gd @@ -0,0 +1,46 @@ +extends Node2D + +const CNode = preload("res://script/demo02_base/CNode.gd") +const CReference = preload("res://script/demo02_base/CReference.gd") + +# monitor初探 +func _ready(): + #nodeTest() + #nodeFreeTest() + #nodeQueueFreeTest() + #referenceTest() + #referenceFreeTest() + referencUnreferenceTest() + pass + + +func nodeTest(): + for i in range(10000): + var instance = CNode.new() + + +func nodeFreeTest(): + for i in range(10000): + var instance = CNode.new() + instance.free() + +# queue_free效率更高 +func nodeQueueFreeTest(): + for i in range(10000): + var instance = CNode.new() + instance.queue_free() + +func referenceTest(): + for i in range(10000): + var instance = CReference.new() + + +func referenceFreeTest(): + for i in range(10000): + var instance = CReference.new() + instance.free() + +func referencUnreferenceTest(): + for i in range(10000): + var instance = CReference.new() + instance.unreference() diff --git a/script/demo02_base/base3.gd b/script/demo02_base/base3.gd new file mode 100644 index 0000000..c632014 --- /dev/null +++ b/script/demo02_base/base3.gd @@ -0,0 +1,39 @@ +extends Node2D + +const A = preload("res://script/demo02_base/A.gd") +const B = preload("res://script/demo02_base/B.gd") + +func _ready(): + #referenceTest() + #referenceCycleTest() + #referenceUnreference1Test() + referenceUnreference2Test() + pass + + +func referenceTest(): + var a = A.new() + var b = B.new() + +func referenceCycleTest(): + var a = A.new() + var b = B.new() + a.b = b + b.a = a + + +func referenceUnreference1Test(): + var a = A.new() + var b = B.new() + a.b = b + b.a = a + a.unreference() + b.unreference() + +func referenceUnreference2Test(): + var a = A.new() + var b = B.new() + a.b = b + b.a = a + a.b.unreference() + b.a.unreference()