For AI agents: the complete documentation index is available at /npc-mentality/zh/llms.txt, the full documentation bundle is available at /npc-mentality/zh/llms-full.txt, and this page is available as Markdown at /npc-mentality/zh/usage/memory.md.
  • 简体中文
  • NPC记忆系统

    NPC 记住玩家的行为并计算累积态度(Attitude)。

    概述

    玩家:
    - 偷了商人的东西3次
    - 救了村民
    
    NPC:
      商人: "你又来了...别动我的东西。"
      村民: "上次谢谢你的帮助。"

    组件

    NPCMemory — MonoBehaviour。添加到 NPC GameObject。

    字段类型默认值说明
    MaxMemoriesint20最大记忆数,超出时删除最旧的
    memoriesList<MemoryEvent>—记忆列表(可在 Inspector 中查看)

    API

    Remember

    void Remember(EventType type, float weight = 1f, string context = "")
    参数说明
    type行为类型(EventType 枚举)
    weight态度影响。正数 = 友好,负数 = 敌对
    context可选描述字符串
    npc.Remember(EventType.Steal,  weight: -3f);
    npc.Remember(EventType.Help,   weight:  5f);
    npc.Remember(EventType.Attack, weight: -10f);
    npc.Remember(EventType.Gift,   weight:  3f);

    GetAttitude

    float GetAttitude()

    返回所有记忆的 weight 总和。

    float attitude = npc.GetAttitude();
    // attitude > 0  → 友好
    // attitude == 0 → 中立
    // attitude < 0  → 敌对

    HasMemory / ForgetOldest

    bool HasMemory(EventType type)
    void ForgetOldest()

    示例

    using NpcMentality;
    
    public class ShopKeeper : MonoBehaviour
    {
        private NPCMemory _memory;
    
        private void Awake() => _memory = GetComponent<NPCMemory>();
    
        public string GetGreeting()
        {
            return _memory.GetAttitude() switch
            {
                < -5f => "滚出去。",
                < 0f  => "你要买什么?",
                < 5f  => "欢迎光临。",
                _     => "希望多来几个像你这样的顾客!"
            };
        }
    }