Modding:Wishes: Difference between revisions

From Caves of Qud Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 2: Line 2:
Creating your own wishes requires 2.0.200.24 or greater.
Creating your own wishes requires 2.0.200.24 or greater.


To create a wish, you define a <code>XRL.World.Capabilities.WishHandler</code> attribute on a public class, you pass two parameters, a RegularExpression pattern string, and a Method Name which should take a successful Match if one is foundYour method should return true if you handled the wish.
To create a wish, you define a <code>XRL.Wish.WishCommand</code> attribute on a public methodThis method should either be void or bool.


<syntaxhighlight lang="csharp">
<syntaxhighlight lang="csharp">


[XRL.World.Capabilities.WishHandler(@"^mytestwish$", "TestWishHandler")]
public class MyWishHandler
public static class MyWishHandler
{
{
  // the method should be public static, return a bool, and take a "Match" or it will not get called
  // Handles "testwish:foo" or "testwish foo" as a wish command
  public static bool TestWishHandler(System.Text.RegularExpressions.Match match)
  [XRL.Wish.WishCommand(Command = "testwish")]
  {
  public static bool TestWishHandler(string rest)
     Popup.Show("Matched: " + match.Groups[0].ToString());
  {
     Popup.Show("Matched: " + rest);
     // if we dont return true, other wishes will also parse this wish message
     // if we dont return true, other wishes will also parse this wish message
     return true;
     return true;
  }
  }
 
  // Handles "testwish" with nothing else! (no string param)
  [XRL.Wish.WishCommand(Command = "testwish")]
  public static void TestWishHandler()
  {
    Popup.Show("Matched it the short way");
    // if we return void, it assumes we handled it
  }
 
  // showing of non-static also!
  public int count = 0;
 
  // no command -- uses the method name by default!
  [XRL.Wish.WishCommand]
  public void inc()
  {
    Popup.Show(count++);
  }
 
  [XRL.Wish.WishCommand]
  public void dec()
  {
    Popup.Show(count--);
  }
 
  [XRL.Wish.WishCommand(Regex = @"other fancy match \d things"]
  public void Handle(System.Text.RegularExpression.Match match)
  {
    Popup.Show(match.Groups[0].ToString());
  }
 
}
}
</syntaxhighlight>
</syntaxhighlight>


The regular expression passed to the attribute is parsed using case insensitive matching.
The regular expression passed to the attribute is parsed using case insensitive matching.

Revision as of 06:27, 17 April 2020

Creating your own wishes requires 2.0.200.24 or greater.

To create a wish, you define a XRL.Wish.WishCommand attribute on a public method. This method should either be void or bool.

public class MyWishHandler
{
  // Handles "testwish:foo" or "testwish foo" as a wish command
  [XRL.Wish.WishCommand(Command = "testwish")]
  public static bool TestWishHandler(string rest)
  {
     Popup.Show("Matched: " + rest);
     // if we dont return true, other wishes will also parse this wish message
     return true;
  }

  // Handles "testwish" with nothing else! (no string param)
  [XRL.Wish.WishCommand(Command = "testwish")]
  public static void TestWishHandler()
  {
     Popup.Show("Matched it the short way");
     // if we return void, it assumes we handled it
  }

  // showing of non-static also!
  public int count = 0;

  // no command -- uses the method name by default!
  [XRL.Wish.WishCommand]
  public void inc()
  {
    Popup.Show(count++);
  }

  [XRL.Wish.WishCommand]
  public void dec()
  {
    Popup.Show(count--);
  }

  [XRL.Wish.WishCommand(Regex = @"other fancy match \d things"]
  public void Handle(System.Text.RegularExpression.Match match)
  {
    Popup.Show(match.Groups[0].ToString());
  }

}

The regular expression passed to the attribute is parsed using case insensitive matching.