如何使用基本节点/类枚举?
也许这是一个非常愚蠢的问题,但我在Google上找不到答案。
Godot中有一个节点 - textedit。它有几个枚举。其中之一叫做Menuitems。其中之一是菜单_clear和灰色的“ = 3”。它的描述指出:“擦除整个文本文本。”
我的问题是:如何使用此信息清除整个文本文本?
我尝试简单地访问它,然后
$TextEdit.MENU_CLEAR -> nothing
尝试将其放置为很长的
$TextEdit.MenuItems.MENU_CLEAR -> nothing
尝试将其作为函数调用
$TextEdit.MENU_CLEAR() -> nothing
而
print($TextEdit.MENU_CLEAR) -> "3"
路,一旦我致电菜单_clear,就应该执行。但是我做错了什么。 PLS,帮助
编辑1: 这是我的一件代码(只需检查有效的方法,一次在一行时运行所有内容):
if $TextEdit.get_line_count() > 6:
$TextEdit.text = ""
$TextEdit.menu_option($TextEdit.MENU_CLEAR)
这是我想做的:
if $TextEdit.get_line_count() > 6:
$TextEdit.select(1, 0, 1, 0) #selects the oldest line
$TextEdit.menu_option($TextEdit.MENU_CUT) #deletes it
这是我现在得到的屏幕截图 请参阅右侧的滑块 这就是我想要的 只有6行 因此,屏幕上总是只有6行,并且应删除旧线。那是我遇到枚举问题的地方,这就是帖子的原因。 PS 仅阅读设置为false
Maybe it's a really stupid question, but I can't find the answer on Google.
There is a node in Godot - TextEdit. It has several enums. One of them is called MenuItems. One of these is MENU_CLEAR and "= 3" in grey. The description for it states: "Erases the whole TextEdit text."
My question is: how do I use this info to clear the whole TextEdit text?
I tried simply accessing it with
$TextEdit.MENU_CLEAR -> nothing
I tried going the long way
$TextEdit.MenuItems.MENU_CLEAR -> nothing
I tried putting it as a function call
$TextEdit.MENU_CLEAR() -> nothing
However, if I try to print it, I get it's number
print($TextEdit.MENU_CLEAR) -> "3"
The description of MENU_CLEAR, at least for me, implies there is a function somewhere there, that should be executed once I make a call to MENU_CLEAR. But I do something wrong. Pls, help
Edit1:
Here's my piece of code (just checking what works, running everything after IF one line at a time):
if $TextEdit.get_line_count() > 6:
$TextEdit.text = ""
$TextEdit.menu_option($TextEdit.MENU_CLEAR)
Here's what I want to do:
if $TextEdit.get_line_count() > 6:
$TextEdit.select(1, 0, 1, 0) #selects the oldest line
$TextEdit.menu_option($TextEdit.MENU_CUT) #deletes it
Here's the screenshot of what I get now
See the slider on the right side
And here's what I want it to be
Just 6 lines
So there should always be only 6 lines at once on the screen, and the old lines should be deleted. That's where I encountered problems with enums, and that was the reason for the post.
P.S. Read only is set to false
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
清除textedit
我将使用
$ textedit.text =“”
清除所有文本。解决您的问题。
菜单_Clear
是一个常数。它具有值3
。就其本身而言,它无能为力3
不会做。但是,
enum
(是具有名称的常数列表),因为某些东西使用它们。要找出什么,我建议您在文档中搜索enum
的名称(在这种情况下为menuitems
)(尤其是同一类)。在这种情况处> Menu_option 方法。从文档中:
因此,您会这样使用它:
是的,这也有效:
或者您可以这样做(这是同一件事,除了它不太清楚它的含义):
从Textedit开始时删除线条,
让我们说我们想要开始时删除线路。我们将删除的行数为
lines_to_remove
(例如1
)。我们可以使用
Menu_option
来执行此操作。这样:选择要删除的行。换句话说,选择“形式”第一行的开始(
0
),到将留下的第一行的开始(lines_to_remove
):这将删除所选文本。但是,它也将设置剪贴板。
另外,我们可以操纵
文本
。例如:在这里,我得到文本
$ textedit.text
,然后按新行分开,这给了我array
。然后,我将要保留的array
串联(即跳过要删除的元素),然后将结果设置回$ textedit.text.text
。无论采用哪种方法,您都会发现光标将被放置在文本开始时。
因此,我们将在操纵文本之前存储位置:
然后将其放回...好吧,实际上我们要减去我们删除的行数,然后将其放回原处:
如果这导致了负面
CURSOR_LINE
,将其放置在第一行上。我想我可以检查并设置cursor_line
和cursor_column
在这种情况下为零,但是尝试此操作时并不是问题。限制数字 。
现在,如果您想确保最多有几行(例如六行),则现在要确保最多有一条 我们可以阅读我们拥有多少行,减去您允许的最大值,如果结果为正,那就是必须删除多少行:
您为什么需要执行此操作,而不仅仅是删除第一行?因为用户可能会一次粘贴多行。说到哪个,这旨在在
“ text_changed”
信号上运行(尽管它也可以在_process
)上运行剪贴板。
操纵
文本
属性也有效(并且不编写剪贴板)。这是代码:Clearing TextEdit
I would use
$TextEdit.text = ""
to clear all the text.To address your question. The
MENU_CLEAR
is a constant. It has the value3
. By itself it does nothing a3
would not do.However,
enum
(which are lists of constants with a name) are defined because something uses them. To find out what, I suggest you search for the name of theenum
(which isMenuItems
in this case) in the documentation (in particular on the same class).In this case we find the
menu_option
method. From the documentation:Thus, you would use it like this:
Yes, this also works:
Or you can do it like this (which is the same thing, except it less clear what it means):
Removing lines from the start of TextEdit
Let us say we want to remove lines at the start. The number of lines we will remove will be
lines_to_remove
(e.g.1
).We can do this using
menu_option
. Like this:Select the lines we want to remove. In other words, select form the start of the first line (
0
), to the start of the first line that will stay (lines_to_remove
):Clearcut:This will remove the selected text. However, it will also set the clipboard.
Alternatively, we can manipulate
text
. For example:Here I get the text
$TextEdit.text
and split it by the new line, which gives me anArray
. Then I concatenate the elements form theArray
that I want to keep (i.e. skipping the ones I want to remove), And set the result back to$TextEdit.text
.Regardless of the approach you will find that the cursor will be placed at the start of the text.
So, we are going to store the position before manipulating the text:
And then set it back... Well, actually we want to subtract the number of lines we removed and then set it back:
If this resulted in a negative
cursor_line
, it will be placed on the first line. I suppose I could check and set bothcursor_line
andcursor_column
to zero in that case, but it has not been a problem while trying this out.Limiting the number of lines of TextEdit
Now, if you want to make sure you have at most a give number of lines (e.g. six lines). We can read how many lines we have, subtract the maximum you allow, and if the result is positive, that is how many lines have to remove:
Why would you need to do this instead of just removing the first line? Because the user might paste multiple lines at once. Speaking of which, this is intended to run on the
"text_changed"
signal (although it would also work on_process
):Since this version is cutting the text, it writes the clipboard.
Manipulating the
text
property also works (and does not write the clipboard). This is the code: