Can You Detect Compile Target At Runtime?

Godot Version

4.2

Question

Is there any way to detect the compile target at runtime? I want to display the touch controls if the user is on Android, but hide them if the user is on Windows.

In Godot, you can detect the target platform at runtime using scripting and preprocessor directives to display or hide touch controls based on whether the user is running the application on Android or Windows.

Here is an example script that demonstrates how to show or hide touch controls based on the runtime platform detection:

gdscript
复制代码
extends Node

Define the touch controls node

var touch_controls

func _ready():
# Get the touch controls node
touch_controls = $TouchControls

# Check the current platform
var platform = OS.get_name()
if platform == "Android":
    # Show touch controls
    touch_controls.visible = true
elif platform == "Windows":
    # Hide touch controls
    touch_controls.visible = false

In this script:

OS.get_name() is used to get the name of the current platform.
if conditional statements are used to control the visibility of the touch controls based on the platform name.
Ensure that the touch controls node is correctly named and that its path is accurate in the scene tree. This way, when the game runs on an Android device, the touch controls will be shown, and when it runs on Windows, they will be hidden.

1 Like