Updating variables via script
Posted: Mon Jan 15, 2024 4:05 pm
I've created a script that updates a variable in both dialogue system and in playmaker, for scenarios in which I need both variables synchronized simultaneously. Unfortunately, it's behaving inconsistently.
Sequence of events:
1. A sequence command Dual(test, false) changes the variable Main.test to false
2. A conversation hits a condition Main.test == true and follows the 'false' branch.
3. A sequence command Dual(test, true) changes the variable Main.test to true
4. A conversation hits a condition Main.test == true and still follows the 'false' branch.
During these events, the variable updates correctly in the runtime variable inspector. Manually changing the value to false and back to true causes the conversation to correctly follow the 'true' branch. And using my custom sequencer command to then change it back to false causes the conversation to correctly follow the 'false' branch.
Here is my code:
The code also updates other variables such as Float, Int, and String correctly, but causes conditions to fail during dialogue. The only condition I've found which works correctly is bool false.
Is there something wrong with way I'm calling 'DialogueLua.SetVariable()' ? And if so, then I can't figure out why it is updating correctly in the runtime variable inspector, but not correctly triggering dialogue conditions.
Sequence of events:
1. A sequence command Dual(test, false) changes the variable Main.test to false
2. A conversation hits a condition Main.test == true and follows the 'false' branch.
3. A sequence command Dual(test, true) changes the variable Main.test to true
4. A conversation hits a condition Main.test == true and still follows the 'false' branch.
During these events, the variable updates correctly in the runtime variable inspector. Manually changing the value to false and back to true causes the conversation to correctly follow the 'true' branch. And using my custom sequencer command to then change it back to false causes the conversation to correctly follow the 'false' branch.
Here is my code:
Code: Select all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PixelCrushers.DialogueSystem;
using HutongGames.PlayMaker;
using Sirenix.OdinInspector;
namespace PixelCrushers.DialogueSystem.SequencerCommands {
public class SequencerCommandDual : SequencerCommand {
[InfoBox("This script enables the custom sequencer command DualVariable, which sets the same variable in Dialogue System and on a Playmaker FSM. Syntax: DualVariable([Variable Name], [Value], [FSM Name]. If [FSM Name] is left blank, it defaults to 'Main'.)")]
public void Awake() {
// The GameObject name is always "Database"
string gameObjectName = "Database";
// Get the parameters
string variableName = GetParameter(0); // The name of the variable to set
string value = GetParameter(1); // The value to set the variable to
string fsmName = GetParameter(2); // The name of the FSM component, defaults to "Main" if blank
// Default the FSM name to "Main" if it's blank
fsmName = string.IsNullOrEmpty(fsmName) ? "Main" : fsmName;
// Find the GameObject "Database"
GameObject database = GameObject.Find(gameObjectName);
if (database == null) {
Debug.LogError("SequencerCommandDualVariable: GameObject '" + gameObjectName + "' not found");
Stop();
return;
}
// Get the specified FSM component from the "Database" GameObject
PlayMakerFSM fsm = null;
foreach (var component in database.GetComponents<PlayMakerFSM>()) {
if (component.FsmName.Equals(fsmName)) {
fsm = component;
break;
}
}
if (fsm == null) {
Debug.LogError("SequencerCommandDualVariable: FSM '" + fsmName + "' not found in GameObject '" + gameObjectName + "'");
Stop();
return;
}
// Set the FSM variable
SetFSMVariable(fsm, fsmName, variableName, value);
//Set the variable within Dialogue System
SetDialogueSystemVariable(fsmName, variableName, value);
// Send the 'Start' event to the FSM
fsm.SendEvent("Update / Clothes");
// Done
Stop();
}
private void SetFSMVariable(PlayMakerFSM fsm, string fsmName, string variableName, string value) {
var fsmVariable = fsm.FsmVariables.FindVariable(variableName);
if (fsmVariable == null) {
Debug.LogError("SequencerCommandDualVariable: Variable '" + variableName + "' not found in FSM '" + fsmName + "'");
return;
}
// Set the value based on the type of the variable
if (fsmVariable is FsmBool fsmBoolVar) {
fsmBoolVar.Value = bool.Parse(value);
}
else if (fsmVariable is FsmFloat fsmFloatVar) {
fsmFloatVar.Value = float.Parse(value);
}
else if (fsmVariable is FsmInt fsmIntVar) {
fsmIntVar.Value = int.Parse(value);
}
else if (fsmVariable is FsmString fsmStringVar) {
fsmStringVar.Value = value;
}
else {
Debug.LogError("SequencerCommandDualVariable: Unsupported variable type for '" + variableName + "'");
}
}
private void SetDialogueSystemVariable(string fsmName, string variableName, string value) {
string fullVariableName = fsmName + "." + variableName;
DialogueLua.SetVariable(fullVariableName, value);
}
}
}
Is there something wrong with way I'm calling 'DialogueLua.SetVariable()' ? And if so, then I can't figure out why it is updating correctly in the runtime variable inspector, but not correctly triggering dialogue conditions.