diff --git a/doc/demo04_signal/image/yield.JPG b/doc/demo04_signal/image/yield.JPG new file mode 100644 index 0000000..87008a0 Binary files /dev/null and b/doc/demo04_signal/image/yield.JPG differ diff --git a/doc/demo04_signal/image/yield.JPG.import b/doc/demo04_signal/image/yield.JPG.import new file mode 100644 index 0000000..75773dc --- /dev/null +++ b/doc/demo04_signal/image/yield.JPG.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/yield.JPG-89a6f81a6f048deba903ba89a4540728.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://doc/demo04_signal/image/yield.JPG" +dest_files=[ "res://.import/yield.JPG-89a6f81a6f048deba903ba89a4540728.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/doc/demo04_signal/signal.md b/doc/demo04_signal/signal.md new file mode 100644 index 0000000..3d09109 --- /dev/null +++ b/doc/demo04_signal/signal.md @@ -0,0 +1,60 @@ +# 1. 信号signal + +- 信号是用来完成模块或功能之间通信的媒介,其实就是约定了一些方法的回调形式 + +- 设计模式上叫做观察者设计模式 + +``` +1. 观察者和被观察者是抽象耦合的,解耦模块 +2. 建立一套统一的触发机制 +``` + +- Godot引擎官方建议在你的游戏开发中更多的使用信号来完成模块或功能间的通信 + +- 第一种使用方法 + +``` +# 第一种信号接受方法,通过在场景中配置信号的接收方法 +func _on_Button1_pressed(): + print("hello button1") +``` + +- 第二种使用方法 + +``` +# 第二种信号接受方法,通过代码控制信号的接收,更加的灵活,比较推荐方式 +func _ready(): + $Button2.connect("pressed", self, "onButton2") + +func onButton2(): + print("button2 pressed") +``` + +# 2. 自定义信号 + +- 自定义信号 + +``` +signal mySignal(a, b) +``` + +- 发送信号 + +``` +emit_signal("mySignal", 1, 2) +``` + +- 解除绑定信号 + +``` +disconnect("mySignal", 1, 2) +``` + +# 3. 异步回调yield + +- yield, to produce a result, answer, or piece of information,立即结束当前函数调用,无需等待 +- yield(obj, signal),函数立即返回,并且保存当前执行的位置和状态 +- yield返回GDScriptFunctionState类型对象,类似于Java的CompleteFuture +- resume恢复GDScriptFunctionState保存的调用函数状态 + +![Image text](image/yield.JPG) \ No newline at end of file