TouchScreenButton in Godot – Complete Guide

Welcome to an exciting voyage into the world of Godot 4, where we’ll explore the essential elements of creating touch-enabled games for mobile devices. As we dive into the functionalities and applications of the TouchScreenButton class, we encourage both fledgling developers and seasoned programmers to discover how this feature can transform gameplay on touch devices. Join us as we unlock the potential of TouchScreenButtons and harness the power of Godot’s robust game development engine.

What Is TouchScreenButton?

TouchScreenButton is a Godot 4 class designed to facilitate the implementation of virtual buttons for touch screen devices. It’s specifically tailored for gameplay, allowing players to interact intuitively with the game through touch.

What Is It For?

The TouchScreenButton class is used to create responsive and multitouch-capable controls within a game. It allows developers to craft interactive elements that are critical for touch screen gameplay, such as character movement or action initiation buttons.

Why Should I Learn About TouchScreenButton?

Understanding the TouchScreenButton is crucial for game developers targeting mobile platforms. It provides a way to create a seamless and engaging user experience, ensuring that players can enjoy the game to its fullest on their touch devices. Learning to use this class effectively opens the door to developing games that feel native to smartphones and tablets.

CTA Small Image

Creating a Simple TouchScreenButton

Let’s start by adding a basic TouchScreenButton to our Godot project. This will serve as the foundation for more complex interactions down the line.

# Add a TouchScreenButton to the scene
var touch_button =

Once the button is added, you can set up an icon for the button, so it’s visible on the screen and the player can interact with it.

# Set an icon texture for the TouchScreenButton
touch_button.texture_normal = preload("res://path_to_your_button_icon.png")

Configuring TouchScreenButton Properties

Next, let’s look at configuring some key properties of the TouchScreenButton to make it respond to touch events properly.

# Set the button to not disappear after being touched
touch_button.visibility_mode = TouchScreenButton.VISIBILITY_ALWAYS

# Enable the button to continue sending input events as long as it's pressed
touch_button.passby_press = true

After setting up the visibility and input mode, it’s often a good practice to define the action associated with the button press within the Input Map.

# Define an action in the Input Map (Project Settings -> Input Map)
var action_name = "jump"

Connecting TouchScreenButton Signals

Signals are pivotal in Godot to trigger responses to events. Here we will connect the ‘pressed’ and ‘released’ signals of our TouchScreenButton to custom functions.

# Connect the 'pressed' signal
touch_button.connect("pressed", self, "_on_TouchScreenButton_pressed")

# Function to handle what happens when the button is pressed
func _on_TouchScreenButton_pressed():
    print("Button Pressed")

# Connect the 'released' signal
touch_button.connect("released", self, "_on_TouchScreenButton_released")

# Function to handle what happens when the button is released
func _on_TouchScreenButton_released():
    print("Button Released")

Responding to TouchScreenButton Input

Now that we have signals connected, we can make our game character jump by responding to the button press.

func _on_TouchScreenButton_pressed():
    if Input.is_action_pressed("jump"):
        # Code to make the character jump

In the case of a released event, you might want to stop an action or trigger animations to revert to their original state.

func _on_TouchScreenButton_released():
    if Input.is_action_just_released("jump"):
        # Code to stop the jump or revert to original animation

This concludes the second part of our tutorial on using the TouchScreenButton in Godot 4. We have covered creating buttons, configuring properties, connecting signals, and responding to input events. Stay tuned for the third part, where we will delve even deeper into the interactivity possibilities provided by the TouchScreenButton!

In this next section, we will expand upon our basic TouchScreenButton implementation and explore additional functionalities and options that are crucial for developing engaging touch controls in your Godot 4 games.

TouchScreenButton offers various methods to programmatically control its behavior and appearance, allowing for a dynamic and adaptive user experience. For example, you can change the button’s visibility in response to game events:

# Make the button visible
touch_button.visible = true

# Hide the button when not needed
touch_button.visible = false

Moreover, you might want to create a toggle button that changes its pressed state without additional player input:

# Set the button as a toggle mode button
touch_button.toggle_mode = true

# Check if the button is currently pressed
if touch_button.is_pressed():
    # Toggle the button state
    touch_button.set_pressed(not touch_button.is_pressed())

Implementing feedback such as changing the button’s appearance when pressed can provide a more intuitive user experience:

# Change the appearance of the button when pressed
touch_button.texture_pressed = preload("res://path_to_your_pressed_button_icon.png")

For games that require holding down a button for continuous actions, you can use the `is_pressed()` method to determine the state of the button and execute corresponding continuous actions:

# Inside the _process function, check for the hold state
func _process(delta):
    if touch_button.is_pressed():
        # Perform continuous action like moving a character

To cater for different device aspect ratios or configurations where buttons need to be moved or adjusted on-the-fly, you can modify the button position programmatically:

# Set the position of the button
touch_button.rect_position = Vector2(100, 200)

If you need to adjust the size of the TouchScreenButton to better fit different screen sizes, you can use the `rect_min_size` property:

# Adjust the button minimum size
touch_button.rect_min_size = Vector2(64, 64)

In games where the layout may change (such as when players unlock new abilities or switch control schemes), you’ll want to add or remove TouchScreenButtons dynamically:

# Removing a TouchScreenButton from the scene

