Extending the text processor

The Pixie
This code gives a couple of extra options to the text processor.

Use "either" instead of "if" when you also have text for when the condition is false. Here is an example. If game.weather is over 5 the player is told it is sunny, otherwise she is told it is cloudy.

You look up at the sky. It is {either game.weather > 5:sunny:cloudy}.

Use "eval" to display the result of a Quest expression. This could be the return value of a function, a calculation, etc.

You look up at the sky. {eval:WeatherDescription()}
Your health is {eval:GetHealth()}.
Your progress is {eval:100 * GetScore() / 312}%.
Hi, {eval:CapFirst(\"mary\")}

The expression must return a value, you will get an error otherwise. If the expression has double quotes, you will need to escape them, as shown in the last example.

The code itself consists of three functions, the first replacing the original (with six new lines as indicated), and two new functions. You can paste the whole lot into a library file or the game file.
  <function name="ProcessTextCommand" parameters="section, data" type="string">
if (StartsWith(section, "if ")) {
return (ProcessTextCommand_If(section, data))
}
// Next six lines added by The Pixie
else if (StartsWith(section, "either ")) {
return (ProcessTextCommand_Either(section, data))
}
else if (StartsWith(section, "eval:")) {
return (ProcessTextCommand_Eval(section, data))
}
else if (StartsWith(section, "object:")) {
return (ProcessTextCommand_Object(section, data))
}
else if (StartsWith(section, "command:")) {
return (ProcessTextCommand_Command(Mid(section, 9), data))
}
else if (StartsWith(section, "page:")) {
return (ProcessTextCommand_Command(Mid(section, 6), data))
}
else if (StartsWith(section, "exit:")) {
return (ProcessTextCommand_Exit(section, data))
}
else if (StartsWith(section, "once:")) {
return (ProcessTextCommand_Once(section, data))
}
else if (StartsWith(section, "random:")) {
return (ProcessTextCommand_Random(section, data))
}
else if (StartsWith(section, "rndalt:")) {
return (ProcessTextCommand_RandomAlias(section, data))
}
else if (StartsWith(section, "img:")) {
return (ProcessTextCommand_Img(section, data))
}
else if (StartsWith(section, "counter:")) {
return (ProcessTextCommand_Counter(Mid(section, 9), data))
}
else if (StartsWith(section, "select:")) {
return (ProcessTextCommand_Select(section, data))
}
else {
dot = Instr(section, ".")
if (dot = 0) {
return ("{" + ProcessTextSection(section, data) + "}")
}
else {
objectname = Left(section, dot - 1)
attributename = Mid(section, dot + 1)
object = GetObject(objectname)
if (object = null) {
return ("{" + ProcessTextSection(section, data) + "}")
}
else {
if (HasAttribute(object, attributename)) {
type = TypeOf(object, attributename)
switch (type) {
case ("string", "int", "double") {
return (ToString(GetAttribute(object, attributename)))
}
case ("boolean") {
result = GetAttribute(object, attributename)
if (result) {
return ("true")
}
else {
return ("false")
}
}
default {
return ("(" + type + ")")
}
}
}
else {
return ("")
}
}
}
}
</function>

<function name="ProcessTextCommand_Either" parameters="section, data" type="string"><![CDATA[
command = Mid(section, 8)
colon = Instr(command, ":")
colon2 = Instr(colon + 1, command, ":")
if (colon = 0 or colon2 = 0) {
return ("{either " + command + "}")
}
else {
text = Mid(command, colon + 1, colon2 - colon - 1)
text2 = Mid(command, colon2 + 1)
condition = Left(command, colon - 1)
result = Eval(condition)
if (result) {
return (ProcessTextSection(text, data))
}
else {
return (ProcessTextSection(text2, data))
}
}
]]></function>


<function name="ProcessTextCommand_Eval" parameters="section, data" type="string"><![CDATA[
exp = Mid(section, 6)
return("" + Eval(exp))
]]></function>

jaynabonne
Very nice!

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums