Skip to content

Commit

Permalink
Fix blackbaord tracker for custom variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
isadorasophia committed May 11, 2024
1 parent 214b84e commit b7bf1ca
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
81 changes: 81 additions & 0 deletions src/Murder/Core/Dialogs/OrphanBlackboardContext.cs
@@ -0,0 +1,81 @@
using System.Text.Json.Serialization;

namespace Murder.Core.Dialogs;

/// <summary>
/// This is used to track variables created on the fly. This used to be an
/// object, but the serialization doesn't really like that, so I'll follow a
/// similar pattern to other facts.
/// </summary>
public readonly struct OrphanBlackboardContext
{
public readonly FactKind Kind = FactKind.Invalid;

public readonly string? StrValue = null;

public readonly int? IntValue = null;

public readonly float? FloatValue = null;

public readonly bool? BoolValue = null;

public OrphanBlackboardContext() { }

[JsonConstructor]
public OrphanBlackboardContext(FactKind kind, bool? boolValue, int? intValue, float? floatValue, string? strValue)
{
(Kind, StrValue, IntValue, FloatValue, BoolValue) = (kind, strValue, intValue, floatValue, boolValue);
}

public OrphanBlackboardContext(object @value)
{
bool? @bool = null;
int? @int = null;
float? @float = null;
string? @string = null;

FactKind kind = FactKind.Invalid;

// Do not propagate previous values.
switch (@value)
{
case bool toBool:
kind = FactKind.Bool;
@bool = toBool;

break;

case int toInt:
kind = FactKind.Int;
@int = toInt;

break;

case float toFloat:
kind = FactKind.Float;
@float = toFloat;

break;

case string toString:
kind = FactKind.String;
@string = toString;

break;
}

(Kind, StrValue, IntValue, FloatValue, BoolValue) = (kind, @string, @int, @float, @bool);
}

public object? GetValue()
{
return Kind switch
{
FactKind.Bool => BoolValue,
FactKind.Int => IntValue,
FactKind.Float => FloatValue,
FactKind.String => StrValue,
_ => null,
};
}
}
8 changes: 4 additions & 4 deletions src/Murder/Data/Save/BlackboardTracker.cs
Expand Up @@ -26,7 +26,7 @@ public class BlackboardTracker
/// in the story.
/// </summary>
[Serialize]
private readonly Dictionary<string, object> _variablesWithoutBlackboard = new(StringComparer.InvariantCultureIgnoreCase);
private readonly Dictionary<string, OrphanBlackboardContext> _variablesWithoutBlackboard = new(StringComparer.InvariantCultureIgnoreCase);

[Serialize]
private readonly Dictionary<Guid, ImmutableDictionary<string, BlackboardInfo>> _characterBlackboards = [];
Expand Down Expand Up @@ -396,12 +396,12 @@ public void SetString(string? name, string fieldName, string value, Guid? charac
/// </summary>
private T GetValue<T>(string name) where T : notnull
{
if (!_variablesWithoutBlackboard.TryGetValue(name, out object? result))
if (!_variablesWithoutBlackboard.TryGetValue(name, out OrphanBlackboardContext orphanContext))
{
return default!;
}

if (result is not T resultAsT)
if (orphanContext.GetValue() is not T resultAsT)
{
GameLogger.Error($"Invalid expected type of {typeof(T).Name} for {name}!");
return default!;
Expand Down Expand Up @@ -481,7 +481,7 @@ public void SetString(string? name, string fieldName, string value, Guid? charac
/// </summary>
private void SetValue<T>(string name, T value) where T : notnull
{
_variablesWithoutBlackboard[name] = value;
_variablesWithoutBlackboard[name] = new(value);

// do not trigger modified since this does not imply in story outside of the dialogue.
}
Expand Down

0 comments on commit b7bf1ca

Please sign in to comment.