pull/2/head
jaysunxiao 2021-10-20 11:52:25 +08:00
parent dd583d2ed0
commit 862be13435
12 changed files with 120 additions and 25 deletions

View File

@ -9,7 +9,7 @@
config_version=4 config_version=4
_global_script_classes=[ { _global_script_classes=[ {
"base": "Reference", "base": "Node2D",
"class": "MyClass", "class": "MyClass",
"language": "GDScript", "language": "GDScript",
"path": "res://script/demo02_base/MyClass.gd" "path": "res://script/demo02_base/MyClass.gd"

View File

@ -1,6 +1,6 @@
[gd_scene load_steps=2 format=2] [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"] [node name="Node2D" type="Node2D"]
script = ExtResource( 1 ) script = ExtResource( 1 )

View File

@ -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 )

View File

@ -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 )

4
script/demo02_base/A.gd Normal file
View File

@ -0,0 +1,4 @@
extends Reference
# 表示B对象
var b

4
script/demo02_base/B.gd Normal file
View File

@ -0,0 +1,4 @@
extends Reference
# 表示A对象
var a

View File

@ -0,0 +1,4 @@
extends Node2D
func _to_string():
print("This is C Node2!")

View File

@ -0,0 +1,5 @@
# 如果没有继承任何类则默认继承Reference
extends Reference
func _to_string():
print("This is C Reference!")

View File

@ -1,4 +1,4 @@
# extends Node2D extends Node2D
const StringUtils = preload("res://zfoo/util/StringUtils.gd") 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(dict), dict]))
print(StringUtils.format(template, [TYPE_DICTIONARY, typeof(FOOD), FOOD])) print(StringUtils.format(template, [TYPE_DICTIONARY, typeof(FOOD), FOOD]))
print(StringUtils.format(template, [TYPE_DICTIONARY, typeof(FOOD.GOOD), FOOD.GOOD])) print(StringUtils.format(template, [TYPE_DICTIONARY, typeof(FOOD.GOOD), FOOD.GOOD]))
print(self is Reference) print(self is Node)
print(a is Dog) # 类似于instanceof print(a is Dog) # 类似于instanceof
func some_function(param1, param2): func some_function(param1, param2):

View File

@ -1,5 +1,6 @@
extends Node2D extends Node2D
# Declare member variables here. Examples: # Declare member variables here. Examples:
export var a = 1 export var a = 1
export var b:NodePath export var b:NodePath
@ -8,7 +9,7 @@ export(String, FILE) var e
export(String, FILE, "*.txt") var d export(String, FILE, "*.txt") var d
export(Resource) var f export(Resource) var f
export(Color, RGB) var g export(Color, RGB) var g
var arr = []
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
print("new一个对象--------------------------------") print("new一个对象--------------------------------")
@ -36,24 +37,4 @@ func _ready():
print("内部类测试--------------------------------") print("内部类测试--------------------------------")
myClass.innerClassTest() 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]))

View File

@ -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()

View File

@ -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()