变量诸元

This commit is contained in:
2025-07-31 17:17:07 +08:00
parent 9140f73226
commit 3f13f0c71f
4 changed files with 58 additions and 21 deletions

Binary file not shown.

View File

@@ -440,6 +440,14 @@
<value>AcceptedOffer</value> <value>AcceptedOffer</value>
</li> </li>
``` ```
### `Condition_VariableNotEqual`
检查一个变量是否 **不等于** 一个特定值。
```xml
<li Class="WulaFallenEmpire.Condition_VariableNotEqual">
<name>QuestStage</name>
<value>3</value>
</li>
```
### `Condition_CompareVariable` (基类) ### `Condition_CompareVariable` (基类)
这是一个抽象基类,不应直接使用。以下所有比较条件(大于、小于等)都继承自这个基类,并共享其参数。 这是一个抽象基类,不应直接使用。以下所有比较条件(大于、小于等)都继承自这个基类,并共享其参数。
@@ -496,14 +504,4 @@
<value>2</value> <value>2</value>
</li> </li>
``` ```
### `Condition_VariableNotEqual`
检查一个变量是否 **不等于** 一个特定值。
```xml
<li Class="WulaFallenEmpire.Condition_VariableNotEqual">
<name>QuestStage</name>
<value>3</value>
</li>
```
--- ---

View File

@@ -16,13 +16,13 @@ namespace WulaFallenEmpire
public override bool IsMet(out string reason) public override bool IsMet(out string reason)
{ {
object variable = EventContext.GetVariable<object>(name); if (!EventContext.HasVariable(name))
if (variable == null)
{ {
reason = $"Variable '{name}' not set."; EventContext.SetVariable(name, "0");
return false;
} }
object variable = EventContext.GetVariable<object>(name);
string compareValue = value; string compareValue = value;
if (!string.IsNullOrEmpty(valueVariableName)) if (!string.IsNullOrEmpty(valueVariableName))
{ {
@@ -58,13 +58,13 @@ namespace WulaFallenEmpire
public override bool IsMet(out string reason) public override bool IsMet(out string reason)
{ {
float variable = EventContext.GetVariable<float>(name, float.NaN); if (!EventContext.HasVariable(name))
if (float.IsNaN(variable))
{ {
reason = $"Variable '{name}' not set or not a number."; EventContext.SetVariable(name, 0f);
return false;
} }
float variable = EventContext.GetVariable<float>(name);
float compareValue = value; float compareValue = value;
if (!string.IsNullOrEmpty(valueVariableName)) if (!string.IsNullOrEmpty(valueVariableName))
{ {
@@ -113,9 +113,42 @@ namespace WulaFallenEmpire
protected override string GetOperatorString() => "<="; protected override string GetOperatorString() => "<=";
} }
public class Condition_VariableNotEqual : Condition_CompareVariable public class Condition_VariableNotEqual : Condition
{ {
protected override bool Compare(float var1, float var2) => var1 != var2; public string name;
protected override string GetOperatorString() => "!="; public string value;
public string valueVariableName;
public override bool IsMet(out string reason)
{
if (!EventContext.HasVariable(name))
{
EventContext.SetVariable(name, "0");
}
object variable = EventContext.GetVariable<object>(name);
string compareValue = value;
if (!string.IsNullOrEmpty(valueVariableName))
{
compareValue = EventContext.GetVariable<object>(valueVariableName)?.ToString();
if (compareValue == null)
{
reason = $"Comparison variable '{valueVariableName}' not set.";
return false;
}
}
bool met = variable.ToString() != compareValue;
if (!met)
{
reason = $"Requires {name} != {compareValue} (Current: {variable})";
}
else
{
reason = "";
}
return met;
}
} }
} }

View File

@@ -153,6 +153,12 @@ namespace WulaFallenEmpire
public override void Execute(Dialog_CustomDisplay dialog = null) public override void Execute(Dialog_CustomDisplay dialog = null)
{ {
// Only set the variable if it doesn't already exist.
if (EventContext.HasVariable(name))
{
return;
}
// Try to parse as int, then float, otherwise keep as string // Try to parse as int, then float, otherwise keep as string
if (int.TryParse(value, out int intValue)) if (int.TryParse(value, out int intValue))
{ {