How adskdev/godot-photon-plugin works
Purpose and Runtime
This repository builds a native C++ GDExtension for Godot 4.3+ that exposes Photon Realtime/LoadBalancing multiplayer capabilities to Godot projects. The compiled extension is declared by addons/photon/photon_ext.gdextension and runs in-process within the Godot engine.
Build and Native Dependency Boundary
CMake is the build entry point: CMakeLists.txt. It links the Godot C++ bindings and the externally supplied Photon C++ Realtime SDK (Common, Photon, and LoadBalancing libraries). The repository targets Windows and Linux; SDK binaries and Godot bindings are expected as external/submodule dependencies rather than represented in this tree.
GDExtension Registration
src/register_types.cpp and src/register_types.h are the extension initialization boundary. They register the C++ classes with Godot’s ClassDB during GDExtension startup, making Photon client and network-view types available to Godot scripts/scenes. addons/photon/photon_ext.gdextension directs Godot to load the platform-specific native library.
Photon Client and Session Control
src/PhotonClient.h and src/PhotonClient.cpp provide the central Godot-facing Photon Realtime client. This component owns the integration with Photon LoadBalancing: connection to the Master Server, lobby and room lifecycle, matchmaking, player/room properties, scene loading, RPC routing, and network object ownership. It forms the primary boundary between Godot APIs and Photon callbacks/events.
Network Object Identity and RPC Layer
src/PhotonView.h and src/PhotonView.cpp define the base networked-object abstraction. Photon views associate Godot objects with Photon identities, coordinate instantiation/destruction, track ownership, and dispatch RPCs with Photon target policies such as all clients, other clients, master client, and buffered recipients.
State Replication Views
Specialized views implement replication for common Godot node state:
• src/PhotonTransformView.h / src/PhotonTransformView.cpp synchronize transforms with smoothing/dead reckoning.
• src/PhotonRigidbodyView.h / src/PhotonRigidbodyView.cpp synchronize physics-body state and motion prediction.
• src/PhotonAnimatorView.h / src/PhotonAnimatorView.cpp synchronize animation-related state.
These classes sit between local Godot scene nodes and Photon event serialization, applying received state to local objects while publishing authoritative/local changes.
Primary Control Flow
At engine startup, Godot loads the extension manifest, invokes registration, and exposes the registered classes. Game scripts configure and drive PhotonClient, which calls Photon Realtime for connection, matchmaking, and room operations. Network instantiation creates or resolves PhotonView instances; their specialized child views serialize local state outbound and consume incoming Photon events/RPCs inbound. Photon’s cloud Master Server, lobby, and room infrastructure remain external services.
Documentation Distribution
The public API documentation is generated for Read the Docs from docs/source/index.rst and docs/source/conf.py. The installable Godot-facing artifact is organized under addons/photon/, allowing consumers to copy that directory into a project’s res://addons/.
Main entry points: register_types.cpp Type registration · PhotonView.cpp PhotonView · PhotonClient.cpp PhotonClient · CMakeLists.txt CMake build
Components 15
Build & Packaging
- CMake build · Builds the native extension for Windows and Linux.
CMakeLists.txt - Extension manifest · Selects the platform-specific native library.
addons/photon/photon_ext.gdextension - Native extension library
Godot Extension
- Type registration · Registers exposed C++ classes with Godot ClassDB.
src/register_types.cpp - PhotonClient · Controls connection, matchmaking, rooms, properties, scenes, and Photon callbacks.
src/PhotonClient.cpp - Game scripts & scenes
Network Replication
- PhotonView · Manages Photon identity, ownership, instantiation, destruction, and RPC dispatch.
src/PhotonView.cpp - PhotonTransformView · Synchronizes transforms with smoothing and dead reckoning.
src/PhotonTransformView.cpp - PhotonRigidbodyView · Synchronizes body state and motion prediction.
src/PhotonRigidbodyView.cpp - PhotonAnimatorView · Synchronizes animation-related state.
src/PhotonAnimatorView.cpp
External Runtime
- Godot C++ bindings
- Photon Realtime SDK · Common, Photon, and LoadBalancing libraries.
- Godot engine · Loads and invokes the extension in process.
- Photon cloud · Master Server, lobby, and room infrastructure.
Documentation
- API documentation
docs/source/index.rst
Connections 21
- CMake build → Godot C++ bindings: links
- CMake build → Photon Realtime SDK: links
- CMake build → Native extension library: builds
- Extension manifest → Native extension library: loads
- Godot engine → Extension manifest: reads
- Godot engine → Type registration: initializes
- Type registration → PhotonClient: registers
- Type registration → PhotonView: registers
- Type registration → PhotonTransformView: registers
- Type registration → PhotonRigidbodyView: registers
- Type registration → PhotonAnimatorView: registers
- Game scripts & scenes → PhotonClient: drives sessions
- PhotonClient → Photon Realtime SDK: uses LoadBalancing
- Photon Realtime SDK → Photon cloud: connects to
- PhotonClient → PhotonView: routes events and RPCs
- PhotonView → PhotonTransformView: base class
- PhotonView → PhotonRigidbodyView: base class
- PhotonView → PhotonAnimatorView: base class
- PhotonTransformView → Photon Realtime SDK: serializes state
- PhotonRigidbodyView → Photon Realtime SDK: serializes state
- PhotonAnimatorView → Photon Realtime SDK: serializes state