[英]How do I emit a signal argument as an int instead of string?
In Godot 3.3, I'm trying to make a Label respond to text entered through a LineEdit node.在 Godot 3.3 中,我试图使 Label 响应通过 LineEdit 节点输入的文本。 I connected the objects and can emit the signal, but the signal is only ever sent as a string, not as the int that I want.
我连接了对象并可以发出信号,但信号只作为字符串发送,而不是我想要的 int。 When I use the strong typing, I get the error "Cannot convert argument 1 from String to int.."
当我使用强类型时,出现错误“无法将参数 1 从 String 转换为 int..”
When I stop using strong typing and go back to weak typing, I have no errors.当我停止使用强类型和 go 回到弱类型时,我没有错误。 How do I emit a signal and make sure it's the data type I specified?
如何发出信号并确保它是我指定的数据类型?
In the LineEdit node: emit_signal("text_entered", text as int)
在 LineEdit 节点中:
emit_signal("text_entered", text as int)
In the Label node:在 Label 节点中:
func _on_text_entered(value:int):
<-This function header causes errors func _on_text_entered(value:int):
<-这个 function header 导致错误
func _on_text_entered(value):
<-While this one does not. func _on_text_entered(value):
<-虽然这个没有。
In your LineEdit
, "text_entered" is a build-in signal that the LineEdit
uses.在您的
LineEdit
中,“text_entered”是LineEdit
使用的内置信号。 And when the LineEdit
uses it, it sends String
s (regardless of what you send when you use it).当
LineEdit
使用它时,它会发送String
s(无论您在使用它时发送什么)。
When you send an int
, there is no problem with the connected function taking int
.当你发送一个
int
时,连接的 function 接受int
没有问题。 But when LineEdit
sends a String
(as it does) the type does not match, and you get an error.但是,当
LineEdit
发送一个String
(它确实如此)时,类型不匹配,并且您会收到错误消息。
To answer the question on the title:回答标题的问题:
How do I emit a signal argument as an int instead of string?
如何将信号参数作为 int 而不是字符串发出?
You are doing it.你在做。 The code
emit_signal("text_entered", text as int)
is correct.代码
emit_signal("text_entered", text as int)
是正确的。
The problem is that LineEdit
will send String
s as well.问题是
LineEdit
也会发送String
。
Of course, when you don't specify the type in the connected function, it can take both the int
you send, and the String
that LineEdit
send.当然,当你在连接的function中没有指定类型时,它可以同时接受你发送的
int
和LineEdit
发送的String
。
Solution?解决方案?
Declare a new signal.声明一个新信号。 For example
number_entered
:例如
number_entered
:
signal number_entered(number)
And emit that:并发出:
emit_signal("number_entered", text as int)
Since this is a custom signal you are declaring, LineEdit
does not use it, and you would be in control on what you send.由于这是您声明的自定义信号,
LineEdit
不会使用它,您可以控制发送的内容。 So you can connect your function that takes int
to that signal, and it should give you no problems.因此,您可以将接受该信号的
int
连接到该信号,它应该不会给您带来任何问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.