Running code in the editor¶
Co to j the.
Niebezpieczeństwo
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.
I jak go uruchomić¶
To turn a script into a tool, add the keyword
tool at the top of your code.
To check if you are currently in the editor, use:
Engine.editor_hint.
For example, if you want to execute some code only in the editor, use:
if Engine.editor_hint: # Code to execute when in editor.
if (Engine.EditorHint) { // Code to execute when in editor. }
On the other hand, if you want to execute code only in game, simply negate the same statement:
if not Engine.editor_hint: # Code to execute when in game.
if (!Engine.EditorHint) { // Code to execute when in game. }
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:
func _process(delta): if Engine.editor_hint: # Code to execute in editor. if not Engine.editor_hint: # Code to execute in game. # Code to execute both in editor and in game.
public override void _Process(float delta) { if (Engine.EditorHint) { // Code to execute in editor. } if (!Engine.EditorHint) { // Code to execute in game. } // Code to execute both in editor and in game. }
Informacja
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.
Try it out¶
Add a
Sprite node to your scene and set the texture to Godot icon. Attach and open a script, and change it to this:
tool extends Sprite func _process(delta): rotation_degrees += 180 * delta
using Godot; using System; [Tool] public class MySprite : Sprite { public override void _Process(float delta) { RotationDegrees += 180 * delta; } }
Save the script and return to the editor. You should now see your object rotate. If you run the game, it will also rotate.
Informacja
If you don't see the changes, reload the scene (close it and open it again).
Now let's choose which code runs when. Modify your
_process() function to look like this:
func _process(delta): if Engine.editor_hint: rotation_degrees += 180 * delta else: rotation_degrees -= 180 * delta
public override void _Process(float delta) { if (Engine.EditorHint) { RotationDegrees += 180 * delta; } else { RotationDegrees -= 180 * delta; } }
Save the script. Now the object will spin clockwise in the editor, but if you run the game, it will spin counter-clockwise.
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.
tool extends Sprite export var speed = 1 setget set_speed # Update speed and reset the rotation. func set_speed(new_speed): speed = new_speed rotation_degrees = 0 func _process(delta): rotation_degrees += 180 * delta * speed
using Godot; using System; [Tool] public class MySprite : Sprite { private float speed = 1; [Export] public float Speed { get => speed; set => SetSpeed(value); } // Update speed and reset the rotation. private void SetSpeed(float newSpeed) { speed = newSpeed; RotationDegrees = 0; } public override void _Process(float delta) { RotationDegrees += 180 * delta * speed; } }
Informacja.
Instancjowanie scen¶
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:
func _ready(): var node = Spatial.new() add_child(node) # Parent could be any node in the scene node.set_owner(get_tree().edited_scene_root)
public override void _Ready() { var node = new Spatial(); AddChild(node); // Parent could be any node in the scene node.Owner = GetTree().EditedSceneRoot; }
If you are using EditorScript:
func _run(): var parent = get_scene().find_node("Parent") # Parent could be any node in the scene var node = Spatial.new() parent.add_child(node) node.set_owner(get_scene())
public override void _Run() { var parent = GetScene().FindNode("Parent"); // Parent could be any node in the scene var node = new Spatial(); parent.AddChild(node); node.Owner = GetScene(); }
Ostrzeżenie
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.