Is there any way I can invoke a method in my Script from the editor? (for testing purposes)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By d2clon

I come from Unity, and there you can use ContextMenu method attribute. It will add a button in the editor you can click and the method in your script will be invoked.

This is very helpful for testing/debugging purposes. When you are testing a functionality and you want an easy way to trigger it.

Is there something similar in Godot, or any workaround I can use?

:bust_in_silhouette: Reply From: Wakatta

Yes you can.
In fact all of my projects use this and you can build your entire game in the editor hardly ever needing to press the Play button.

What you’re looking for is how to run code in the editor or you can make editor plugins instead.

So the following is one of my personal excerpts you’re free to modify as needed.

  1. Editor > Open Editor Settings Folder
  2. Open script_templates folder and create a new GDScript file

Example Template

tool
extends %BASE%

export(bool) var update = false setget _update_scene


func _ready():
%TS%if Engine.editor_hint:
%TS%%TS%call("connect", "script_changed", self,"_script_changed")
%TS%%TS%_script_changed()

func _script_changed():
%TS%%TS%pass

func _update_scene(_b):
%TS%%TS%pass

Notes

  • Everytime you make changes to the scene there are cases where you
    will need to reload so Save Scene then Reload Saved Scene

  • The _script_changed() function circumvents that to some degree but
    only for changes made to the script itself and you should do things
    like variable initialization there

  • Any exportable variable resource can be used to update the scene when changed
    as demonstrated above

  • To use select Node > Attach Script > Select Template

Thanks for the detailed explanation. This helps a lot to make the game run in editor mode. And this may be very helpful for many scenarios.

In my case, I don’t need to run the whole game in the editor. What I need is to run the game normally in the run window. And then trigger a method of one of my scripts from a custom editor button. Just for testing/debugging purposes. For this method only.

Now that I am writing this, maybe the easiest way is to put a button into the game and catch the click on it. In Godot is insanely easy to add a button and then remove it. So maybe this is the way to go.

d2clon | 2022-12-10 15:17

Wait wait wait.
Hold the phone… You want to

run the game normally in the run window. And then trigger a method of one of my scripts from a custom editor button.

In that case in Debug enable
Debug > Synchronize Scene Changes
Debug > Synchronize Script Changes

And whenever you save the game gets updated

Not entirely sure but believe Godot links the editor and game window through some kind of network (most likely localhost) find which port the above question would be easily solved

Wakatta | 2022-12-10 18:45

:bust_in_silhouette: Reply From: idbrii

If you’re trying to iterate on code you can try making it EditorScript so you can get the run menu item in the code editor.

tool
extends EditorScript

func _run():
  print("Hello from the Godot Editor!")

Then the built-in code editor will have a Run menu item (Ctrl-Shift-X I think).

This doesn’t work at all if you’re using an external editor (the menu item isn’t accessible anywhere).

See the docs for some important limitations in where output is visible.