Skip to content

Game.UI.Widgets.PathSegment

Assembly: Assembly-CSharp
Namespace: Game.UI.Widgets

Type: struct PathSegment

Base: System.ValueType, implements IJsonWritable, IJsonReadable, IEquatable

Summary:
Represents a single segment in a UI path used by widgets — the segment can be either a string key (named child) or an integer index (positional child). Designed for lightweight JSON (de)serialization with cohtml (IJsonWriter/IJsonReader) so paths can be transmitted/received to/from the UI layer. The struct is immutable in typical use (fields are public but treated as the data carried by the struct) and supplies implicit conversions from string and int for convenient construction.


Fields

  • private System.String m_Key
    Holds the string key for the segment (nullable). When non-null, the segment represents a named child. Marked with [CanBeNull] in code to indicate it may be null.

  • private System.Int32 m_Index
    Holds the numeric index for the segment. A value of -1 indicates "no index" (unused). When m_Key is null and m_Index != -1, the segment represents a positional child.

Properties

  • public static PathSegment Empty { get; }
    Returns a PathSegment representing an empty/invalid segment (m_Index == -1 and m_Key == null). Useful as a sentinel value when a segment is absent or invalid.

Constructors

  • public PathSegment(string key)
    Creates a PathSegment that represents a named key. Sets m_Key to the provided key and m_Index to -1.

  • public PathSegment(int index)
    Creates a PathSegment that represents an index. Sets m_Index to the provided index and m_Key to null.

Note: as a struct, a default parameterless constructor also exists (m_Key == null, m_Index == 0) but typical valid "empty" sentinel is provided by PathSegment.Empty.

Methods

  • public void Write(IJsonWriter writer)
    Serializes the segment to a cohtml JSON writer:
  • If m_Key != null writes the key as a string.
  • Else if m_Index != -1 writes the index as a number.
  • Otherwise writes a JSON null. Used to send path segments to the UI.

  • public void Read(IJsonReader reader)
    Reads a segment from a cohtml JSON reader by peeking the value type:

  • If the value is a string, reads into m_Key and sets m_Index = -1.
  • If the value is a number, sets m_Key = null and reads into m_Index.
  • Otherwise skips the value and sets segment to empty (m_Key = null, m_Index = -1).

  • public bool Equals(PathSegment other)
    Value equality comparing both m_Key and m_Index. Two segments are equal only when their keys are reference-equal/equals and their indices match. (Note: string equality uses the == operator for strings.)

  • public override bool Equals(object obj)
    Standard override that attempts to unbox/convert obj to PathSegment and delegates to the typed Equals.

  • public override int GetHashCode()
    Combines the hash code of m_Key (0 if null) and m_Index using the common pattern: (keyHash * 397) ^ m_Index.

  • public static bool operator ==(PathSegment left, PathSegment right)
    Equality operator delegating to Equals.

  • public static bool operator !=(PathSegment left, PathSegment right)
    Inequality operator delegating to !Equals.

  • public override string ToString()
    Returns m_Key if not null; otherwise returns m_Index.ToString(). For the empty sentinel (m_Key == null and m_Index == -1) this will return "-1".

  • public static implicit operator PathSegment(string key)
    Implicit conversion allowing a string to be used wherever a PathSegment is expected; constructs a PathSegment(key).

  • public static implicit operator PathSegment(int index)
    Implicit conversion allowing an int to be used wherever a PathSegment is expected; constructs a PathSegment(index).

Usage Example

// Constructing segments
PathSegment segA = new PathSegment("children"); // named segment
PathSegment segB = 3;                           // implicit int -> index segment
PathSegment empty = PathSegment.Empty;

// Serializing to cohtml JSON writer
IJsonWriter writer = /* obtain writer from cohtml context */;
segA.Write(writer); // writes a string
segB.Write(writer); // writes a number
empty.Write(writer); // writes null

// Reading from cohtml JSON reader
IJsonReader reader = /* obtain reader positioned at a value */;
PathSegment seg;
seg.Read(reader); // seg now contains either a string-key or an index or is empty

// Equality checks
if (segA == "children") { /* implicit conversion from string used */ }
if (segB != 2) { /* compare segments by index */ }

// Debug/logging
Debug.Log(segA.ToString()); // prints "children"

Notes and tips: - Use the Empty sentinel when you need to indicate an absent segment instead of relying on default struct values. - The type is intended for UI path manipulation and JSON communication with cohtml; it is small and cheap to copy.