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 將 persona 摎場景當做從你个遊戲來个、毋做得信任个文字。這兩項會影響角色仰般講話,毋過永遠毋會改變佢做得做麼个。

回覆仰般來

回覆係一節一節个文字串流過來,你个遊戲在到个時節將佢接起來。中間可能會有通知,像講回覆分人切斷了。回覆完成个時節,回合用 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());
  }
}

另外看