/// <summary> /// Change any twine variable values as appropriate. This includes /// addition as well as assignment. /// </summary> private void RunVariableAssignments() { if (assignmentVars != null) { if (globalVariables == null) { globalVariables = TwineVariables.GetVariableObject(); } // This character is a marker that says to use addition rather than // assignment. string plusSign = "+"; for (int i = 0; i < assignmentVars.Count; i++) { string varName = assignmentVars[i]; string varValue = assignmentVals[i]; if (!varValue.Contains(plusSign)) { globalVariables.AssignValue(varName, varValue); } else { // Remove the "+" marker and increment value varValue = varValue.Substring(1); globalVariables.IncrementValue(varName, varValue); } } } }
/// <summary> /// Sets validChildren and validChildNames to include only the links that are allowed by the conditionals assigned to them. /// </summary> private void UpdateConditionalLinks() { if (conditionalVars.Count > 0) { if (globalVariables == null) { globalVariables = TwineVariables.GetVariableObject(); } List <GameObject> checkedChildren = new List <GameObject>(); List <string> checkedChildNames = new List <string>(); for (int index = 0; index < linkNames.Length; index++) { if (!conditionalLinks.Contains(linkNames[index])) { checkedChildNames.Add(linkNames[index]); checkedChildren.Add(children[index]); } else { string linkName = linkNames[index]; int condIndex = conditionalLinks.IndexOf(linkName); string varName = conditionalVars[condIndex]; // Operation of the condition - e.g. "=", "!=" string operation = conditionalOp[condIndex]; // Truth value of the conditional bool conditionMet = false; switch (operation) { // For equals and not equals, we compare the values // without caring if they can be converted to ints. case "=": conditionMet = globalVariables.GetValue(varName) == conditionalVals[condIndex]; break; case "!=": conditionMet = !(globalVariables.GetValue(varName) == conditionalVals[condIndex]); break; default: // For greater-than/less-than-style comparisons, we // break and leave conditionMet as false unless both // values can be converted to ints. int val1; if (!Int32.TryParse(globalVariables.GetValue(varName), out val1)) { break; } int val2; if (!Int32.TryParse(conditionalVals[condIndex], out val2)) { break; } switch (operation) { case "<": if (val1 < val2) { conditionMet = true; } break; case "<=": if (val1 <= val2) { conditionMet = true; } break; case ">": if (val1 > val2) { conditionMet = true; } break; case ">=": if (val1 >= val2) { conditionMet = true; } break; default: conditionMet = false; Exception e = new Exception("Twine conditional has unknown operation"); throw e; } break; } if (conditionMet) { checkedChildNames.Add(linkNames[index]); checkedChildren.Add(children[index]); } } } validLinkNames = checkedChildNames.ToArray(); validChildren = checkedChildren.ToArray(); } else { validLinkNames = linkNames; validChildren = children; } }