Running code in the editor

    You can use it for doing many things, but it is mostly useful in level design for visually presenting things that are hard to predict ourselves. Here are some use cases:

    • If you have jumppads with varying jump heights, you can draw the maximum jump height a player would reach if it jumped on one, also making level design easier.

    To turn a script into a tool, add keyword 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 editor, use:

    GDScript

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

    GDScript

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

    GDScript

    Note

    Modifications in editor are permanent. In our case, when we remove the script, the node will keep its rotation direction. Be careful so that you don’t make unwanted modifications.

    Add a node to your scene and set the texture to Godot icon. Attach and open a script, and change it to this:

    GDScript

    Save the script and return to the editor. You should now see your object rotate. If you run the game, it will also rotate.

    Note

    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:

    GDScript

    Save the script. Now the object will spin clockwise in the editor, but if you run the game, it will spin counter-clockwise.

    Note

    Code from other nodes doesn’t run in the editor. Your access to other nodes is limited. You can access the tree and nodes, and their default properties, but you can’t access user variables. If you want to do so, other nodes have to run in the editor too. AutoLoad nodes cannot be accessed in the editor at all.

    Using tool improperly can yield many errors. It is advised to first write the code how you want it, and only then add the keyword to the top. Also make sure you divide your code into part that runs in editor and part that runs in game. This way you can find your bug easier.