Utils/GameObjectExtensions.cs

Extension methods for game objects and components. Provides CopyPropertiesTo to serialize one Component to JSON, remove the __guid field, and deserialize into another component immediately. Provides GetScenePath to build a path string from a GameObject by recursing through its Parent and concatenating names, returning empty for a Scene.

using Sandbox.Diagnostics;
using Scene = Sandbox.Scene;

namespace KOTH;

public static partial class GameObjectExtensions
{
	public static void CopyPropertiesTo(this Component src, Component dst)
	{
		var json = src.Serialize().AsObject();
		json.Remove("__guid");
		dst.DeserializeImmediately(json);
	}

	public static string GetScenePath(this GameObject go)
	{
		return go is Scene ? "" : $"{go.Parent.GetScenePath()}/{go.Name}";
	}
}