(摘) Godot 免费跨平台游戏引擎: 信号

声明:内容源自网络,版权归原作者所有。若有侵权请在网页聊天中联系我

原地址见这里

一、类自定义信号

extends Node

signal custom_signal(para1)  #定义信号

func _ready():
	self.connect("custom_signal", self, "_on_test_node_custom_signal", ["ab",1000])  #连接信号

func _input(event):
	if event is InputEventMouseButton:
	   if event.button_index == BUTTON_LEFT and event.pressed:
	      emit_signal("custom_signal","what") #发送信号

#信号回调
func _on_test_node_custom_signal(para1,para2,para3):
     print(para1," ",para2," ",para3)

上面的代码中,para1 对应"what",para2对应"ab",para3对应1000

二、实例化对象添加信号

extends Node
var node_ins

func _ready():
    node_ins = load("res://node.gd").new()
    #1.定义信号
    node_ins.add_user_signal("custom_user_signal")
    #2.连接信号
    node_ins.connect("custom_user_signal",self,"_on_node_custom_user_signal")

func _input(event):
    if event is InputEventMouseButton:
        if event.button_index == BUTTON_LEFT and event.pressed:
            #3发送信号
            node_ins.emit_signal("custom_user_signal")

#回调函数
func _on_node_custom_user_signal():
    print("_on_node_custom_user_signal")

三、信号相关的其他函数

void add_user_signal ( String signal, Array arguments=[ ] )

int connect ( String signal, Object target, String method, Array binds=[ ], int flags=0 )

void disconnect ( String signal, Object target, String method )

Variant emit_signal ( String signal ) vararg

Array get_signal_connection_list ( String signal ) const

Array get_signal_list ( ) const

bool has_user_signal ( String signal ) const

bool is_blocking_signals ( ) const

bool is_connected ( String signal, Object target, String method ) const

void set_block_signals ( bool enable )

相关文章