vanilladocs
vanillapunch.com
API / misc/dialogue

misc/dialogue

대사 진행기와 대본 파서.

  • 원본 misc/dialogue.js 를 옮긴 것이다. (DialogueRunner, DialogueScriptParser)
  • DialogueRunner: 대사, 선택지, 라벨, 점프로 된 대본을 차례대로 풀어 간다. 비주얼노벨 / RPG 대화 / 튜토리얼이 제각각 만들던 상태기계다.
runner.set_script(entries); runner.start(None);
runner.get_current()   → DialogueCurrent::Line / choice / finished
대사에서 runner.advance(), 선택지에서 runner.choose(번호).
  • DialogueScriptParser: 한 줄 명령어 글을 대본으로 바꾼다.
/label 이름표
/jump 이름표
/speaker 화자이름              (이후 /say 의 기본 화자)
/say 대사 내용
/option 보기 글 -> 이름표       (이어 쓴 /option 은 선택지 하나로 묶인다)
/end

"/" 로 시작하지 않는 줄은 바로 앞 화자의 /say 로 본다. "//" 는 주석.

  • 원본과 다른 점: 대본 항목은 여러 필드를 가진 객체 대신 DialogueEntry 열거형이다. 한 항목이 라벨이면서 대사일 수는 없다. get_current 는 { kind, ... } 객체 대신 DialogueCurrent 열거형을 돌려주고, 선택지의 options 는 보기 글 목록(Vec<String>)이다.

Dialogue runner and script parser.

  • A port of the original misc/dialogue.js. (DialogueRunner, DialogueScriptParser)
  • DialogueRunner: steps through a script made of lines, choices, labels, and jumps in order. The state machine that visual novels / RPG dialogues / tutorials each built on their own.
runner.set_script(entries); runner.start(None);
runner.get_current()   → DialogueCurrent::Line / choice / finished
runner.advance() on a line, runner.choose(index) on a choice.
  • DialogueScriptParser: turns one-line command text into a script.
/label label_name
/jump label_name
/speaker speaker_name              (default speaker for the following /say)
/say line text
/option option text -> label_name       (consecutive /option lines are grouped into one choice)
/end

Lines not starting with "/" are treated as /say of the preceding speaker. "//" is a comment.

  • Differences from the original: Script entries are the DialogueEntry enum instead of objects with several fields. One entry cannot be both a label and a line. get_current returns the DialogueCurrent enum instead of a { kind, ... } object, and a choice's options are a list of option texts (Vec<String>).

DialogueOptionstruct traits Clone, Debug, PartialEq, Eq

소스source

선택지의 보기 하나. (원본 { text, jump? })

One option of a choice. (original { text, jump? })

예제Example대본을 항목으로 바로 짓고 점프 없는 보기 고르기Build a script directly from entries and pick an option without a jump
use vanilla::{DialogueCurrent, DialogueEntry, DialogueOption, DialogueRunner};

let entry_list = vec![
	DialogueEntry::Choice(vec![
		DialogueOption { text: String::from("Yes"), jump: Some(String::from("yes")) },
		DialogueOption { text: String::from("No"), jump: None },
	]),
	DialogueEntry::Line { speaker: String::from("Guide"), text: String::from("Maybe later.") },
	DialogueEntry::End,
	DialogueEntry::Label(String::from("yes")),
	DialogueEntry::Line { speaker: String::from("Guide"), text: String::from("Great!") },
];
let mut dialogue_runner = DialogueRunner::new();
dialogue_runner.set_script(entry_list);
dialogue_runner.start(None);
dialogue_runner.choose(1);
assert_eq!(dialogue_runner.get_current(), DialogueCurrent::Line { speaker: String::from("Guide"), text: String::from("Maybe later.") });
필드fieldpub text: String#0.3.2 부터since 0.3.2소스source

보기 글.

Option text.

반환ReturnsString

필드fieldpub jump: Option<String>#0.3.2 부터since 0.3.2소스source

고르면 옮겨 갈 라벨 이름. None 이거나 모르는 라벨이면 선택지 다음 항목으로 간다.

Name of the label to move to when picked. If None or an unknown label, goes to the entry after the choice.

반환ReturnsOption<String>

DialogueRunnerstruct traits Clone, Debug, Default

짝Counterpart: vanilla.js DialogueRunner소스source

대사 진행기.

Dialogue runner.

예제Example대본을 풀어 가며 대사를 넘기고 보기를 고르기Step through a script, advancing lines and picking options
use vanilla::{DialogueCurrent, DialogueRunner, DialogueScriptParser};

