What's the #if and endif keyword when creating plugins in C#?

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

In the Making Plugins documentation, it has the #if TOOl and endif keywords before and after the script. What does it do? and is it necessary?

:bust_in_silhouette: Reply From: rob405

From the Making Plugins documentation:
“In addition to the EditorPlugin script, any other GDScript that your plugin uses must also be a tool. Any GDScript without tool imported into the editor will act like an empty file!”

I’m not exactly sure why they’re written like this, but the #if TOOLS and #endif tell Godot that you are importing TOOLS in between the two keywords, which is mandatory for creating a plugin. The #if has to always be at the beginning of the script, and the #endif at the end.

Bit late, still for the sake of completeness…
#if” is a C# preprocessor directive. Everything between “#if TOOLS” and “#endif” will only be compiled if the “TOOLS” symbol is defined.
Godot seem to define “TOOLS” when it builds scripts for in-editor debugging, but not when exporting.
Not sure if it is required by Godot, but an editor plugin is not needed in an exported game, and might in fact be harmful depending on what it does, so it is a good idea to always have these directives.

2 Likes