Your First NPC

Build a talking NPC in your Unity scene.

Prerequisites

  • Installation complete: SDK installed, Editor and Server API keys configured in the Control Panel

Step 1: Create an Entity Mind

Your NPC needs a personality. Create one in the dashboard:

  1. Go to loremind.peekgames.dev
  2. Click Entity Minds > Create
  3. Fill in:
    • Name: Innkeeper Garrick
    • Personality: Friendly tavern keeper who loves gossip. Knows everyone in town.
  4. Click Create

Entity Mind creation form

See Entity Minds for detailed configuration options.


Step 2: Add an NPC to Your Scene

  1. Create a new GameObject (or select an existing character)
  2. Name it Innkeeper
  3. Add Component > LoreMind > LoreMind NPC
  4. In the Inspector, select Innkeeper Garrick from the Entity Mind dropdown

Step 3: Talk to Your NPC

Create a simple test script:

csharp
using UnityEngine;
using Peek.LoreMind;

public class TalkToNPC : MonoBehaviour
{
    [SerializeField] private LoreMindNPC npc;

    void Start()
    {
        TestNPC();
    }

    async void TestNPC()
    {
        Debug.Log("Asking NPC a question...");

        var response = await npc.RespondAsync("Hello! What's your name?");

        if (response.success)
        {
            Debug.Log($"NPC says: {response.response}");
        }
        else
        {
            Debug.LogError($"Error {response.error}: {response.message}");
        }
    }
}
  1. Attach this script to your Innkeeper GameObject
  2. Drag the LoreMindNPC component to the npc field
  3. Press Play
  4. Check the Console for the NPC's response

Quick Reference

Event-Based Pattern

If you prefer events over async/await:

csharp
void Start()
{
    npc.OnResponseReceived.AddListener(OnNPCResponse);
    npc.OnError.AddListener(OnNPCError);
}

public void OnPlayerSpeak(string text)
{
    npc.Respond(text); // Fire and forget
}

void OnNPCResponse(string npcText)
{
    dialogueUI.ShowText(npcText);
}

void OnNPCError(string error)
{
    Debug.LogError($"NPC Error: {error}");
}

With events, OnResponseReceived and OnResponseComplete fire only for a reply, and OnError for a failure. OnRequestFinished fires once when the request ends, success or failure, so check response.success there before showing response.response. See What Happens on Every Request.

Checking Responses

csharp
var response = await npc.RespondAsync("Hello!");

if (response.success)
{
    Debug.Log($"Response: {response.response}");
    Debug.Log($"Character: {response.character}");
}
else
{
    // Never show made-up dialogue on failure: show a "try again" state instead
    Debug.LogWarning($"{response.error}: {response.message}");
}

Troubleshooting

Entity Mind dropdown is empty

  • Check your Editor API Key is configured (starts with sk_editor_)
  • Click the Refresh button next to the dropdown
  • Verify you've created at least one Entity Mind in the dashboard

"Unauthorized" or "Invalid API key"

  • Editor keys start with sk_editor_
  • Server keys start with sk_server_
  • Make sure you copied the full key

NPC not responding

  • Check the Console for error messages
  • Verify internet connection
  • Ensure the Entity Mind is selected in the Inspector

"Insufficient credits"


What's Next?

You have a working NPC. Here's what to explore:

Add Context

Context helps NPCs understand their surroundings:

csharp
npc.SetLocation("The Rusty Anvil Tavern", "Crowded and noisy");
npc.Context.timeOfDay = "evening";

var response = await npc.RespondAsync("Is it always this busy?");
// NPC responds knowing they're in a busy tavern at night

See Context System for details.

Add Voice

Let players speak to NPCs:

Prepare for Production

Before shipping your game, you need to:

  1. Set up a backend to securely route NPC requests
  2. Configure the SDK to use your backend URL (game client builds can't call LoreMind directly)
  3. Have your backend add each player's ID from their verified login (needed for long-term memory)

See Backend Setup for complete instructions.

Full Guides