let script_text = "
/speaker Clerk
/say Welcome.
/option Go on an adventure -> adventure
/option Go home
/label home
/say Goodbye.
/end
/label adventure
/say Let's go!
";
let mut dialogue_runner = DialogueRunner::new();
dialogue_runner.set_script(DialogueScriptParser::parse(script_text));
dialogue_runner.start(None);
assert_eq!(dialogue_runner.get_current(), DialogueCurrent::Line { speaker: String::from("Clerk"), text: String::from("Welcome.") });

dialogue_runner.advance();
assert_eq!(dialogue_runner.get_current(), DialogueCurrent::Choice { options: vec![String::from("Go on an adventure"), String::from("Go home")] });

dialogue_runner.choose(0);
assert_eq!(dialogue_runner.get_current(), DialogueCurrent::Line { speaker: String::from("Clerk"), text: String::from("Let's go!") });

dialogue_runner.advance();
assert!(dialogue_runner.is_finished());
assert_eq!(dialogue_runner.get_current(), DialogueCurrent::Finished);
생성자constructorpub fn new() -> DialogueRunner#0.3.2 부터since 0.3.2소스source

생성. (빈 대본, 끝난 상태)

Constructor. (empty script, finished state)

반환ReturnsDialogueRunner 대본이 없는 진행기. (set_script 와 start 로 시작한다)A runner with no script. (start with set_script and start)

짝Counterpart: vanilla.js DialogueRunner.constructor

메서드methodpub fn set_script(&mut self, entry_list: Vec<DialogueEntry>)#0.3.2 부터since 0.3.2소스source

대본 설정. (라벨 자리를 표로 만들고, 끝난 상태로 둔다. 같은 라벨이 여럿이면 뒤의 것)

Sets the script. (builds a table of label positions and leaves it in the finished state. If a label appears more than once, the later one wins)

entry_listVec<DialogueEntry>
대본 항목 목록. (DialogueScriptParser::parse 의 결과나 손으로 지은 목록)List of script entries. (the result of DialogueScriptParser::parse or a hand-built list)

짝Counterpart: vanilla.js DialogueRunner.setScript

메서드methodpub fn start(&mut self, label_name: Option<&str>)#0.3.2 부터since 0.3.2소스source

시작. (라벨을 넘기면 그 자리부터, 없는 라벨이면 처음부터)

  • 라벨과 점프를 건너 보여 줄 첫 대사나 선택지에 선다. 대본이 비었거나 그런 항목이 없으면 끝난 상태다.

Starts. (from the given label, or from the beginning if the label does not exist)

  • Skips labels and jumps and stops at the first line or choice to show. Finished if the script is empty or has no such entry.
label_nameOption<&str>
시작할 라벨 이름. None 이면 처음부터.Name of the label to start from. None starts from the beginning.

짝Counterpart: vanilla.js DialogueRunner.start

예제Example라벨에서 시작하기Start from a label
use vanilla::{DialogueCurrent, DialogueRunner, DialogueScriptParser};

let mut dialogue_runner = DialogueRunner::new();
dialogue_runner.set_script(DialogueScriptParser::parse("/say First.\n/label second\n/say Second."));
dialogue_runner.start(Some("second"));
assert_eq!(dialogue_runner.get_current(), DialogueCurrent::Line { speaker: String::new(), text: String::from("Second.") });

dialogue_runner.start(Some("missing"));
assert_eq!(dialogue_runner.get_current(), DialogueCurrent::Line { speaker: String::new(), text: String::from("First.") });
메서드methodpub fn get_current(&self) -> DialogueCurrent#0.3.2 부터since 0.3.2소스source

지금 항목 반환.

Returns the current entry.

반환ReturnsDialogueCurrent 지금 보여 줄 대사나 선택지(보기 글 목록, 복사본). 끝났으면 DialogueCurrent::Finished.The line or choice (list of option texts, a copy) to show now. DialogueCurrent::Finished if finished.

짝Counterpart: vanilla.js DialogueRunner.getCurrent

메서드methodpub fn advance(&mut self)#0.3.2 부터since 0.3.2소스source

다음으로. (대사 항목에서 부른다. 선택지 위에서는 무시)

Advances to the next. (called on a line entry, ignored on a choice)

짝Counterpart: vanilla.js DialogueRunner.advance

메서드methodpub fn choose(&mut self, option_index: usize)#0.3.2 부터since 0.3.2소스source

선택. (선택지 항목에서 부른다. 보기의 점프 라벨이 있으면 거기로, 없거나 모르는 라벨이면 다음 항목으로)

  • 끝났거나 지금 항목이 선택지가 아니면 무시한다.

