Struct yarnspinner::runtime::Dialogue
source · pub struct Dialogue { /* private fields */ }
Expand description
Co-ordinates the execution of Yarn programs.
The main functions of interest are Dialogue::continue_
and Dialogue::set_selected_option
.
Implementations§
source§impl Dialogue
impl Dialogue
sourcepub fn new(
variable_storage: Box<dyn VariableStorage>,
text_provider: Box<dyn TextProvider>
) -> Dialogue ⓘ
pub fn new( variable_storage: Box<dyn VariableStorage>, text_provider: Box<dyn TextProvider> ) -> Dialogue ⓘ
Creates a new Dialogue
instance with the given VariableStorage
and TextProvider
.
- The
TextProvider
is used to retrieve the text of lines and options. - The
VariableStorage
is used to store and retrieve variables.
If you don’t need any fancy behavior, you can use StringTableTextProvider
and MemoryVariableStorage
.
source§impl Dialogue
impl Dialogue
sourcepub fn language_code(&self) -> Option<&Language>
pub fn language_code(&self) -> Option<&Language>
sourcepub fn set_language_code(
&mut self,
language_code: impl Into<Option<Language>>
) -> Option<Language>
pub fn set_language_code( &mut self, language_code: impl Into<Option<Language>> ) -> Option<Language>
Sets the Dialogue
’s language. A value of None
means that you are using the base language, i.e. the one the Yarn files are written in.
Returns the last language code.
sourcepub fn library(&self) -> &Library
pub fn library(&self) -> &Library
Gets the Library
that this Dialogue uses to locate functions.
When the Dialogue is constructed, the Library is initialized with
the built-in operators like +
, -
, and so on.
sourcepub fn library_mut(&mut self) -> &mut Library
pub fn library_mut(&mut self) -> &mut Library
See Dialogue::library
.
sourcepub fn line_hints_enabled(&self) -> bool
pub fn line_hints_enabled(&self) -> bool
Gets whether Dialogue::next
is able able to return DialogueEvent::LineHints
events.
The default is false
.
sourcepub fn set_line_hints_enabled(&mut self, enabled: bool) -> &mut Dialogue ⓘ
pub fn set_line_hints_enabled(&mut self, enabled: bool) -> &mut Dialogue ⓘ
Mutable gets whether Dialogue::next
is able able to return DialogueEvent::LineHints
events.
The default is false
.
sourcepub fn text_provider(&self) -> &dyn TextProvider
pub fn text_provider(&self) -> &dyn TextProvider
Gets the currently registered TextProvider
.
sourcepub fn text_provider_mut(&mut self) -> &mut dyn TextProvider
pub fn text_provider_mut(&mut self) -> &mut dyn TextProvider
Mutable gets the currently registered TextProvider
.
sourcepub fn variable_storage(&self) -> &dyn VariableStorage
pub fn variable_storage(&self) -> &dyn VariableStorage
Gets the currently registered VariableStorage
.
sourcepub fn variable_storage_mut(&mut self) -> &mut dyn VariableStorage
pub fn variable_storage_mut(&mut self) -> &mut dyn VariableStorage
Mutable gets the currently registered VariableStorage
.
source§impl Dialogue
impl Dialogue
sourcepub fn continue_(&mut self) -> Result<Vec<DialogueEvent>, DialogueError>
pub fn continue_(&mut self) -> Result<Vec<DialogueEvent>, DialogueError>
Starts, or continues, execution of the current program.
Calling this method returns a batch of DialogueEvent
s that should be handled by the caller before calling Dialogue::continue_
again.
Some events can be ignored, however this method will error if the following events are not properly handled:
DialogueEvent::Options
indicates that the program is waiting for the user to select an option. The user’s selection must be passed toDialogue::set_selected_option
before callingDialogue::continue_
again.DialogueEvent::DialogueComplete
means that the program reached its end. When this occurs,Dialogue::set_node
must be called beforeDialogue::continue_
is called again.
See the documentation of DialogueEvent
for more information on how to handle each event.
The Iterator
implementation of Dialogue
is a convenient way to call Dialogue::next
repeatedly, although it panics if an error occurs.
§Implementation Notes
All handlers in the original were converted to DialogueEvent
s because registration of complex callbacks is very unidiomatic in Rust.
Specifically, we cannot guarantee Send
and Sync
properly without a lot of std::sync::RwLock
boilerplate. The original implementation
also allows unsound parallel mutation of Dialogue
’s state, which would result in a deadlock in our case.
sourcepub fn replace_program(&mut self, program: Program) -> &mut Dialogue ⓘ
pub fn replace_program(&mut self, program: Program) -> &mut Dialogue ⓘ
sourcepub fn add_program(&mut self, program: Program) -> &mut Dialogue ⓘ
pub fn add_program(&mut self, program: Program) -> &mut Dialogue ⓘ
Merges the currently set Program
with the given one. If there is no program set, the given one is set.
sourcepub fn set_node(
&mut self,
node_name: impl Into<String>
) -> Result<&mut Dialogue, DialogueError>
pub fn set_node( &mut self, node_name: impl Into<String> ) -> Result<&mut Dialogue, DialogueError>
Prepares the Dialogue
that the user intends to start running a node.
After this method is called, you call Dialogue::next
to start executing it.
If Dialogue::line_hints_enabled
has been set, the next Dialogue::next
call will return a DialogueEvent::LineHints
,
as the Dialogue determines which lines may be delivered during the node_name
node’s execution.
§Errors
Returns an error if no node with the value of node_name
has been loaded.
sourcepub fn pop_line_hints(&mut self) -> Option<Vec<LineId>>
pub fn pop_line_hints(&mut self) -> Option<Vec<LineId>>
Attempts to pop the line hints that were generated by the last Dialogue::set_node
call.
Panics if Dialogue::line_hints_enabled
is false
.
sourcepub fn stop(&mut self) -> Vec<DialogueEvent>
pub fn stop(&mut self) -> Vec<DialogueEvent>
Immediately stops the Dialogue
Returns unfinished DialogueEvent
s that should be handled by the caller. The last is guaranteed to be DialogueEvent::DialogueComplete
.
sourcepub fn unload_all(&mut self)
pub fn unload_all(&mut self)
Unloads all nodes from the Dialogue.
sourcepub fn node_names(&self) -> Option<impl Iterator<Item = &str>>
pub fn node_names(&self) -> Option<impl Iterator<Item = &str>>
Gets the names of the nodes in the currently loaded Program, if there is one.
sourcepub fn get_line_id_for_node(&self, node_name: &str) -> Option<LineId>
pub fn get_line_id_for_node(&self, node_name: &str) -> Option<LineId>
Returns the line ID that contains the original, uncompiled source text for a node.
A node’s source text will only be present in the string table if its
tags
header contains rawText
.
Because the Dialogue
API is designed to be unaware
of the contents of the string table, this method does not test to
see if the string table contains an entry with the line ID. You will
need to test for that yourself.
Returns the tags for the node node_name
.
The tags for a node are defined by setting the tags
header in
the node’s source code. This header must be a space-separated list
Returns None
if the node is not present in the program.
sourcepub fn node_exists(&self, node_name: &str) -> bool
pub fn node_exists(&self, node_name: &str) -> bool
Gets a value indicating whether a specified node exists in the Program
.
sourcepub fn current_node(&self) -> Option<String>
pub fn current_node(&self) -> Option<String>
Gets the name of the node that this Dialogue is currently executing.
If Dialogue::next
has never been called, this value will be None
.
sourcepub fn analyse(&self, context: &mut Context) -> &Dialogue ⓘ
pub fn analyse(&self, context: &mut Context) -> &Dialogue ⓘ
Analyses the currently loaded Yarn program with the given Context
. Call Context::finish_analysis
afterwards to get the results.
sourcepub fn set_selected_option(
&mut self,
selected_option_id: OptionId
) -> Result<&mut Dialogue, DialogueError>
pub fn set_selected_option( &mut self, selected_option_id: OptionId ) -> Result<&mut Dialogue, DialogueError>
Signals to the Dialogue
that the user has selected a specified DialogueOption
.
After the Dialogue emitted a DialogueEvent::Options
in Dialogue::continue_
, this method must be called before Dialogue::next
is called.
The ID number that should be passed as the parameter to this method should be the OptionId
field in the DialogueOption
that represents the user’s selection.
§Panics
- If the Dialogue is not expecting an option to be selected.
- If the option ID is not found in the vector of
DialogueOption
provided byDialogueEvent::Options
.
§See Also
sourcepub fn is_active(&self) -> bool
pub fn is_active(&self) -> bool
Gets a value indicating whether the Dialogue is currently executing Yarn instructions.
sourcepub fn is_waiting_for_option_selection(&self) -> bool
pub fn is_waiting_for_option_selection(&self) -> bool
Returns true
if the last call to Dialogue::continue_
returned DialogueEvent::Options
and the dialogue is therefore
waiting for the user to select an option via Dialogue::set_selected_option
. If this is true
, calling Dialogue::continue_
will error
and Dialogue::next
will panic.
Trait Implementations§
source§impl Iterator for Dialogue
impl Iterator for Dialogue
source§fn next(&mut self) -> Option<<Dialogue as Iterator>::Item>
fn next(&mut self) -> Option<<Dialogue as Iterator>::Item>
Panicking version of Dialogue::continue_
.
§type Item = Vec<DialogueEvent>
type Item = Vec<DialogueEvent>
source§fn next_chunk<const N: usize>(
&mut self
) -> Result<[Self::Item; N], IntoIter<Self::Item, N>>where
Self: Sized,
fn next_chunk<const N: usize>(
&mut self
) -> Result<[Self::Item; N], IntoIter<Self::Item, N>>where
Self: Sized,
iter_next_chunk
)N
values. Read more1.0.0 · source§fn size_hint(&self) -> (usize, Option<usize>)
fn size_hint(&self) -> (usize, Option<usize>)
1.0.0 · source§fn count(self) -> usizewhere
Self: Sized,
fn count(self) -> usizewhere
Self: Sized,
1.0.0 · source§fn last(self) -> Option<Self::Item>where
Self: Sized,
fn last(self) -> Option<Self::Item>where
Self: Sized,
source§fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>>
fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>>
iter_advance_by
)n
elements. Read more1.0.0 · source§fn nth(&mut self, n: usize) -> Option<Self::Item>
fn nth(&mut self, n: usize) -> Option<Self::Item>
n
th element of the iterator. Read more1.28.0 · source§fn step_by(self, step: usize) -> StepBy<Self>where
Self: Sized,
fn step_by(self, step: usize) -> StepBy<Self>where
Self: Sized,
1.0.0 · source§fn chain<U>(self, other: U) -> Chain<Self, <U as IntoIterator>::IntoIter>
fn chain<U>(self, other: U) -> Chain<Self, <U as IntoIterator>::IntoIter>
1.0.0 · source§fn zip<U>(self, other: U) -> Zip<Self, <U as IntoIterator>::IntoIter>where
Self: Sized,
U: IntoIterator,
fn zip<U>(self, other: U) -> Zip<Self, <U as IntoIterator>::IntoIter>where
Self: Sized,
U: IntoIterator,
source§fn intersperse(self, separator: Self::Item) -> Intersperse<Self>
fn intersperse(self, separator: Self::Item) -> Intersperse<Self>
iter_intersperse
)separator
between adjacent
items of the original iterator. Read moresource§fn intersperse_with<G>(self, separator: G) -> IntersperseWith<Self, G>
fn intersperse_with<G>(self, separator: G) -> IntersperseWith<Self, G>
iter_intersperse
)separator
between adjacent items of the original iterator. Read more1.0.0 · source§fn map<B, F>(self, f: F) -> Map<Self, F>
fn map<B, F>(self, f: F) -> Map<Self, F>
1.0.0 · source§fn filter<P>(self, predicate: P) -> Filter<Self, P>
fn filter<P>(self, predicate: P) -> Filter<Self, P>
1.0.0 · source§fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F>
fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F>
1.0.0 · source§fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
1.0.0 · source§fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P>
fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P>
1.0.0 · source§fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
1.57.0 · source§fn map_while<B, P>(self, predicate: P) -> MapWhile<Self, P>
fn map_while<B, P>(self, predicate: P) -> MapWhile<Self, P>
1.0.0 · source§fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
n
elements. Read more1.0.0 · source§fn take(self, n: usize) -> Take<Self>where
Self: Sized,
fn take(self, n: usize) -> Take<Self>where
Self: Sized,
n
elements, or fewer
if the underlying iterator ends sooner. Read more1.0.0 · source§fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
1.29.0 · source§fn flatten(self) -> Flatten<Self>
fn flatten(self) -> Flatten<Self>
source§fn map_windows<F, R, const N: usize>(self, f: F) -> MapWindows<Self, F, N>
fn map_windows<F, R, const N: usize>(self, f: F) -> MapWindows<Self, F, N>
iter_map_windows
)f
for each contiguous window of size N
over
self
and returns an iterator over the outputs of f
. Like slice::windows()
,
the windows during mapping overlap as well. Read more1.0.0 · source§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
1.0.0 · source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
source§fn collect_into<E>(self, collection: &mut E) -> &mut E
fn collect_into<E>(self, collection: &mut E) -> &mut E
iter_collect_into
)1.0.0 · source§fn partition<B, F>(self, f: F) -> (B, B)
fn partition<B, F>(self, f: F) -> (B, B)
source§fn is_partitioned<P>(self, predicate: P) -> bool
fn is_partitioned<P>(self, predicate: P) -> bool
iter_is_partitioned
)true
precede all those that return false
. Read more1.27.0 · source§fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
1.27.0 · source§fn try_for_each<F, R>(&mut self, f: F) -> R
fn try_for_each<F, R>(&mut self, f: F) -> R
1.0.0 · source§fn fold<B, F>(self, init: B, f: F) -> B
fn fold<B, F>(self, init: B, f: F) -> B
1.51.0 · source§fn reduce<F>(self, f: F) -> Option<Self::Item>
fn reduce<F>(self, f: F) -> Option<Self::Item>
source§fn try_reduce<F, R>(
&mut self,
f: F
) -> <<R as Try>::Residual as Residual<Option<<R as Try>::Output>>>::TryType
fn try_reduce<F, R>( &mut self, f: F ) -> <<R as Try>::Residual as Residual<Option<<R as Try>::Output>>>::TryType
iterator_try_reduce
)1.0.0 · source§fn all<F>(&mut self, f: F) -> bool
fn all<F>(&mut self, f: F) -> bool
1.0.0 · source§fn any<F>(&mut self, f: F) -> bool
fn any<F>(&mut self, f: F) -> bool
1.0.0 · source§fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
1.30.0 · source§fn find_map<B, F>(&mut self, f: F) -> Option<B>
fn find_map<B, F>(&mut self, f: F) -> Option<B>
source§fn try_find<F, R>(
&mut self,
f: F
) -> <<R as Try>::Residual as Residual<Option<Self::Item>>>::TryType
fn try_find<F, R>( &mut self, f: F ) -> <<R as Try>::Residual as Residual<Option<Self::Item>>>::TryType
try_find
)1.0.0 · source§fn position<P>(&mut self, predicate: P) -> Option<usize>
fn position<P>(&mut self, predicate: P) -> Option<usize>
1.6.0 · source§fn max_by_key<B, F>(self, f: F) -> Option<Self::Item>
fn max_by_key<B, F>(self, f: F) -> Option<Self::Item>
1.15.0 · source§fn max_by<F>(self, compare: F) -> Option<Self::Item>
fn max_by<F>(self, compare: F) -> Option<Self::Item>
1.6.0 · source§fn min_by_key<B, F>(self, f: F) -> Option<Self::Item>
fn min_by_key<B, F>(self, f: F) -> Option<Self::Item>
1.15.0 · source§fn min_by<F>(self, compare: F) -> Option<Self::Item>
fn min_by<F>(self, compare: F) -> Option<Self::Item>
1.0.0 · source§fn unzip<A, B, FromA, FromB>(self) -> (FromA, FromB)
fn unzip<A, B, FromA, FromB>(self) -> (FromA, FromB)
1.36.0 · source§fn copied<'a, T>(self) -> Copied<Self>
fn copied<'a, T>(self) -> Copied<Self>
source§fn array_chunks<const N: usize>(self) -> ArrayChunks<Self, N>where
Self: Sized,
fn array_chunks<const N: usize>(self) -> ArrayChunks<Self, N>where
Self: Sized,
iter_array_chunks
)N
elements of the iterator at a time. Read more1.11.0 · source§fn product<P>(self) -> P
fn product<P>(self) -> P
source§fn cmp_by<I, F>(self, other: I, cmp: F) -> Ordering
fn cmp_by<I, F>(self, other: I, cmp: F) -> Ordering
iter_order_by
)Iterator
with those
of another with respect to the specified comparison function. Read more1.5.0 · source§fn partial_cmp<I>(self, other: I) -> Option<Ordering>
fn partial_cmp<I>(self, other: I) -> Option<Ordering>
PartialOrd
elements of
this Iterator
with those of another. The comparison works like short-circuit
evaluation, returning a result without comparing the remaining elements.
As soon as an order can be determined, the evaluation stops and a result is returned. Read moresource§fn partial_cmp_by<I, F>(self, other: I, partial_cmp: F) -> Option<Ordering>where
Self: Sized,
I: IntoIterator,
F: FnMut(Self::Item, <I as IntoIterator>::Item) -> Option<Ordering>,
fn partial_cmp_by<I, F>(self, other: I, partial_cmp: F) -> Option<Ordering>where
Self: Sized,
I: IntoIterator,
F: FnMut(Self::Item, <I as IntoIterator>::Item) -> Option<Ordering>,
iter_order_by
)Iterator
with those
of another with respect to the specified comparison function. Read moresource§fn eq_by<I, F>(self, other: I, eq: F) -> bool
fn eq_by<I, F>(self, other: I, eq: F) -> bool
iter_order_by
)1.5.0 · source§fn lt<I>(self, other: I) -> bool
fn lt<I>(self, other: I) -> bool
Iterator
are lexicographically
less than those of another. Read more1.5.0 · source§fn le<I>(self, other: I) -> bool
fn le<I>(self, other: I) -> bool
Iterator
are lexicographically
less or equal to those of another. Read more1.5.0 · source§fn gt<I>(self, other: I) -> bool
fn gt<I>(self, other: I) -> bool
Iterator
are lexicographically
greater than those of another. Read more1.5.0 · source§fn ge<I>(self, other: I) -> bool
fn ge<I>(self, other: I) -> bool
Iterator
are lexicographically
greater than or equal to those of another. Read moresource§fn is_sorted_by<F>(self, compare: F) -> bool
fn is_sorted_by<F>(self, compare: F) -> bool
is_sorted
)source§fn is_sorted_by_key<F, K>(self, f: F) -> bool
fn is_sorted_by_key<F, K>(self, f: F) -> bool
is_sorted
)Auto Trait Implementations§
impl !RefUnwindSafe for Dialogue
impl Send for Dialogue
impl Sync for Dialogue
impl Unpin for Dialogue
impl !UnwindSafe for Dialogue
Blanket Implementations§
§impl<T> AnyExt for T
impl<T> AnyExt for T
§fn downcast_ref<T>(this: &Self) -> Option<&T>where
T: Any,
fn downcast_ref<T>(this: &Self) -> Option<&T>where
T: Any,
T
behind reference§fn downcast_mut<T>(this: &mut Self) -> Option<&mut T>where
T: Any,
fn downcast_mut<T>(this: &mut Self) -> Option<&mut T>where
T: Any,
T
behind mutable reference§fn downcast_rc<T>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>>where
T: Any,
fn downcast_rc<T>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>>where
T: Any,
T
behind Rc
pointer§fn downcast_arc<T>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>>where
T: Any,
fn downcast_arc<T>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>>where
T: Any,
T
behind Arc
pointer§fn downcast_box<T>(this: Box<Self>) -> Result<Box<T>, Box<Self>>where
T: Any,
fn downcast_box<T>(this: Box<Self>) -> Result<Box<T>, Box<Self>>where
T: Any,
T
behind Box
pointer§fn downcast_move<T>(this: Self) -> Option<T>
fn downcast_move<T>(this: Self) -> Option<T>
Self
to T
,
useful only in generic context as a workaround for specialization