在编辑器中运行代码

tool 是什么?

tool 是一个强大的代码行,当添加到脚本的顶部时,它会在编辑器中执行。 您还可以决定脚本的哪些部分在编辑器中执行,哪部分在游戏中执行,以及哪部分在两者中均执行。

您可以使用它来做很多事情,它在层次设计中非常有用,可以直观地呈现难以预测的事物。 以下是一些用例:

  • If you have a cannon that shoots cannonballs affected by physics (gravity), you can draw the cannonball’s trajectory in the editor, making level design a lot easier.
  • 如果您有不同跳跃高度的跳线,您可以绘制游戏角色能跳过的最大跳跃高度,也可以让关卡设计变得更容易。
  • 如果您的游戏角色不使用精灵,却使用代码来绘制,您可以在编辑器中执行该绘图代码以查看您的游戏角色。

危险

tool scripts run inside the editor, and let you access the scene tree of the currently edited scene. This is a powerful feature which also comes with caveats, as the editor does not include protections for potential misuse of tool scripts. Be extremely cautious when manipulating the scene tree, especially via Node.queue_free, as it can cause crashes if you free a node while the editor runs logic involving it.

如何使用它

To turn a script into a tool, add the keyword tool at the top of your code.

要检查您当前是否在编辑器中,请使用 : Engine.editor_hint

For example, if you want to execute some code only in the editor, use:

GDScript

C#

  1. if Engine.editor_hint:
  2. # Code to execute when in editor.
  1. if (Engine.EditorHint)
  2. {
  3. // Code to execute when in editor.
  4. }

On the other hand, if you want to execute code only in game, simply negate the same statement:

GDScript

C#

  1. if not Engine.editor_hint:
  2. # Code to execute when in game.
  1. if (!Engine.EditorHint)
  2. {
  3. // Code to execute when in game.
  4. }

Pieces of code do not have either of the 2 conditions above will run both in-editor and in-game.

Here is how a _process() function might look for you:

GDScript

C#

  1. func _process(delta):
  2. if Engine.editor_hint:
  3. # Code to execute in editor.
  4. if not Engine.editor_hint:
  5. # Code to execute in game.
  6. # Code to execute both in editor and in game.
  1. public override void _Process(float delta)
  2. {
  3. if (Engine.EditorHint)
  4. {
  5. // Code to execute in editor.
  6. }
  7. if (!Engine.EditorHint)
  8. {
  9. // Code to execute in game.
  10. }
  11. // Code to execute both in editor and in game.
  12. }

注解

Modifications in editor are permanent. For example, in the following case, when we remove the script, the node will keep its rotation. Be careful to avoid making unwanted modifications.

试试看

在场景中添加一个 Sprite 节点,并将纹理设置为Godot图标。 添加并打开脚本,并将其更改为:

GDScript

C#

  1. tool
  2. extends Sprite
  3. func _process(delta):
  4. rotation_degrees += 180 * delta
  1. using Godot;
  2. using System;
  3. [Tool]
  4. public class MySprite : Sprite
  5. {
  6. public override void _Process(float delta)
  7. {
  8. RotationDegrees += 180 * delta;
  9. }
  10. }

保存脚本并返回编辑器。 现在您应该看到您的对象在旋转。 如果您运行游戏,它也会旋转。

../../_images/rotating_in_editor.gif

注解

如果您没有看到变化,请重新加载场景(关闭它并再次打开)。

现在让我们选择何时运行代码。 将 _process() 函数修改为:

GDScript

C#

  1. func _process(delta):
  2. if Engine.editor_hint:
  3. rotation_degrees += 180 * delta
  4. else:
  5. rotation_degrees -= 180 * delta
  1. public override void _Process(float delta)
  2. {
  3. if (Engine.EditorHint)
  4. {
  5. RotationDegrees += 180 * delta;
  6. }
  7. else
  8. {
  9. RotationDegrees -= 180 * delta;
  10. }
  11. }

保存脚本。 现在,对象将在编辑器中顺时针旋转,但如果您运行游戏,它将逆时针旋转。

Editing variables

Add and export a variable speed to the script. The function set_speed after “setget” is executed with your input to change the variable. Modify _process() to include the rotation speed.

GDScript

C#

  1. tool
  2. extends Sprite
  3. export var speed = 1 setget set_speed
  4. # Update speed and reset the rotation.
  5. func set_speed(new_speed):
  6. speed = new_speed
  7. rotation_degrees = 0
  8. func _process(delta):
  9. rotation_degrees += 180 * delta * speed
  1. using Godot;
  2. using System;
  3. [Tool]
  4. public class MySprite : Sprite
  5. {
  6. private float speed = 1;
  7. [Export]
  8. public float Speed {
  9. get => speed;
  10. set => SetSpeed(value);
  11. }
  12. // Update speed and reset the rotation.
  13. private void SetSpeed(float newSpeed)
  14. {
  15. speed = newSpeed;
  16. RotationDegrees = 0;
  17. }
  18. public override void _Process(float delta)
  19. {
  20. RotationDegrees += 180 * delta * speed;
  21. }
  22. }

注解

其他节点的代码不会在编辑器中运行。 您对其他节点的访问权限被限制了。 您可以访问树和节点及其默认属性,但无法访问用户变量。 如果要这样做,其他节点也必须在编辑器中运行。 AutoLoad节点时无法在编辑器中访问的。

实例化场景

You can instantiate packed scenes normally and add them to the scene currently opened in the editor. Be sure to set the scene root as the owner of all the nodes created this way or the nodes won’t be visible in the editor.

If you are using tool:

GDScript

C#

  1. func _ready():
  2. var node = Spatial.new()
  3. add_child(node) # Parent could be any node in the scene
  4. node.set_owner(get_tree().edited_scene_root)
  1. public override void _Ready()
  2. {
  3. var node = new Spatial();
  4. AddChild(node); // Parent could be any node in the scene
  5. node.Owner = GetTree().EditedSceneRoot;
  6. }

If you are using EditorScript:

GDScript

C#

  1. func _run():
  2. var parent = get_scene().find_node("Parent") # Parent could be any node in the scene
  3. var node = Spatial.new()
  4. parent.add_child(node)
  5. node.set_owner(get_scene())
  1. public override void _Run()
  2. {
  3. var parent = GetScene().FindNode("Parent"); // Parent could be any node in the scene
  4. var node = new Spatial();
  5. parent.AddChild(node);
  6. node.Owner = GetScene();
  7. }

警告

Using tool improperly can yield many errors. It is advised to first write the code how you want it, and only then add the tool keyword to the top. Also, make sure to separate code that runs in-editor from code that runs in-game. This way, you can find bugs more easily.