Chooses. (called on a choice entry. Goes to the option's jump label if any, otherwise or for an unknown label to the next entry)

  • Ignored if finished or if the current entry is not a choice.
option_indexusize
고를 보기 번호. (0 부터, 보기 수 이상이면 무시)Index of the option to pick. (from 0, ignored if not less than the option count)

짝Counterpart: vanilla.js DialogueRunner.choose

메서드methodpub fn is_finished(&self) -> bool#0.3.2 부터since 0.3.2소스source

끝났는지.

Whether it has finished.

반환Returnsbool /end 나 대본 끝에 닿았거나 아직 시작하지 않았으면 true.true if /end or the end of the script was reached, or if not started yet.

짝Counterpart: vanilla.js DialogueRunner.isFinished

DialogueScriptParserstruct

짝Counterpart: vanilla.js DialogueScriptParser소스source

대본 파서. (정적 함수 묶음)

Script parser. (a set of static functions)

예제Example한 줄 명령어 글을 대본 항목으로One-line command text into script entries
use vanilla::{DialogueEntry, DialogueOption, DialogueScriptParser};

let entry_list = DialogueScriptParser::parse("/speaker Guide\nHello.\n// comment\n/option Next -> next\n/option Stay\n/label next\n/end");
assert_eq!(entry_list, vec![
	DialogueEntry::Line { speaker: String::from("Guide"), text: String::from("Hello.") },
	DialogueEntry::Choice(vec![
		DialogueOption { text: String::from("Next"), jump: Some(String::from("next")) },
		DialogueOption { text: String::from("Stay"), jump: None },
	]),
	DialogueEntry::Label(String::from("next")),
	DialogueEntry::End,
]);
정적staticpub fn parse(script_text: &str) -> Vec<DialogueEntry>#0.3.2 부터since 0.3.2소스source

해석. (줄마다 앞뒤 공백을 떼고 명령어를 본다. 모르는 명령어는 건너뛴다)

  • 빈 줄과 "//" 로 시작하는 줄은 건너뛴다. 이어 쓴 /option 줄은 다른 줄이 오거나 글이 끝날 때 선택지 하나로 묶는다.

Parses. (trims each line and reads the command. Unknown commands are skipped)

  • Skips empty lines and lines starting with "//". Consecutive /option lines are grouped into one choice when another line comes or the text ends.
script_text&str
대본 글. (줄은 '\n' 으로 나눈다)Script text. (lines are split by '\n')

반환ReturnsVec<DialogueEntry> 대본 항목 목록. (/speaker 는 항목을 만들지 않고 뒤 대사의 화자만 바꾼다)List of script entries. (/speaker makes no entry and only changes the speaker of the following lines)

짝Counterpart: vanilla.js DialogueScriptParser.parse

DialogueEntryenum traits Clone, Debug, PartialEq, Eq

소스source

대본 항목. (원본 { speaker, text } / { label } / { jump } / { choice: [...] } / { end: true })

Script entry. (original { speaker, text } / { label } / { jump } / { choice: [...] } / { end: true })

이름Name값Value설명Description
LineLine{ speaker: String, text: String, }대사. (화자 이름과 대사 글, 화자가 없으면 빈 글)Line. (speaker name and line text, an empty string if there is no speaker)
LabelLabel(String)라벨. (점프할 자리 이름, 진행할 때는 건너뛴다)Label. (name of a jump target, skipped while running)
JumpJump(String)점프. (그 이름의 라벨 다음으로 옮긴다, 모르는 라벨이면 다음 항목으로)Jump. (moves to just after the label with that name, or to the next entry for an unknown label)
ChoiceChoice(Vec<DialogueOption>)선택지. (보기 목록, choose 로 고를 때까지 머문다)Choice. (list of options, stays until picked with choose)
End대본 끝. (여기 닿으면 끝난다)End of script. (finishes when reached)

DialogueCurrentenum traits Clone, Debug, PartialEq, Eq

소스source

지금 보여 줄 것. (원본 { kind: "line" | "choice" | "finished", ... })

What to show now. (original { kind: "line" | "choice" | "finished", ... })

이름Name값Value설명Description
LineLine{ speaker: String, text: String, }대사를 보여 줄 차례. (advance 로 넘긴다)Time to show a line. (advance with advance)
ChoiceChoice{ options: Vec<String>, }선택지를 보여 줄 차례. (보기 글 목록, choose 로 고른다)Time to show a choice. (list of option texts, pick with choose)
Finished끝났거나 시작하지 않았다.Finished or not started.