feat: 扩展事件系统,改进变量处理和调试功能
本次提交对事件系统进行了多项增强和修复,主要包括: - 为`Effect_SetVariable`添加了类型支持(Int, Float, String, Bool),允许更精确地设置变量类型 [在`EventDef_Wula.xml`, `EventDef_WULA_FE_Spiritualist.xml`, `Effect.cs`中]。 - 改进了`Condition`类,增加了类型检查和错误处理,避免了类型不匹配导致的错误 [在`Condition.cs`中]。 - 修复了`Effect_ModifyVariable`中的错误,允许使用变量名作为修改值,并支持int和float类型的操作 [在`Effect.cs`中]。 - 添加了`Effect_StoreFactionGoodwill`,用于存储派系好感度到变量中 [在`Effect.cs`中]。 - 增加了`Dialog_ManageEventVariables`,用于调试和管理事件变量 [在`DebugActions.cs`, `Dialog_ManageEventVariables.cs`中]。 - 改进了`EventVariableManager`,增加了类型转换的错误处理和日志记录,并添加了获取所有变量的函数 [在`EventVariableManager.cs`中]。 这些改动提高了事件系统的稳定性和可扩展性,并为调试提供了更多工具。
This commit is contained in:
Binary file not shown.
@@ -20,10 +20,12 @@
|
|||||||
<li Class="WulaFallenEmpire.Effect_SetVariable">
|
<li Class="WulaFallenEmpire.Effect_SetVariable">
|
||||||
<name>Wula_UI_Check_Odyssey_Is_Active</name>
|
<name>Wula_UI_Check_Odyssey_Is_Active</name>
|
||||||
<value>0</value>
|
<value>0</value>
|
||||||
|
<type>Int</type>
|
||||||
</li>
|
</li>
|
||||||
<li Class="WulaFallenEmpire.Effect_SetVariable">
|
<li Class="WulaFallenEmpire.Effect_SetVariable">
|
||||||
<name>Wula_UI_Check_Anomaly_Is_Active</name>
|
<name>Wula_UI_Check_Anomaly_Is_Active</name>
|
||||||
<value>0</value>
|
<value>0</value>
|
||||||
|
<type>Int</type>
|
||||||
</li>
|
</li>
|
||||||
<li Class="WulaFallenEmpire.Effect_ModifyVariable" MayRequire="Ludeon.RimWorld.Odyssey">
|
<li Class="WulaFallenEmpire.Effect_ModifyVariable" MayRequire="Ludeon.RimWorld.Odyssey">
|
||||||
<name>Wula_UI_Check_Odyssey_Is_Active</name>
|
<name>Wula_UI_Check_Odyssey_Is_Active</name>
|
||||||
@@ -40,7 +42,7 @@
|
|||||||
<li Class="WulaFallenEmpire.Effect_ClearVariable">
|
<li Class="WulaFallenEmpire.Effect_ClearVariable">
|
||||||
<name>Wula_FE_Spiritualist_Goodwill</name>
|
<name>Wula_FE_Spiritualist_Goodwill</name>
|
||||||
</li>
|
</li>
|
||||||
<li Class="WulaFallenEmpire.Effect_CheckFactionGoodwill">
|
<li Class="WulaFallenEmpire.Effect_StoreFactionGoodwill">
|
||||||
<factionDef>Wula_FE_Spiritualist_Faction</factionDef>
|
<factionDef>Wula_FE_Spiritualist_Faction</factionDef>
|
||||||
<variableName>Wula_FE_Spiritualist_Goodwill</variableName>
|
<variableName>Wula_FE_Spiritualist_Goodwill</variableName>
|
||||||
</li>
|
</li>
|
||||||
@@ -546,10 +548,12 @@
|
|||||||
<li Class="WulaFallenEmpire.Effect_SetVariable">
|
<li Class="WulaFallenEmpire.Effect_SetVariable">
|
||||||
<name>Wula_Has_Receive_Silver_From_Anisia</name>
|
<name>Wula_Has_Receive_Silver_From_Anisia</name>
|
||||||
<value>0</value>
|
<value>0</value>
|
||||||
|
<type>Int</type>
|
||||||
</li>
|
</li>
|
||||||
<li Class="WulaFallenEmpire.Effect_SetVariable">
|
<li Class="WulaFallenEmpire.Effect_SetVariable">
|
||||||
<name>Wula_Has_Receive_MechWula_From_Anisia</name>
|
<name>Wula_Has_Receive_MechWula_From_Anisia</name>
|
||||||
<value>0</value>
|
<value>0</value>
|
||||||
|
<type>Int</type>
|
||||||
</li>
|
</li>
|
||||||
</effects>
|
</effects>
|
||||||
</li>
|
</li>
|
||||||
|
|||||||
@@ -27,6 +27,7 @@
|
|||||||
<li Class="WulaFallenEmpire.Effect_SetVariable">
|
<li Class="WulaFallenEmpire.Effect_SetVariable">
|
||||||
<name>WULA_FE_Spiritualist_Influence</name>
|
<name>WULA_FE_Spiritualist_Influence</name>
|
||||||
<value>0</value>
|
<value>0</value>
|
||||||
|
<type>Int</type>
|
||||||
</li>
|
</li>
|
||||||
</effects>
|
</effects>
|
||||||
</li>
|
</li>
|
||||||
@@ -105,6 +106,7 @@
|
|||||||
<li Class="WulaFallenEmpire.Effect_SetVariable">
|
<li Class="WulaFallenEmpire.Effect_SetVariable">
|
||||||
<name>WULA_FE_Spiritualist_Influence</name>
|
<name>WULA_FE_Spiritualist_Influence</name>
|
||||||
<value>0</value>
|
<value>0</value>
|
||||||
|
<type>Int</type>
|
||||||
</li>
|
</li>
|
||||||
</effects>
|
</effects>
|
||||||
</li>
|
</li>
|
||||||
@@ -170,6 +172,7 @@
|
|||||||
<li Class="WulaFallenEmpire.Effect_SetVariable">
|
<li Class="WulaFallenEmpire.Effect_SetVariable">
|
||||||
<name>WULA_FE_Spiritualist_Influence</name>
|
<name>WULA_FE_Spiritualist_Influence</name>
|
||||||
<value>0</value>
|
<value>0</value>
|
||||||
|
<type>Int</type>
|
||||||
</li>
|
</li>
|
||||||
</effects>
|
</effects>
|
||||||
</li>
|
</li>
|
||||||
@@ -248,6 +251,7 @@
|
|||||||
<li Class="WulaFallenEmpire.Effect_SetVariable">
|
<li Class="WulaFallenEmpire.Effect_SetVariable">
|
||||||
<name>WULA_FE_Spiritualist_Influence</name>
|
<name>WULA_FE_Spiritualist_Influence</name>
|
||||||
<value>0</value>
|
<value>0</value>
|
||||||
|
<type>Int</type>
|
||||||
</li>
|
</li>
|
||||||
</effects>
|
</effects>
|
||||||
</li>
|
</li>
|
||||||
|
|||||||
@@ -19,26 +19,53 @@ namespace WulaFallenEmpire
|
|||||||
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
|
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
|
||||||
if (!eventVarManager.HasVariable(name))
|
if (!eventVarManager.HasVariable(name))
|
||||||
{
|
{
|
||||||
eventVarManager.SetVariable(name, "0");
|
reason = $"Variable '{name}' not found.";
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
object variable = eventVarManager.GetVariable<object>(name);
|
object variable = eventVarManager.GetVariable<object>(name);
|
||||||
|
string compareValueStr = value;
|
||||||
|
|
||||||
string compareValue = value;
|
|
||||||
if (!string.IsNullOrEmpty(valueVariableName))
|
if (!string.IsNullOrEmpty(valueVariableName))
|
||||||
{
|
{
|
||||||
compareValue = eventVarManager.GetVariable<object>(valueVariableName)?.ToString();
|
compareValueStr = eventVarManager.GetVariable<object>(valueVariableName)?.ToString();
|
||||||
if (compareValue == null)
|
if (compareValueStr == null)
|
||||||
{
|
{
|
||||||
reason = $"Comparison variable '{valueVariableName}' not set.";
|
reason = $"Comparison variable '{valueVariableName}' not set.";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool met = variable.ToString() == compareValue;
|
bool met = false;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (variable is int)
|
||||||
|
{
|
||||||
|
met = (int)variable == int.Parse(compareValueStr);
|
||||||
|
}
|
||||||
|
else if (variable is float)
|
||||||
|
{
|
||||||
|
met = (float)variable == float.Parse(compareValueStr);
|
||||||
|
}
|
||||||
|
else if (variable is bool)
|
||||||
|
{
|
||||||
|
met = (bool)variable == bool.Parse(compareValueStr);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
met = variable?.ToString() == compareValueStr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (System.Exception e)
|
||||||
|
{
|
||||||
|
Log.Warning($"[EventSystem] Condition_VariableEquals: Could not compare '{variable}' and '{compareValueStr}'. Error: {e.Message}");
|
||||||
|
reason = "Type mismatch or parsing error during comparison.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (!met)
|
if (!met)
|
||||||
{
|
{
|
||||||
reason = $"Requires {name} = {compareValue} (Current: {variable})";
|
reason = $"Requires {name} = {compareValueStr} (Current: {variable})";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -62,6 +89,7 @@ namespace WulaFallenEmpire
|
|||||||
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
|
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
|
||||||
if (!eventVarManager.HasVariable(name))
|
if (!eventVarManager.HasVariable(name))
|
||||||
{
|
{
|
||||||
|
Log.Message($"[EventSystem] {GetType().Name}: Variable '{name}' not found, defaulting to 0f.");
|
||||||
eventVarManager.SetVariable(name, 0f);
|
eventVarManager.SetVariable(name, 0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,11 +102,13 @@ namespace WulaFallenEmpire
|
|||||||
if (float.IsNaN(compareValue))
|
if (float.IsNaN(compareValue))
|
||||||
{
|
{
|
||||||
reason = $"Comparison variable '{valueVariableName}' not set or not a number.";
|
reason = $"Comparison variable '{valueVariableName}' not set or not a number.";
|
||||||
|
Log.Warning($"[EventSystem] {GetType().Name} check for '{name}' failed: {reason}");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool met = Compare(variable, compareValue);
|
bool met = Compare(variable, compareValue);
|
||||||
|
Log.Message($"[EventSystem] {GetType().Name} check: Name='{name}', CurrentValue='{variable}', CompareValue='{compareValue}', Met={met}");
|
||||||
if (!met)
|
if (!met)
|
||||||
{
|
{
|
||||||
reason = $"Requires {name} {GetOperatorString()} {compareValue} (Current: {variable})";
|
reason = $"Requires {name} {GetOperatorString()} {compareValue} (Current: {variable})";
|
||||||
@@ -126,26 +156,54 @@ namespace WulaFallenEmpire
|
|||||||
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
|
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
|
||||||
if (!eventVarManager.HasVariable(name))
|
if (!eventVarManager.HasVariable(name))
|
||||||
{
|
{
|
||||||
eventVarManager.SetVariable(name, "0");
|
reason = $"Variable '{name}' not found.";
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
object variable = eventVarManager.GetVariable<object>(name);
|
object variable = eventVarManager.GetVariable<object>(name);
|
||||||
|
string compareValueStr = value;
|
||||||
|
|
||||||
string compareValue = value;
|
|
||||||
if (!string.IsNullOrEmpty(valueVariableName))
|
if (!string.IsNullOrEmpty(valueVariableName))
|
||||||
{
|
{
|
||||||
compareValue = eventVarManager.GetVariable<object>(valueVariableName)?.ToString();
|
compareValueStr = eventVarManager.GetVariable<object>(valueVariableName)?.ToString();
|
||||||
if (compareValue == null)
|
if (compareValueStr == null)
|
||||||
{
|
{
|
||||||
reason = $"Comparison variable '{valueVariableName}' not set.";
|
reason = $"Comparison variable '{valueVariableName}' not set.";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool met = variable.ToString() != compareValue;
|
bool met = false;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (variable is int)
|
||||||
|
{
|
||||||
|
met = (int)variable != int.Parse(compareValueStr);
|
||||||
|
}
|
||||||
|
else if (variable is float)
|
||||||
|
{
|
||||||
|
met = (float)variable != float.Parse(compareValueStr);
|
||||||
|
}
|
||||||
|
else if (variable is bool)
|
||||||
|
{
|
||||||
|
met = (bool)variable != bool.Parse(compareValueStr);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
met = variable?.ToString() != compareValueStr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (System.Exception e)
|
||||||
|
{
|
||||||
|
Log.Warning($"[EventSystem] Condition_VariableNotEqual: Could not compare '{variable}' and '{compareValueStr}'. Error: {e.Message}");
|
||||||
|
reason = "Type mismatch or parsing error during comparison.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.Message($"[EventSystem] Condition_VariableNotEqual check: Name='{name}', Type='{variable?.GetType().Name ?? "null"}', CurrentValue='{variable}', CompareValue='{compareValueStr}', Met={met}");
|
||||||
if (!met)
|
if (!met)
|
||||||
{
|
{
|
||||||
reason = $"Requires {name} != {compareValue} (Current: {variable})";
|
reason = $"Requires {name} != {compareValueStr} (Current: {variable})";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using LudeonTK;
|
||||||
using Verse;
|
using Verse;
|
||||||
using RimWorld;
|
using RimWorld;
|
||||||
using LudeonTK;
|
|
||||||
|
|
||||||
namespace WulaFallenEmpire
|
namespace WulaFallenEmpire
|
||||||
{
|
{
|
||||||
@@ -13,7 +13,6 @@ namespace WulaFallenEmpire
|
|||||||
List<DebugMenuOption> list = new List<DebugMenuOption>();
|
List<DebugMenuOption> list = new List<DebugMenuOption>();
|
||||||
foreach (EventDef localDef in DefDatabase<EventDef>.AllDefs)
|
foreach (EventDef localDef in DefDatabase<EventDef>.AllDefs)
|
||||||
{
|
{
|
||||||
// Capture the local variable for the lambda
|
|
||||||
EventDef currentDef = localDef;
|
EventDef currentDef = localDef;
|
||||||
list.Add(new DebugMenuOption(currentDef.defName, DebugMenuOptionMode.Action, delegate
|
list.Add(new DebugMenuOption(currentDef.defName, DebugMenuOptionMode.Action, delegate
|
||||||
{
|
{
|
||||||
@@ -23,4 +22,13 @@ namespace WulaFallenEmpire
|
|||||||
Find.WindowStack.Add(new Dialog_DebugOptionListLister(list));
|
Find.WindowStack.Add(new Dialog_DebugOptionListLister(list));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class WulaDebugActionsVariables
|
||||||
|
{
|
||||||
|
[DebugAction("Wula Fallen Empire", "Manage Event Variables", actionType = DebugActionType.Action, allowedGameStates = AllowedGameStates.PlayingOnMap)]
|
||||||
|
private static void ManageEventVariables()
|
||||||
|
{
|
||||||
|
Find.WindowStack.Add(new Dialog_ManageEventVariables());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using UnityEngine;
|
||||||
|
using Verse;
|
||||||
|
|
||||||
|
namespace WulaFallenEmpire
|
||||||
|
{
|
||||||
|
public class Dialog_ManageEventVariables : Window
|
||||||
|
{
|
||||||
|
private Vector2 scrollPosition;
|
||||||
|
private Dictionary<string, string> editBuffers = new Dictionary<string, string>();
|
||||||
|
private EventVariableManager manager;
|
||||||
|
|
||||||
|
public override Vector2 InitialSize => new Vector2(800f, 600f);
|
||||||
|
|
||||||
|
public Dialog_ManageEventVariables()
|
||||||
|
{
|
||||||
|
forcePause = true;
|
||||||
|
doCloseX = true;
|
||||||
|
doCloseButton = true;
|
||||||
|
closeOnClickedOutside = true;
|
||||||
|
absorbInputAroundWindow = true;
|
||||||
|
manager = Find.World.GetComponent<EventVariableManager>();
|
||||||
|
RefreshBuffers();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RefreshBuffers()
|
||||||
|
{
|
||||||
|
editBuffers.Clear();
|
||||||
|
foreach (var kvp in manager.GetAllVariables())
|
||||||
|
{
|
||||||
|
editBuffers[kvp.Key] = kvp.Value?.ToString() ?? "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void DoWindowContents(Rect inRect)
|
||||||
|
{
|
||||||
|
Listing_Standard listing = new Listing_Standard();
|
||||||
|
listing.Begin(inRect);
|
||||||
|
|
||||||
|
if (listing.ButtonText("Refresh"))
|
||||||
|
{
|
||||||
|
RefreshBuffers();
|
||||||
|
}
|
||||||
|
if (listing.ButtonText("Clear All Variables"))
|
||||||
|
{
|
||||||
|
manager.ClearAll();
|
||||||
|
RefreshBuffers();
|
||||||
|
}
|
||||||
|
|
||||||
|
listing.GapLine();
|
||||||
|
|
||||||
|
Rect viewRect = new Rect(0f, 0f, inRect.width - 16f, manager.GetAllVariables().Count * 32f);
|
||||||
|
Widgets.BeginScrollView(listing.GetRect(inRect.height - 100f), ref scrollPosition, viewRect);
|
||||||
|
|
||||||
|
Listing_Standard varListing = new Listing_Standard();
|
||||||
|
varListing.Begin(viewRect);
|
||||||
|
|
||||||
|
var allVars = manager.GetAllVariables().OrderBy(kvp => kvp.Key).ToList();
|
||||||
|
|
||||||
|
foreach (var kvp in allVars)
|
||||||
|
{
|
||||||
|
Rect rowRect = varListing.GetRect(30f);
|
||||||
|
string key = kvp.Key;
|
||||||
|
object value = kvp.Value;
|
||||||
|
string typeName = value?.GetType().Name ?? "null";
|
||||||
|
|
||||||
|
Widgets.Label(rowRect.LeftPart(0.4f).Rounded(), $"{key} ({typeName})");
|
||||||
|
|
||||||
|
string buffer = editBuffers[key];
|
||||||
|
string newValue = Widgets.TextField(rowRect.RightPart(0.6f).LeftPart(0.8f).Rounded(), buffer);
|
||||||
|
editBuffers[key] = newValue;
|
||||||
|
|
||||||
|
if (Widgets.ButtonText(rowRect.RightPart(0.1f).Rounded(), "Set"))
|
||||||
|
{
|
||||||
|
// Attempt to parse and set the variable
|
||||||
|
if (value is int)
|
||||||
|
{
|
||||||
|
if (int.TryParse(newValue, out int intVal)) manager.SetVariable(key, intVal);
|
||||||
|
}
|
||||||
|
else if (value is float)
|
||||||
|
{
|
||||||
|
if (float.TryParse(newValue, out float floatVal)) manager.SetVariable(key, floatVal);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
manager.SetVariable(key, newValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
varListing.End();
|
||||||
|
Widgets.EndScrollView();
|
||||||
|
listing.End();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -163,25 +163,34 @@ namespace WulaFallenEmpire
|
|||||||
{
|
{
|
||||||
public string name;
|
public string name;
|
||||||
public string value;
|
public string value;
|
||||||
|
public string type; // Int, Float, String, Bool
|
||||||
|
public bool forceSet = false;
|
||||||
|
|
||||||
public override void Execute(Dialog_CustomDisplay dialog = null)
|
public override void Execute(Dialog_CustomDisplay dialog = null)
|
||||||
{
|
{
|
||||||
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
|
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
|
||||||
if (!eventVarManager.HasVariable(name))
|
if (!forceSet && eventVarManager.HasVariable(name))
|
||||||
{
|
{
|
||||||
if (int.TryParse(value, out int intValue))
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
object realValue = value;
|
||||||
|
if (!string.IsNullOrEmpty(type))
|
||||||
|
{
|
||||||
|
if (type.Equals("int", System.StringComparison.OrdinalIgnoreCase) && int.TryParse(value, out int intVal))
|
||||||
{
|
{
|
||||||
eventVarManager.SetVariable(name, intValue);
|
realValue = intVal;
|
||||||
}
|
}
|
||||||
else if (float.TryParse(value, out float floatValue))
|
else if (type.Equals("float", System.StringComparison.OrdinalIgnoreCase) && float.TryParse(value, out float floatVal))
|
||||||
{
|
{
|
||||||
eventVarManager.SetVariable(name, floatValue);
|
realValue = floatVal;
|
||||||
}
|
}
|
||||||
else
|
else if (type.Equals("bool", System.StringComparison.OrdinalIgnoreCase) && bool.TryParse(value, out bool boolVal))
|
||||||
{
|
{
|
||||||
eventVarManager.SetVariable(name, value);
|
realValue = boolVal;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
eventVarManager.SetVariable(name, realValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -337,14 +346,14 @@ namespace WulaFallenEmpire
|
|||||||
Add,
|
Add,
|
||||||
Subtract,
|
Subtract,
|
||||||
Multiply,
|
Multiply,
|
||||||
Divide,
|
Divide
|
||||||
Set
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Effect_ModifyVariable : Effect
|
public class Effect_ModifyVariable : Effect
|
||||||
{
|
{
|
||||||
public string name;
|
public string name;
|
||||||
public float value;
|
public string value;
|
||||||
|
public string valueVariableName;
|
||||||
public VariableOperation operation;
|
public VariableOperation operation;
|
||||||
|
|
||||||
public override void Execute(Dialog_CustomDisplay dialog = null)
|
public override void Execute(Dialog_CustomDisplay dialog = null)
|
||||||
@@ -357,40 +366,67 @@ namespace WulaFallenEmpire
|
|||||||
|
|
||||||
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
|
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
|
||||||
|
|
||||||
if (!eventVarManager.HasVariable(name))
|
// Determine the value to modify by
|
||||||
|
string valueStr = value;
|
||||||
|
if (!string.IsNullOrEmpty(valueVariableName))
|
||||||
{
|
{
|
||||||
eventVarManager.SetVariable(name, 0f);
|
valueStr = eventVarManager.GetVariable<object>(valueVariableName)?.ToString();
|
||||||
|
if (valueStr == null)
|
||||||
|
{
|
||||||
|
Log.Error($"[WulaFallenEmpire] Effect_ModifyVariable: valueVariableName '{valueVariableName}' not found.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
float currentValue = eventVarManager.GetVariable<float>(name);
|
// Get the target variable, or initialize it
|
||||||
|
object variable = eventVarManager.GetVariable<object>(name);
|
||||||
switch (operation)
|
if (variable == null)
|
||||||
{
|
{
|
||||||
case VariableOperation.Add:
|
Log.Message($"[EventSystem] Effect_ModifyVariable: Variable '{name}' not found, initializing to 0.");
|
||||||
currentValue += value;
|
variable = 0;
|
||||||
break;
|
}
|
||||||
case VariableOperation.Subtract:
|
|
||||||
currentValue -= value;
|
|
||||||
break;
|
|
||||||
case VariableOperation.Multiply:
|
|
||||||
currentValue *= value;
|
|
||||||
break;
|
|
||||||
case VariableOperation.Divide:
|
|
||||||
if (value != 0)
|
|
||||||
{
|
|
||||||
currentValue /= value;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Log.Error($"[WulaFallenEmpire] Effect_ModifyVariable tried to divide by zero for variable '{name}'.");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case VariableOperation.Set:
|
|
||||||
currentValue = value;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
eventVarManager.SetVariable(name, currentValue);
|
object originalValue = variable;
|
||||||
|
object newValue = null;
|
||||||
|
|
||||||
|
// Perform operation based on type
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (variable is int || (variable is float && !valueStr.Contains("."))) // Allow int ops
|
||||||
|
{
|
||||||
|
int currentVal = System.Convert.ToInt32(variable);
|
||||||
|
int modVal = int.Parse(valueStr);
|
||||||
|
newValue = (int)Modify((float)currentVal, (float)modVal, operation);
|
||||||
|
}
|
||||||
|
else // Default to float operation
|
||||||
|
{
|
||||||
|
float currentVal = System.Convert.ToSingle(variable);
|
||||||
|
float modVal = float.Parse(valueStr);
|
||||||
|
newValue = Modify(currentVal, modVal, operation);
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.Message($"[EventSystem] Modifying variable '{name}'. Operation: {operation}. Value: {valueStr}. From: {originalValue} To: {newValue}");
|
||||||
|
eventVarManager.SetVariable(name, newValue);
|
||||||
|
}
|
||||||
|
catch (System.Exception e)
|
||||||
|
{
|
||||||
|
Log.Error($"[WulaFallenEmpire] Effect_ModifyVariable: Could not parse or operate on value '{valueStr}' for variable '{name}'. Error: {e.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private float Modify(float current, float modifier, VariableOperation op)
|
||||||
|
{
|
||||||
|
switch (op)
|
||||||
|
{
|
||||||
|
case VariableOperation.Add: return current + modifier;
|
||||||
|
case VariableOperation.Subtract: return current - modifier;
|
||||||
|
case VariableOperation.Multiply: return current * modifier;
|
||||||
|
case VariableOperation.Divide:
|
||||||
|
if (modifier != 0) return current / modifier;
|
||||||
|
Log.Error($"[WulaFallenEmpire] Effect_ModifyVariable tried to divide by zero.");
|
||||||
|
return current;
|
||||||
|
default: return current;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -556,4 +592,33 @@ namespace WulaFallenEmpire
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public class Effect_StoreFactionGoodwill : Effect
|
||||||
|
{
|
||||||
|
public FactionDef factionDef;
|
||||||
|
public string variableName;
|
||||||
|
|
||||||
|
public override void Execute(Dialog_CustomDisplay dialog = null)
|
||||||
|
{
|
||||||
|
if (factionDef == null || string.IsNullOrEmpty(variableName))
|
||||||
|
{
|
||||||
|
Log.Error("[WulaFallenEmpire] Effect_StoreFactionGoodwill is not configured correctly.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
|
||||||
|
Faction faction = Find.FactionManager.FirstFactionOfDef(factionDef);
|
||||||
|
|
||||||
|
if (faction != null)
|
||||||
|
{
|
||||||
|
int goodwill = faction.GoodwillWith(Faction.OfPlayer);
|
||||||
|
Log.Message($"[EventSystem] Storing goodwill for faction '{faction.Name}' ({goodwill}) into variable '{variableName}'.");
|
||||||
|
eventVarManager.SetVariable(variableName, goodwill);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Log.Warning($"[EventSystem] Effect_StoreFactionGoodwill: Faction '{factionDef.defName}' not found. Storing 0 in variable '{variableName}'.");
|
||||||
|
eventVarManager.SetVariable(variableName, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,6 +48,9 @@ namespace WulaFallenEmpire
|
|||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(name)) return;
|
if (string.IsNullOrEmpty(name)) return;
|
||||||
|
|
||||||
|
// Log the variable change
|
||||||
|
Log.Message($"[EventSystem] Setting variable '{name}' to value '{value}' of type {value?.GetType().Name ?? "null"}.");
|
||||||
|
|
||||||
// Clear any existing variable with the same name to prevent type confusion
|
// Clear any existing variable with the same name to prevent type confusion
|
||||||
ClearVariable(name);
|
ClearVariable(name);
|
||||||
|
|
||||||
@@ -83,48 +86,45 @@ namespace WulaFallenEmpire
|
|||||||
if (string.IsNullOrEmpty(name)) return defaultValue;
|
if (string.IsNullOrEmpty(name)) return defaultValue;
|
||||||
|
|
||||||
object value = null;
|
object value = null;
|
||||||
bool found = false;
|
if (pawnListVars.TryGetValue(name, out var pawnListVal))
|
||||||
|
|
||||||
if (typeof(T) == typeof(List<Pawn>) && pawnListVars.TryGetValue(name, out var pawnListVal))
|
|
||||||
{
|
{
|
||||||
value = pawnListVal;
|
value = pawnListVal;
|
||||||
found = true;
|
|
||||||
}
|
}
|
||||||
else if (typeof(T) == typeof(Pawn) && pawnVars.TryGetValue(name, out var pawnVal))
|
else if (pawnVars.TryGetValue(name, out var pawnVal))
|
||||||
{
|
{
|
||||||
value = pawnVal;
|
value = pawnVal;
|
||||||
found = true;
|
|
||||||
}
|
}
|
||||||
else if (typeof(T) == typeof(float) && floatVars.TryGetValue(name, out var floatVal))
|
else if (floatVars.TryGetValue(name, out var floatVal))
|
||||||
{
|
{
|
||||||
value = floatVal;
|
value = floatVal;
|
||||||
found = true;
|
|
||||||
}
|
}
|
||||||
else if (typeof(T) == typeof(int) && intVars.TryGetValue(name, out var intVal))
|
else if (intVars.TryGetValue(name, out var intVal))
|
||||||
{
|
{
|
||||||
value = intVal;
|
value = intVal;
|
||||||
found = true;
|
|
||||||
}
|
}
|
||||||
else if (stringVars.TryGetValue(name, out var stringVal))
|
else if (stringVars.TryGetValue(name, out var stringVal))
|
||||||
{
|
{
|
||||||
value = stringVal;
|
value = stringVal;
|
||||||
found = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (found)
|
if (value != null)
|
||||||
{
|
{
|
||||||
if (value is T typedValue)
|
if (value is T typedValue)
|
||||||
{
|
{
|
||||||
return typedValue;
|
return typedValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
// Handle cases where T is object but the stored value is, e.g., an int
|
||||||
|
if (typeof(T) == typeof(object))
|
||||||
|
{
|
||||||
|
return (T)value;
|
||||||
|
}
|
||||||
return (T)System.Convert.ChangeType(value, typeof(T));
|
return (T)System.Convert.ChangeType(value, typeof(T));
|
||||||
}
|
}
|
||||||
catch (System.Exception)
|
catch (System.Exception e)
|
||||||
{
|
{
|
||||||
Log.Warning($"[WulaFallenEmpire] EventVariableManager: Variable '{name}' of type {value.GetType()} could not be converted to {typeof(T)}.");
|
Log.Warning($"[WulaFallenEmpire] EventVariableManager: Variable '{name}' of type {value.GetType()} could not be converted to {typeof(T)}. Error: {e.Message}");
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -143,6 +143,10 @@ namespace WulaFallenEmpire
|
|||||||
|
|
||||||
public void ClearVariable(string name)
|
public void ClearVariable(string name)
|
||||||
{
|
{
|
||||||
|
if (HasVariable(name))
|
||||||
|
{
|
||||||
|
Log.Message($"[EventSystem] Clearing variable '{name}'.");
|
||||||
|
}
|
||||||
intVars.Remove(name);
|
intVars.Remove(name);
|
||||||
floatVars.Remove(name);
|
floatVars.Remove(name);
|
||||||
stringVars.Remove(name);
|
stringVars.Remove(name);
|
||||||
@@ -158,5 +162,16 @@ namespace WulaFallenEmpire
|
|||||||
pawnVars.Clear();
|
pawnVars.Clear();
|
||||||
pawnListVars.Clear();
|
pawnListVars.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Dictionary<string, object> GetAllVariables()
|
||||||
|
{
|
||||||
|
var allVars = new Dictionary<string, object>();
|
||||||
|
foreach (var kvp in intVars) allVars[kvp.Key] = kvp.Value;
|
||||||
|
foreach (var kvp in floatVars) allVars[kvp.Key] = kvp.Value;
|
||||||
|
foreach (var kvp in stringVars) allVars[kvp.Key] = kvp.Value;
|
||||||
|
foreach (var kvp in pawnVars) allVars[kvp.Key] = kvp.Value;
|
||||||
|
foreach (var kvp in pawnListVars) allVars[kvp.Key] = kvp.Value;
|
||||||
|
return allVars;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -83,6 +83,7 @@
|
|||||||
<Compile Include="EventSystem\CompOpenCustomUI.cs" />
|
<Compile Include="EventSystem\CompOpenCustomUI.cs" />
|
||||||
<Compile Include="EventSystem\Condition.cs" />
|
<Compile Include="EventSystem\Condition.cs" />
|
||||||
<Compile Include="EventSystem\DebugActions.cs" />
|
<Compile Include="EventSystem\DebugActions.cs" />
|
||||||
|
<Compile Include="EventSystem\Dialog_ManageEventVariables.cs" />
|
||||||
<Compile Include="EventSystem\DelayedActionManager.cs" />
|
<Compile Include="EventSystem\DelayedActionManager.cs" />
|
||||||
<Compile Include="EventSystem\Dialog_CustomDisplay.cs" />
|
<Compile Include="EventSystem\Dialog_CustomDisplay.cs" />
|
||||||
<Compile Include="EventSystem\Effect.cs" />
|
<Compile Include="EventSystem\Effect.cs" />
|
||||||
|
|||||||
Reference in New Issue
Block a user