Skip to content

Commit

Permalink
Merge pull request #184 from stargazing-dino/extend_with_declarations
Browse files Browse the repository at this point in the history
Fix dialogue variables not mimicking actual narrative variables
  • Loading branch information
janhohenheim authored May 17, 2024
2 parents 2536241 + 09457ed commit 9d596d1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
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

0 comments on commit 9d596d1

Please sign in to comment.