Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dialogue variables not mimicking actual narrative variables #184

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions crates/runtime/src/dialogue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,21 +219,40 @@ impl Dialogue {
self.vm.continue_()
}

fn extend_variable_storage_from(&mut self, program: &Program) {
let initial: HashMap<String, YarnValue> = program
.initial_values
.iter()
.map(|(k, v)| (k.clone(), v.clone().into()))
.collect();

// Extend the VariableStorage with the initial values from the program
if let Err(e) = self.variable_storage_mut().extend(initial) {
error!(
"Failed to populate VariableStorage with initial values: {}",
e
);
}
}

/// Sets or replaces the [`Dialogue`]'s current [`Program`]. The program is replaced, all current state is reset.
pub fn replace_program(&mut self, program: Program) -> &mut Self {
self.vm.program.replace(program);
self.vm.program.replace(program.clone());
self.vm.reset_state();
self.extend_variable_storage_from(&program);
self
}

/// Merges the currently set [`Program`] with the given one. If there is no program set, the given one is set.
pub fn add_program(&mut self, program: Program) -> &mut Self {
if let Some(existing_program) = self.vm.program.as_mut() {
*existing_program = Program::combine(vec![existing_program.clone(), program]).unwrap();
*existing_program =
Program::combine(vec![existing_program.clone(), program.clone()]).unwrap();
} else {
self.vm.program.replace(program);
self.vm.program.replace(program.clone());
self.vm.reset_state();
}
self.extend_variable_storage_from(&program);

self
}
Expand Down
37 changes: 35 additions & 2 deletions crates/yarnspinner/tests/type_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,10 @@ fn test_initial_values() -> anyhow::Result<()> {
.declare_variable(
Declaration::new("$external_str", Type::String).with_default_value("Hello"),
)
.declare_variable(Declaration::new("$external_int", Type::Boolean).with_default_value(true))
.declare_variable(Declaration::new("$external_bool", Type::Number).with_default_value(42))
.declare_variable(Declaration::new("$external_int", Type::Number).with_default_value(42))
.declare_variable(
Declaration::new("$external_bool", Type::Boolean).with_default_value(true),
)
.compile()?;

let mut variable_storage = test_base.variable_storage.clone_shallow();
Expand All @@ -426,6 +428,37 @@ fn test_initial_values() -> anyhow::Result<()> {
Ok(())
}

#[test]
fn test_variable_storage_extended_with_program_initial_values() {
let source = "
<<declare $int = 42>>
<<declare $str = \"Hello\">>
<<declare $bool = true>>
";

let result = Compiler::from_test_source(source).compile().unwrap();

let mut dialogue = Dialogue::new(
Box::new(MemoryVariableStorage::new()),
Box::new(StringTableTextProvider::new()),
);
dialogue.replace_program(result.program.unwrap());

let variable_storage = dialogue.variable_storage();
assert_eq!(
variable_storage.get("$int").unwrap(),
YarnValue::Number(42.0)
);
assert_eq!(
variable_storage.get("$str").unwrap(),
YarnValue::String("Hello".to_string())
);
assert_eq!(
variable_storage.get("$bool").unwrap(),
YarnValue::Boolean(true)
);
}

#[test]
fn test_explicit_types() {
let result = Compiler::from_test_source(
Expand Down
Loading