Platform specific code in C#

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

Is there a way to have platform specific code in Godot so that if your on a specific platform, certain code will run. Something like this maybe:

#if Windows
        //do windows specific thing
#elif Android
        //do android specific thing
#endif
:bust_in_silhouette: Reply From: juppi

Yes. There is a function for that in the Godot.OS Class:

//
// Summary:
//     Returns the name of the host OS. Possible values are: "Android", "iOS", "HTML5",
//     "OSX", "Server", "Windows", "UWP", "X11".
[GodotMethodAttribute("get_name")]
public static string GetName();

Is there a way to do this using preprocessor directives?

jkunable | 2020-07-26 16:23

:bust_in_silhouette: Reply From: CosmoXD

(This is a bit late, I know) In Godot C# you can use preprocessor defines.

public override void _Ready()
    {
#if GODOT_SERVER
        // Don't try to load meshes or anything, this is a server!
        LaunchServer();
#elif GODOT_32 || GODOT_MOBILE || GODOT_WEB
        // Use simple objects when running on less powerful systems.
        SpawnSimpleObjects();
#else
        SpawnComplexObjects();
#endif
    }