事件对话框现在会格式化描述文本,将 `{变量名}` 这样的占位符替换为来自 `EventContext` 的当前值。
这允许事件文本能够动态变化,以反映游戏状态或事件中的选择。添加了 `Wula_Test_VariableEvent` 测试事件来演示和验证此功能。
75 lines
2.2 KiB
C#
75 lines
2.2 KiB
C#
using System.Collections.Generic;
|
|
using Verse;
|
|
|
|
namespace WulaFallenEmpire
|
|
{
|
|
public static class EventContext
|
|
{
|
|
private static Dictionary<string, object> variables = new Dictionary<string, object>();
|
|
|
|
public static void SetVariable(string name, object value)
|
|
{
|
|
if (variables.ContainsKey(name))
|
|
{
|
|
variables[name] = value;
|
|
}
|
|
else
|
|
{
|
|
variables.Add(name, value);
|
|
}
|
|
Log.Message($"[EventContext] Set variable '{name}' to '{value}'.");
|
|
}
|
|
|
|
public static T GetVariable<T>(string name, T defaultValue = default)
|
|
{
|
|
if (variables.TryGetValue(name, out object value))
|
|
{
|
|
if (value is T typedValue)
|
|
{
|
|
return typedValue;
|
|
}
|
|
// Try to convert, e.g., from int to float
|
|
try
|
|
{
|
|
return (T)System.Convert.ChangeType(value, typeof(T));
|
|
}
|
|
catch (System.Exception)
|
|
{
|
|
Log.Warning($"[EventContext] Variable '{name}' is of type {value.GetType()} but could not be converted to {typeof(T)}.");
|
|
return defaultValue;
|
|
}
|
|
}
|
|
Log.Warning($"[EventContext] Variable '{name}' not found. Returning default value.");
|
|
return defaultValue;
|
|
}
|
|
|
|
public static bool HasVariable(string name)
|
|
{
|
|
return variables.ContainsKey(name);
|
|
}
|
|
|
|
public static Dictionary<string, object> GetAllVariables()
|
|
{
|
|
return new Dictionary<string, object>(variables);
|
|
}
|
|
|
|
public static void Clear()
|
|
{
|
|
variables.Clear();
|
|
Log.Message("[EventContext] All variables cleared.");
|
|
}
|
|
|
|
public static void ClearVariable(string name)
|
|
{
|
|
if (variables.Remove(name))
|
|
{
|
|
Log.Message($"[EventContext] Cleared variable '{name}'.");
|
|
}
|
|
else
|
|
{
|
|
Log.Warning($"[EventContext] Tried to clear variable '{name}' but it was not found.");
|
|
}
|
|
}
|
|
}
|
|
}
|