在Godot写一个指挥解析器 - 我该如何在句子中的任何地方识别一个单词?
我正在Godot中创建一个应用程序,在该应用程序中,用户在lenedit节点中输入文本。该代码检查他们键入的内容,并根据输入的单词的不同而做出不同的响应。
到现在
func process_command(_input: String) -> String:
var words = _input.split(" ", false)
if words.size() == 0:
return "Error: no words were parsed."
var first_word = words[0].to_lower()
match first_word:
"hello":
return hello()
"help":
return help()
_:
return "I don't understand."
func hello() -> String:
return "Hello. How are you?"
func help() -> String:
return "Press ESC for help."
为止这些单词是句子中的第二,第三或第十个单词。基本上,我希望它在句子中何处识别一个单词。
我已经弄乱了代码,但是我似乎无法弄清楚如何解决。它不断提出错误。我希望这是一个简单的解决方案。我是Godot和Gdscript的新手。
I'm creating an app in Godot where the user inputs text in a LineEdit node. The code checks what they have typed, and responds differently depending on the words that were entered.
So far I have it working so that it recognizes the first word that was entered:
func process_command(_input: String) -> String:
var words = _input.split(" ", false)
if words.size() == 0:
return "Error: no words were parsed."
var first_word = words[0].to_lower()
match first_word:
"hello":
return hello()
"help":
return help()
_:
return "I don't understand."
func hello() -> String:
return "Hello. How are you?"
func help() -> String:
return "Press ESC for help."
However, I'd like to get it working so that it not only recognizes "hello" or "help" if they are the first words, but also recognizes those words if they are the second, third, or tenth word in a sentence. Basically, I want it to recognize a word no matter where it is in a sentence.
I've messed around with the code, but I can't seem to figure out how to go about it. It keeps coming up with errors. I'm hoping it's a simple solution. I'm very new to Godot and GDScript.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经有一个单词的
array
:让我们明确说明插图类型:
然后,我们可以检查该
array
是否具有您想要的值,类似的东西:我请注意,在比较之前,您将转换为较低的情况。您可以在分裂之前执行此操作,因为转换为较低的情况不会影响空间:
如果这更方便,我们可以将支票放入
bool
s中:那么您可以拥有想要的任何逻辑。
我不知道代码都有两个单词时该怎么办。但是您可以检查一下:
好吧,如果我们有一个要检查的单词的
数组
,该怎么办?我们可以做一个词典!
现在,您可以检查他们是否向Platypus寻求帮助,或者也许是在寻求帮助以找到Platypus?我不知道,请记住,这不能保留秩序。无论如何,您会这样检查:
You already have an
Array
of words:Let us be explicit with the type for illustration:
Then we can check if that
Array
has a value you want, something like this:I notice that you convert to lower case before comparing. You can do that before splitting, as converting to lower case does not affect spaces:
And we can put our checks in
bool
s if that is more convenient:Then you can have whatever logic you want.
I don't know what the code should do when it has both words. But you can check that:
Alright, what if we have an
Array
of words we want to check?We could make a dictionary!
And now you can check if they asking the platypus for help, or perhaps that is asking for help to find the platypus? I don't know, remember this does not preserve order. Anyway, you would check like this: