What is a TriggerHandler?

TriggerHandlers handle the logic associated with each Trigger. They take a delegate that takes one parameter TriggerReader.

Here is a brief introduction to the TriggerReader.

Brief Intro to TriggerReader

TriggerReader is like BinaryReader. TriggerReader is used in retrieving the values associated with a Trigger. This is done through the 3 main Read* methods. The methods reader.PeekString, reader.PeekNumber or reader.PeekVariable return boolean if the next value is the desired type. Using those methods help avoid the Exception that is thrown if you try to read a value that doesn't exist.

In this example "trigger" points to a Trigger (5:999) set %testVariable to 123 and print {This is a String}.

TriggerReader reader = new TriggerReader(trigger);
Monkeyspeak.Variable varValue = reader.ReadVariable(); // This would return a reference to %testVariable
double numValue = reader.ReadNumber(); // This would return 123
string stringValue = reader.ReadString(); // This would return "This is a String"
Trying to read these values out of order would cause an exception to be raised.
This allows you to have some actual structure when processing Triggers.

For example if you add a 0, "Hello World", and %userName* to your trigger. You would read it like this:
triggerReader.ReadNumber(); // Number is 0
triggerReader.ReadString(); // String is "Hello World"
triggerReader.ReadVariable(); // Variable is %userName
If however you decided to try to read the first value 0 as a String
triggerReader.ReadString(); // TriggerReaderException would be thrown here.
This code would throw a TriggerReaderException because the value desired was not the actual value to be expected.

Back to TriggerHandler

Every Trigger in Monkeyspeak must have a TriggerHandler attached to it. Even the Causes (0:0) have to have one. A TriggerHandler also tells the owning Page whether or not to break execution.

To set a TriggerHandler for a Trigger you would use the Page's setTriggerHandler method
// (5:1) print {...} to the console.
page.SetTriggerHandler(TriggerCategory.Effect, 1, delegate(TriggerReader reader) {
    Console.WriteLine(reader.ReadString());
});
For example a Conditional that fails would tell the TriggerHandler to return false. False means the Page will stop executing the Triggers in that block.

This is a block.
(0:0) when something happens,
  (1:0) and something happens,
(0:1) when this also happens,
  (1:1) and ..it happens,
    (5:0) tell everyone ..it happens!!
    (5:1) let it rain.

If a TriggerHandler assigned to (1:0) returns False then
(0:1) when this also happens, and everything to (5:1) let it rain. will not be executed.

Next - Creating a Library

Last edited Jun 20, 2012 at 10:09 PM by Squizzle, version 15

Comments

Gerolkae May 7, 2012 at 5:18 AM 
How can 2 triggers happen at the same time? If 1:0 returns False Shouldn't we be able to execute 0:1 >.>

Squizzle May 7, 2012 at 5:05 AM 
It could be that way too.

Gerolkae May 6, 2012 at 1:02 AM 
(0:0) when something happens,
(1:0) and something happens,
(0:1) when this also happens,
(1:1) and ..it happens,
(5:0) tell everyone ..it happens!!
(5:1) let it rain.

Shouldn't that be more like

(0:0) when something happens,
(1:0) and something happens,
(0:1) Or this Happens,
(1:1) and ..it happens,
(5:0) tell everyone ..it happens!!
(5:1) let it rain.

Gerolkae May 6, 2012 at 1:00 AM 
Are you Doing away with the Add Format?
IE:
' (5:6) whisper {...} to {...}.
Add(New Monkeyspeak.Trigger(Monkeyspeak.TriggerCategory.Effect, 6),
Function(reader As Monkeyspeak.TriggerReader)
Dim tname As Variable = reader.ReadVariable
Dim msg As String = reader.ReadString
sndWhisper(tname.Value.ToString, msg)
Return True
End Function,
"(5:6) whisper {...} to {...}.")