terminalCode Example
GameObject.FindNetworkRoot() extension method
GameObject.FindNetworkRoot() Extension Method
This example shows an extension method for finding the network root of a GameObject hierarchy.
CSHARP
namespace Sandbox;
public static class EngineAdditions
{
extension(GameObject go)
{
/// <summary>
/// Finds the network root of this GameObject hierarchy.
/// Returns the GameObject if it has NetworkMode.Object, otherwise searches up the parent chain.
/// </summary>
public GameObject FindNetworkRoot()
{
if (!go.IsValid()) return null;
if (go.NetworkMode == NetworkMode.Object) return go;
return go.Parent?.FindNetworkRoot();
}
}
}Usage Example
CSHARP
// Find the network root of a GameObject
var networkRoot = myGameObject.FindNetworkRoot();
if (networkRoot.IsValid())
{
Log.Info($"Network root: {networkRoot.Name}");
}Key Features
- Extension Method: Extends GameObject with FindNetworkRoot()
- NetworkMode.Object Check: Returns the GameObject if it's a network root
- Recursive Search: Traverses up the parent chain to find the network root
- Null Safety: Returns null if the GameObject is invalid or no network root found
Use Cases
- Finding the authoritative network object for a hierarchy
- Determining ownership for RPC calls
- Network-aware operations
- Debugging network hierarchies
Notes
- Network roots are GameObjects with NetworkMode.Object
- Children of network roots typically have NetworkMode.Inherit
- Useful for determining which GameObject owns the network state
Was this helpful?