Lifecycle
How Suduxu initializes, runs, and shuts down across runtime environments.
Suduxu runs as a native runtime controlled by a host environment. Its lifecycle is consistent across platforms, but initialization and execution differ depending on whether it is embedded (Unity) or manually managed (Rust / native bindings).
Initialization
The native runtime must be explicitly loaded before any other API usage.
init_suduxu(configuration)This step:
- Loads the Suduxu ABI (DLL / SO / DYLIB)
- Applies behaviour configuration
- Prepares internal event system
No runtime behaviour starts at this stage.
Initialization is handled automatically by the Suduxu Component prefab.
This step:
- Loads native ABI on scene start
- Reads configuration from
suduxu.json - Initializes client and server wrappers
- Registers internal event handlers
No manual initialization is required for Unity projects.
Launch
launch_suduxu()This step:
- Starts the native server thread
- Registers event callbacks
- Starts TCP and UDP subsystems
- Enables client discovery and connection handling
- Activates input and sensor processing
The runtime becomes active after this call completes.
suduxu.Launch();This step:
- Spawns the server thread
- Registers native callbacks
- Starts networking automatically
- Activates input and sensor pipelines
Typically called automatically during scene startup, but can be manually triggered if needed (handled by the Suduxu Component prefab).
Runtime
During runtime, Suduxu operates on an event-driven system.
Core behaviour
- Spawns the server thread
- Registers native callbacks
- Starts networking automatically
- Activates input and sensor pipelines
Client access
for_client(id);
for_broadcast();The default client id to be used is configured when initializing Suduxu, with 0 reserved for broadcast.
suduxu.Input.For(id);
suduxu.Client.Broadcast();The default client id to be used is configured in the Unity Editor on the Suduxu Component element, with 0 reserved for broadcast.
Shutdown
shutdown_suduxu()This step:
- Stops the native runtime
- Joins the server thread
- Disconnects all clients
- Releases native resources
suduxu.Shutdown();This step:
- Stops the native runtime
- Joins the background thread
- Clears internal state
- Releases native resources
Accessing configuration and addresses
Configuration, password, and address information are only valid after the runtime has fully started. Accessing this information immediately after launch may result in null or default values. To ensure valid data, maybe add a short delay of 100-200ms after launch before reading configuration or address information.
Suduxu allows you to access different configuration and address information through the API:
thread::sleep(Duration::from_millis(200)); // Optional delay to ensure runtime is fully initialized
let config: SuduxuConfig = config(); // Access configuration from `suduxu.json`
let password: Option<u32> = password(); // Access the runtime password
let addresses: Arc<AddressObject> = addresses(); // Access network addresses
let qr: QrCode = qr(); // Access QR code information for client connection to the serverSee Reference/Data-Models for details on the
SuduxuConfig,AddressObjectandQrCodedata models.
// Delay already handled by the `Suduxu Component` prefab, so no manual delay is necessary.
SuduxuConfig config = suduxu.Config; // Access configuration from `suduxu.json`
uint? password = suduxu.Password; // Access the runtime password
AddressObject addresses = suduxu.Addresses; // Access network addresses
// QR-Code will be rendered by passing a `RawImage` component to the `Suduxu Component` prefab, so no direct API access is necessary in Unity.See Reference/Data-Models for details on the
SuduxuConfig,AddressObjectandQrCodedata models.
Lifecycle rules
- Initialization must occur before launch (except Unity automatic setup)
- Runtime is asynchronous and event-driven
- Configuration and addresses are only valid after startup completes
- Unity manages lifecycle automatically
- Rust / native environments require explicit control
Minimal flow
init_suduxu(config);
launch_suduxu();
// Runtime operations...
shutdown_suduxu();// Add Suduxu Component to scene
// Suduxu initializes and launches automatically on scene start
// Runtime operations...
// Suduxu shuts down automatically on scene exitIn order to use the suduxu object in Unity it first needs to be injected, this is achieved the following way:
public class SuduxuExample : MonoBehaviour {
[injected]
private Suduxu suduxu;
}Otherwise it can also be called like this:
Suduxu.Instance