Lingara Lingara 說明文件 學習指南 API 函式庫 App 建立 嵌入 網頁版
語言: 繁體中文

NPC 對話

本頁譯自英文。如兩者有出入,以英文頁面為準。 閱讀英文頁面

你的遊戲中的角色可以用玩家正在學習的語言與其對話,難度貼合玩家的等級。玩家說的每一句話就是一個回合:玩家套件送出這句話,角色的回覆則分段串流傳回。

一個回合攜帶什麼

每個回合都會送出角色所需的全部內容,因為 Lingara 不會在回合之間保留任何對話:

  • npc:誰在說話。它的 name 為 1 到 32 個字元,選用的 persona 是一行描述,1 到 120 個字元,例如「一個賣餃子的小販」。
  • scene:選用,對話發生的地方,1 到 160 個字元。
  • source_lang 和 target_lang:玩家自己的語言以及他們正在學習的語言。兩者必須不同,角色只說 target_lang。
  • level:玩家的等級,從 1 到 9。Lingara 的指南〈程度代表什麼〉說明了這套等級量表。
  • line:玩家剛剛說的話,1 到 500 個字元。
  • history:到目前為止的對話,最早的在前,最多 12 行。每一行都有一個 speaker(player 或 npc)以及它的 text,1 到 500 個字元。

每段文字都是單獨一行。對話由你的遊戲保存:玩家套件為你開啟的每段對話保存一個視窗,把每個完成的回合加入其中,把每則回覆截取為前 500 個字元,並捨棄超過 12 行的最早內容。

Lingara 把人設和場景視為來自你的遊戲的不可信文字。它們影響角色說話的方式,但絕不影響角色被允許做什麼。

回覆如何送達

回覆以文字片段的形式串流傳送,你的遊戲在片段送達時依序附加。片段之間可能會出現通知,例如說明回覆被截斷的通知。回覆完整時,回合以 done 結束;失敗時以 error 結束。回合無法續傳:如果連線中斷,玩家需要再說一遍這句話。

對每個角色一次只說一句話。在角色仍在回覆時說出的話會被拒絕而不是排入佇列,因為每個回合都要計費。

誰付費

只有按用量計費用戶端的玩家才能進行對話,每個回合都向該用戶端計費。玩家的權杖需要 embed:play。

程式碼範例語言

Bevy

fn spawn_seller(mut commands: Commands) {
    let mei = NpcDialogue::new("Mei", "zh", 2)
        .and_then(|npc| npc.with_persona("a dumpling seller"))
        .and_then(|npc| npc.with_scene("the night market"))
        .expect("within Lingara's bounds");
    commands.spawn(mei);
    commands.spawn(Player);
}

/// One turn at a time: a line said while the NPC is replying is a `Busy` error.
fn talk(keys: Res<ButtonInput<KeyCode>>, npcs: Query<Entity, With<NpcDialogue>>, mut say: MessageWriter<NpcSay>) {
    if keys.just_pressed(KeyCode::Digit1) {
        for entity in &npcs {
            say.write(NpcSay { entity, line: "饺子多少钱?".into() });
        }
    }
}

/// `streaming` grows as the reply arrives; `turns` holds the finished ones.
fn show_reply(transcripts: Query<&NpcTranscript, Changed<NpcTranscript>>) {
    for transcript in &transcripts {
        if let Some(partial) = &transcript.streaming {
            info!("…{partial}");
        }
    }
}

Unity

public DialogueId OpenDialogue(string npc, string sourceLang, string targetLang, byte level) =>
    Client.DialogueOpen(new DialogueConfig { Npc = new Npc { Name = npc }, SourceLang = sourceLang, TargetLang = targetLang, Level = level });

// The reply streams back as NpcTurn events: append each Fragment's text to your label.
public RequestId Say(DialogueId dialogue, string line) => Client.DialogueSay(dialogue, line);

Godot C#

public long OpenDialogue(string npc, string sourceLang, string targetLang, byte level) =>
    Client.DialogueOpen(new DialogueConfig { Npc = new Npc { Name = npc }, SourceLang = sourceLang, TargetLang = targetLang, Level = level }).Value;

// The reply streams back as NpcTurnFragment signals, then one NpcTurnFinished.
public long Say(long dialogue, string line) => Client.DialogueSay(new DialogueId(dialogue), line).Value;

GDScript

# A LingaraNpc child per character: npc_name, target_lang and level in the
# inspector. say() returns false while a turn is in flight (turns are never
# queued, because each one is billed).
func talk_to_mei(line: String) -> void:
	if mei.say(line):
		bubble.text = "…"


func _on_mei_replied(text: String) -> void:
	bubble.text = text

Unreal C++

void ALingaraEmbedSnippetActor::Say(const FString& Line)
{
	ELingaraCallResult Result;
	FLingaraEmbedError Error;
	if (Dialogue == 0)
	{
		FLingaraDialogueConfig Config;
		Config.Npc.Name = TEXT("Mei");
		Config.Npc.Persona = TEXT("a tea seller at the night market");
		Config.SourceLang = TEXT("en");
		Config.TargetLang = TEXT("zh");
		Config.Level = 2;
		Dialogue = Lingara()->DialogueOpen(Config, Result, Error);
	}
	Reply.Reset();
	Lingara()->DialogueSay(Dialogue, Line, Result, Error);
}

// The reply streams back as npc_turn events: append each fragment.
void ALingaraEmbedSnippetActor::HandleNpcTurn(const FLingaraNpcTurn& Turn)
{
	if (Turn.Dialogue == Dialogue && Turn.Update.Type == ELingaraNpcTurnUpdateType::Fragment)
	{
		Reply += Turn.Update.Text;
	}
}

Kotlin

fun talkToMei(line: String) {
    val dialogue = npc ?: lingara.dialogueOpen(DialogueConfig(Npc("Mei", "a tea seller"), "the tea house", "en", "zh", 2, emptyList()))
    npc = dialogue
    lingara.dialogueSay(dialogue, line)
}

private fun onNpcTurn(update: NpcTurnUpdate) {
    when (update) {
        is NpcTurnUpdate.Opened -> bubble = ""
        is NpcTurnUpdate.Fragment -> bubble += update.text
        is NpcTurnUpdate.Finished -> bubble = update.text
        else -> Unit
    }
}

Java

void talkToMei(String line) {
  if (npc < 0) {
    Npc mei = new Npc("Mei", "a tea seller");
    npc =
        lingara.dialogueOpen(new DialogueConfig(mei, "the tea house", "en", "zh", 2, List.of()));
  }
  lingara.dialogueSay(npc, line);
}

void onNpcTurn(EmbedEvent.NpcTurn turn) {
  if (turn.getUpdate() instanceof NpcTurnUpdate.Fragment fragment) {
    bubble.append(fragment.getText());
  } else if (turn.getUpdate() instanceof NpcTurnUpdate.Finished finished) {
    bubble.setLength(0);
    bubble.append(finished.getText());
  }
}

另請參閱