Skip to content

About

Godot 4 addon: click-to-play overlay, gesture-safe fullscreen/mouse capture, and pause+mute on tab blur for web (HTML5) exports.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

WebPlayGuard

Tests

A tiny Godot 4 addon that makes your web (HTML5) export start and pause the way players expect, on your own page, on itch.io, or inside any game-portal iframe.

Browsers block audio, fullscreen and pointer lock until the player clicks, and Godot's NOTIFICATION_APPLICATION_FOCUS_* does not fire when the player switches tabs in a web build. WebPlayGuard handles both with one autoload and a few project settings.

What it does

  • Click-to-play overlay. A full-screen "Click to play" layer appears on web. The first click, tap or key press closes it and the game starts. The tree is paused until then (optional).
  • Gesture-safe extras. Optional fullscreen and mouse capture happen inside that first click, which is the only place browsers allow them.
  • Mouse re-capture. Ask for capture with WebPlayGuard.request_mouse_capture(). If the browser drops pointer lock (the player pressed Esc), the next click captures the mouse again.
  • Pause and mute on blur. When the tab is hidden, the window loses focus, or the iframe loses focus, the game pauses and the Master bus mutes. Both are undone when focus returns. If your game had already paused or muted, WebPlayGuard leaves that alone.
  • No-op on desktop. The overlay and the pause/mute handling only run on web exports. Nothing changes in your desktop builds.

Install

  1. Copy addons/webplayguard/ into your project's addons/ folder.
  2. Open Project > Project Settings > Plugins and enable WebPlayGuard. This registers the WebPlayGuard autoload.
  3. Export with the Web preset as usual.

That is all. With the defaults you get the click-to-play overlay and pause/mute on blur.

Settings

The plugin adds these under webplayguard/ (turn on Advanced Settings in Project Settings to see them):

Setting Default Meaning
click_to_play/enabled true Show the overlay on web.
click_to_play/text "Click to play" Overlay text.
click_to_play/pause_until_click true Keep the tree paused until the player clicks.
click_to_play/fullscreen_on_click false Go fullscreen on that first click.
click_to_play/capture_mouse_on_click false Capture the mouse on that first click, and on later clicks if capture is dropped.
focus/pause_on_blur true Pause the tree when the page is hidden or unfocused.
focus/mute_on_blur true Mute the Master bus when the page is hidden or unfocused.
desktop/show_overlay false Also show the overlay outside web (handy for testing the look).

Script API

WebPlayGuard.started.connect(_on_started)            # player clicked through
WebPlayGuard.pause_state_changed.connect(_on_pause)  # we paused or unpaused because of focus

WebPlayGuard.is_started()           # bool
WebPlayGuard.is_web()               # bool
WebPlayGuard.request_mouse_capture()  # capture on the next click, and again after Esc
WebPlayGuard.release_mouse_capture()  # stop wanting capture and show the cursor

Start your music and gameplay from started if you want sound to begin only after the player has clicked.

Demo and tests

This repository is also a Godot project (project.godot) with a small demo in demo/ and headless tests in tests/. These are not part of the addon download.

# Unit tests, no display needed (exit code 0 = all passed)
godot --headless --path . -s tests/run_tests.gd

# Web export of the demo (needs web export templates installed)
mkdir -p web-export
godot --headless --path . --export-debug Web web-export/index.html
python3 -m http.server 8000 --directory web-export   # then open http://localhost:8000

Limitations

  • Works with Godot 4.4 and newer. The headless tests run on every push against Godot 4.4.1, 4.5.2, 4.6.3 and 4.7.2 (see .github/workflows/tests.yml). Versions before 4.4 are not supported.
  • Web exports with thread support need cross-origin isolation headers on your host. That is separate from this addon. The default single-threaded export needs no special headers.
  • On web, "unfocused" is detected from the Page Visibility API, window blur/focus events and document.hasFocus(). Some portals embed games in ways that change when these fire.
  • The browser only allows fullscreen and pointer lock inside a real click or key press. fullscreen_on_click and capture_mouse_on_click therefore apply to the overlay click only. With the overlay disabled, mouse capture waits for the player's next click.
  • Audio is unlocked by the player's click reaching the engine, as Godot's own web audio does. This addon does not run its own audio context.
  • Not tested in Safari or on mobile browsers. Pointer lock and fullscreen behavior in real browsers has not been verified by automated tests.

License

MIT. See LICENSE.

About

Godot 4 addon: click-to-play overlay, gesture-safe fullscreen/mouse capture, and pause+mute on tab blur for web (HTML5) exports.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages