Connection, Login, and Sessions
Everything goes through the CrowdControl singleton, which is available in every script. It reports what happens through signals, all of which are emitted on the main thread.
Connect and Log In
Connect your signals first, then call connect_to_crowd_control().
func _ready() -> void:
CrowdControl.logged_out.connect(_on_logged_out)
CrowdControl.auth_code_received.connect(_on_auth_code_received)
CrowdControl.login_failed.connect(_on_login_failed)
CrowdControl.logged_in.connect(_on_logged_in)
CrowdControl.connect_to_crowd_control()
func _on_logged_out() -> void:
# No usable login is stored. Show your login button, or log in right away:
CrowdControl.login()
func _on_auth_code_received(code: String, url: String) -> void:
# The streamer signs in at `url` in their browser.
login_label.text = "Sign in with your browser to continue.\nCode: %s\n%s" % [code, url]
func _on_login_failed(message: String) -> void:
login_label.text = "Login failed: %s" % message
func _on_logged_in() -> void:
CrowdControl.start_session()What happens:
connect_to_crowd_control()loads your effects and opens the connection.connectingis emitted, followed byconnected.- If a login from an earlier launch is stored, it is reused and
logged_inis emitted. Otherwiselogged_outis emitted. login()asks Crowd Control for a login code.auth_code_receiveddelivers the code and the URL of the login page.- The plugin opens the URL in the browser, unless
crowd_control/common/auth/auto_open_urlis off. The streamer picks their platform (Twitch, YouTube, ...) on that page and authorizes the game. logged_inis emitted. If anything goes wrong,login_failedcarries the reason instead.
Call login() again to replace a code that expired or was dismissed. CrowdControl.logout() forgets the stored login.
How Long a Login Lasts
The login is stored and reused on the next launch, so the streamer normally logs in once.
- A login is valid for 24 hours. The plugin extends it automatically while the game is connected, and
token_refreshedis emitted each time. - If the game was not running for longer than that, the stored login has expired and
logged_outis emitted on the next connect. logged_outcan also be emitted while the game is running, for example if the login was revoked. Keep a way to calllogin()reachable from within the game.
Sessions
Viewers can only buy effects while a game session is running.
CrowdControl.start_session() # session_started, or session_start_failed(message)
CrowdControl.stop_session() # session_stoppedTo start the session as soon as the login is done, set CrowdControl.auto_starting_session = true before connecting.
For games with a clear playable window, start the session when gameplay begins and stop it when the run ends. Stopping a session ends all running timed effects and refunds every request that is still waiting.
session_paused(paused) is emitted when the streamer pauses or resumes ordering from their side. The session itself keeps running.
Pausing
Set CrowdControl.paused = true whenever effects should not run: pause menus, cutscenes, loading screens. While paused, no new effects start and the timers of timed effects stop. Viewers see the timers stop as well.
Requests that arrive while paused wait for up to 55 seconds and are refunded after that. If a pause can last longer, disable the effects instead so they cannot be bought.
With crowd_control/common/process/use_scene_tree_pause enabled, the plugin follows get_tree().paused on its own.
Reconnects
The connection is re-established automatically when it drops. Crowd Control also closes every connection after two hours, so this happens during normal use.
When it does, disconnected and connection_errored are emitted, followed by connected and logged_in once the connection is back. The session keeps running. Do not treat these signals as the end of the session, and make your logged_in handler safe to run more than once.
Quitting
Call CrowdControl.close() before the game quits and wait for disconnected. This stops the session; without it the session stays open on the streamer's channel.
func _notification(what: int) -> void:
if what == NOTIFICATION_WM_CLOSE_REQUEST:
if CrowdControl.is_connected_to_crowd_control():
CrowdControl.disconnected.connect(get_tree().quit)
CrowdControl.close()
else:
get_tree().quit()This needs get_tree().auto_accept_quit = false, otherwise the game quits before the session is stopped.
Next Step
Continue with Creating a Basic Effect.
