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.
IsConnected
True once the realtime connection is established. SendChat before this is true logs a warning and does nothing.
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?
Each running game instance gets its own player identity by default, so two testers get two separate relationships with the same NPC. For released games, pass your own stable player ID when connecting so returning players are recognized across devices and sessions; the connection accepts a custom player ID before it opens.