For AI agents: the complete documentation index is available at /npc-mentality/en/llms.txt, the full documentation bundle is available at /npc-mentality/en/llms-full.txt, and this page is available as Markdown at /npc-mentality/en/usage/relationship.md.

Relationship Contagion System

Emotions spread through NPC friendship networks.

Overview

A dislikes the player
    ↓
A's friend B is affected (×0.5)
    ↓
B's friend C is slightly affected (×0.25)

Result: the entire merchant village dislikes you

RelationshipGraph

A ScriptableObject that stores friendship relationships between NPCs.

Create: Right-click in Project panel → Create → NPC Mentality → Relationship Graph

API

// Register friendship
graph.AddFriendship("merchant_a", "villager_b");
graph.AddFriendship("villager_b", "blacksmith_c");

// Get an NPC's friends list
List<string> friends = graph.GetFriends("merchant_a");

RelationshipContagion

Static helper class. Propagates attitude changes through the friend network.

Propagate

static void Propagate(
    string sourceNpcId,
    float attitudeChange,
    RelationshipGraph graph,
    Dictionary<string, NPCMemory> allNpcs,
    int depth = 2,
    float decay = 0.5f
)
ParameterDescription
sourceNpcIdSource NPC ID for the attitude change
attitudeChangeAttitude change amount to propagate
graphFriendship graph
allNpcsDictionary of all NPCs' NPCMemory
depthPropagation depth (default 2 levels)
decayDecay rate per level (default 0.5 = 50%)

Propagation Calculation

depth 1 (direct friends): attitudeChange × decay
depth 2 (friends of friends): attitudeChange × decay²

Example: attitudeChange = -10, decay = 0.5

  • Direct friends: -5
  • Friends of friends: -2.5

Example

using NpcMentality;
using System.Collections.Generic;

public class VillageManager : MonoBehaviour
{
    public RelationshipGraph graph;
    private Dictionary<string, NPCMemory> _npcs = new();

    private void Start()
    {
        graph.AddFriendship("merchant", "innkeeper");
        graph.AddFriendship("innkeeper", "blacksmith");

        foreach (var npc in FindObjectsOfType<NPCMemory>())
            _npcs[npc.gameObject.name] = npc;
    }

    public void OnPlayerAttacksMerchant()
    {
        _npcs["merchant"].Remember(EventType.Attack, weight: -10f);

        RelationshipContagion.Propagate(
            sourceNpcId: "merchant",
            attitudeChange: -10f,
            graph: graph,
            allNpcs: _npcs,
            depth: 2,
            decay: 0.5f
        );
        // merchant: -10, innkeeper: -5, blacksmith: -2.5
    }
}

Tips

  • Lower decay to 0.3 or less for rumors that fade quickly.
  • Increase depth to 3 or more to affect the entire village.
  • Combine with the Rumor System to spread both rumor content and emotion simultaneously.