Interactable - jimdroberts/FishMMO GitHub Wiki

Description

Abstract base class for interactable objects in the game world. Handles interaction logic, network payloads, and UI display. Implements IInteractable and ISpawnable for scene registration and spawning.


API Access

Fields

  • private const double INTERACT_RATE_LIMIT

    The default rate limit (in milliseconds) between allowed interactions.

  • public float InteractionRange

    The maximum distance (in units) at which a player can interact with this object.

  • private float interactionRangeSqr

    The squared interaction range, used for efficient distance checks.

  • public event Action OnDespawn

    Event invoked when this object is despawned.

  • public ObjectSpawner ObjectSpawner { get; set; }

    Reference to the object spawner responsible for spawning/despawning this object.

  • public SpawnableSettings SpawnableSettings { get; set; }

    Settings for spawning this object (e.g., prefab, spawn rules).

  • public long ID { get; set; }

    Unique ID for this interactable object (used for network sync).

  • public Transform Transform { get; private set; }

    The transform of this object in the scene.

  • public GameObject GameObject { get; }

    The GameObject associated with this interactable.

  • public Color GizmoColor

    The color used for drawing gizmos in the Unity Editor.

Properties

  • public virtual string Name { get; }

    The name of this interactable object (defaults to GameObject name).

  • public virtual string Title { get; }

    The display title for this interactable, shown in the UI.

  • public virtual Color TitleColor { get; }

    The color of the title displayed for this interactable in the UI.

  • public virtual double InteractRateLimit { get; }

    The rate limit (in milliseconds) between allowed interactions for this object.

Methods

  • void Awake()

    Initializes the interactable, sets up transform and range, and registers with the scene or sets up UI.

  • void OnDestroy()

    Called when the object is destroyed. Unregisters this interactable from the scene.

  • public override void ReadPayload(NetworkConnection connection, Reader reader)

    Reads network payload data for this interactable, setting its ID and registering it in the scene. connection (NetworkConnection): The network connection. reader (Reader): The network reader.

  • public override void WritePayload(NetworkConnection connection, Writer writer)

    Writes network payload data for this interactable, sending its ID to the writer. connection (NetworkConnection): The network connection. writer (Writer): The network writer.

  • public virtual void OnAwake()

    Called when the object is awakened. Override to implement custom initialization logic.

  • public void Despawn()

    Despawns this interactable using the assigned ObjectSpawner.

  • public override void ResetState(bool asServer)

    Resets the state of this interactable, clearing event handlers and spawn settings. asServer (bool): True if called on the server.

  • public bool InRange(Transform transform)

    Returns true if the specified transform is within interaction range of this object. Uses squared distance for efficiency. transform (Transform): The transform to check range against. Returns: True if in range, false otherwise.

  • public virtual bool CanInteract(IPlayerCharacter character)

    Returns true if the specified player character can interact with this object. Checks rate limiting and range before allowing interaction. character (IPlayerCharacter): The player character attempting to interact. Returns: True if interaction is allowed, false otherwise.

  • void OnDrawGizmos()

    Draws gizmos for the interactable in the Unity Editor (editor only).


Basic Usage

Setup

  1. Inherit from Interactable for any object that should be interactable in the game world.
  2. Implement or override properties and methods as needed for custom logic.
  3. Attach the derived component to a GameObject in your scene.
  4. Use the interaction system to allow players to interact with the object.

Example

// Example 1: Creating a custom interactable
public class MyCustomInteractable : Interactable {
    public override string Title => "Special Object";
    public override Color TitleColor => Color.cyan;
    public override bool CanInteract(IPlayerCharacter character) {
        // Custom logic...
        return base.CanInteract(character);
    }
}

// Example 2: Checking interaction in gameplay
if (interactable.InRange(player.transform) && interactable.CanInteract(player)) {
    // Allow interaction
}

Best Practices

  • Always use InRange and CanInteract to check for valid interactions.
  • Override OnAwake for custom initialization logic in derived classes.
  • Use the InteractRateLimit property to prevent rapid repeated interactions.
  • Register and unregister interactables with the scene for proper network and gameplay integration.
⚠️ **GitHub.com Fallback** ⚠️