Docs/Your First NPC

Your First NPC

Put a talking Mistscale NPC in your scene with one component and a few lines of code.

One component does the work: MistscaleNPC. Add it to a GameObject, give it the ID of an NPC from your project, and it connects on Play. Sending a message is one method; receiving the reply is one event.

Scene setup

  1. 1

    Create the NPC in the dashboard if you have not already (name, role, personality, optional lore files).

  2. 2

    Copy its NPC ID, either from the dashboard or from the Mistscale → Login window's NPC list.

  3. 3

    In your scene, add the MistscaleNPC component to the character's GameObject. Unity will add the required voice and audio components alongside it automatically.

  4. 4

    Paste the NPC ID into the component's NPC Identity field in the Inspector.

  5. 5

    Press Play. The component connects in the background; watch the Console for the connected message if logging is enabled.

Talking to it from code

Wire one event for replies, call one method to speak. A minimal dialogue script looks like this:

TavernKeeper.cs
using Mistscale.SDK;
using UnityEngine;

public class TavernKeeper : MonoBehaviour
{
    [SerializeField] private MistscaleNPC mira;   // drag in the Inspector

    void Start()
    {
        mira.OnNPCResponseReceived += reply => dialogueUI.Show(reply);
    }

    // Hook this to your chat input field or interaction key
    public void Say(string playerMessage)
    {
        mira.SendChat(playerMessage);
    }
}

That is the entire integration for text. The NPC's memory of this player, its mood, and its grounding against your lore all update automatically with every exchange; the next session picks up where this one left off.

The component surface

NPCId
The NPC this component embodies. Set in the Inspector or from code before Play.
SetPlayerId(id)
Sets the stable player identity for this connection. Call before Play — the NPC connects immediately in Start(). See Player Identities.
IsConnected
True once the realtime connection is established. SendChat before this is true logs a warning and does nothing.
IsIdentityResolved
True once the connection's identity has been established (fires right after connecting, near-instant). SendChat before this is true logs a warning and does nothing — the server would reject it anyway.
SendChat(text)
Sends a player message. The reply arrives via OnNPCResponseReceived, and as audio if the NPC has voice enabled.
OnNPCResponseReceived
Fires with the NPC's full reply text. Subscribe once, typically in Start.
OnAudioResponseReceived
Fires with the reply's voice audio when the NPC speaks. Playback is handled for you; subscribe only if you need the raw audio.
OnPlayerSpeechRecognized
Fires with the transcript of what the player said in voice mode. Useful for showing the player's own words in a chat log.
SetSpatialContext(...)
Updates the NPC's sense of place. Covered in Spatial Context in Unity.
ToggleConversation()
Starts or stops a voice conversation. Covered in Voice Conversations.
Built-in test keybind
In the current SDK version, pressing E while the game runs toggles a voice conversation with the NPC. Handy for testing; remap or remove it before shipping if E means something else in your game.
Who does the NPC think it is talking to?
The moment a connection opens, the SDK resolves its identity automatically — before you can send anything. Without a player id set, it falls back to a GUID persisted on-device, so every player sharing that device collapses into one identity. Call mira.SetPlayerId(yourPlayerId) before Play with your own account id, save-slot id, or platform user id so returning players are recognized correctly — see Player Identities for the full flow and how this shows up in your dashboard.