# Dynamically adding a new TouchScreenButton to the scene
var new_touch_button =
new_touch_button.texture_normal = preload("res://your_new_button_icon.png")

These code examples demonstrate the flexibility and range of control you have with the TouchScreenButton class in Godot 4. By leveraging these functionalities, you can create a fully-fledged, touch-optimized control scheme for your mobile game. Keep experimenting with these examples and remember that creative use of TouchScreenButtons can significantly enhance the gameplay experience on touch devices. As you continue your game development journey, consider how you can incorporate these touch elements to engage your players and make your game stand out.

Delving further into the possibilities of the TouchScreenButton in Godot 4, we explore some advanced configurations and scripting tactics that can give your mobile game a polished feel and enhanced interactivity.

Firstly, consider adding haptic feedback, which can be a valuable addition to any game, providing tactile responses to players’ actions for a more immersive experience. While Godot 4 doesn’t have built-in haptic feedback, you can call OS-specific APIs through GDNative or other plugins.

# Placeholder for your haptic feedback function
func provide_haptic_feedback():
    # Call your platform-specific haptic feedback API here

# Connect the 'pressed' signal to trigger haptic feedback
touch_button.connect("pressed", self, "provide_haptic_feedback")

You might also want to adjust the TouchScreenButton’s opacity to signify its interactivity or state within the game, enhancing the visual cues for the player:

# Set the button's self-modulate property to change opacity
touch_button.self_modulate = Color(1, 1, 1, 0.5) # Semi-transparent

Furthermore, in certain game genres, you may need a way to disable the TouchScreenButton without affecting its visibility, such as when controls are temporarily disabled during a cutscene. This can be managed by its ‘disabled’ property:

# Disable the button without hiding it
touch_button.disabled = true

# Enable the button when it should be interactable again
touch_button.disabled = false

For multitouch controls, you’ll want to differentiate between multiple touches. You can assign each TouchScreenButton an exclusive touch index and create conditions based on it:

# Assign exclusive touch indexes
touch_button.touch_index = 0 # Index for the first touch

# Check the touch index during gameplay
if touch_button.touch_index == 0:
    # Perform action associated with the first touch

Another popular feature in mobile games is the ability to drag an on-screen element, like a joystick knob. While TouchScreenButton isn’t designed for drag, you can customize it to emulate this behavior:

# Connect signals for touch drag start and touch drag end
touch_button.connect("touch_drag_start", self, "_on_TouchScreenButton_touch_drag_start")
touch_button.connect("touch_drag_end", self, "_on_TouchScreenButton_touch_drag_end")

# Store the initial position to return to later
var initial_position = touch_button.rect_position

func _on_TouchScreenButton_touch_drag_start(position):
    # Begin drag action

func _on_TouchScreenButton_touch_drag_end():
    # When the drag is finished, reset the position
    touch_button.rect_position = initial_position

Lastly, to cater to the aesthetic of your game, you might want your buttons to animate upon interaction. Godot 4 allows you to trigger animations based on button states using the AnimationPlayer:

# Assuming you have an AnimationPlayer node with animations for pressing and releasing
var animation_player = $AnimationPlayer

# Play the press animation when the button is pressed
func _on_TouchScreenButton_pressed():"button_press")

# Play the release animation when the button is released
func _on_TouchScreenButton_released():"button_release")

By integrating these advanced features, you can achieve a level of detail and responsiveness typical of high-quality mobile games. The complexity can grow with your project, and Godot’s flexibility makes it a perfect choice for both indie developers and AAA studios aiming to fine-tune their touch controls. Remember that the user interface is where your players directly interact with your game, and these subtle touches can contribute significantly to player satisfaction and retention.

Continuing Your Game Development Journey with Godot

Having explored the intricacies of the TouchScreenButton class in Godot 4, you’ve taken valuable steps in enhancing your mobile game development skills. But why stop here? The world of game creation is expansive, and there’s always more to learn and new challenges to conquer.

We at Zenva encourage you to continue building your expertise in Godot, a powerful yet user-friendly engine that’s perfect for crafting cross-platform games. For those eager to deepen their knowledge, our Godot Game Development Mini-Degree is a fantastic next step. This comprehensive course collection will guide you through a variety of vital game development topics, offering project-based learning experiences that culminate in a strong portfolio to showcase your newfound capabilities.

If you’re searching for a broader spectrum of Godot content that encompasses both 2D and 3D game development, feel free to explore our entire catalog of Godot courses. Each course is crafted to boost your career, whether you’re taking your first steps in programming or you’re looking to specialize further. Unleash your creativity, reinforce your learning with our provided quizzes and challenges, and join the ranks of accomplished game developers with Zenva.


Throughout our exploration of the TouchScreenButton in Godot 4, you’ve learned how to implement, customize, and apply advanced techniques to create interactive and responsive touch controls for your mobile games. Remember that each skill you master not only adds to the quality of your current project but also lays the groundwork for more ambitious endeavors in the future. Learning is a continuous journey, and with Godot’s ever-growing capabilities, the potential for innovation is limitless.

Aspiring developers and veterans alike, we invite you to continue this journey with us at Zenva. With our Godot Game Development Mini-Degree, you’re not just learning to program games, you’re gearing up to create worlds. Empower your creative vision with the knowledge and support from our expert-crafted courses, and step forward into the role of a game developer with confidence and expertise. Start shaping your dreams into realities today.

Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.