Modding:Key Mapping (Commands): Difference between revisions

From Caves of Qud Wiki
Jump to navigation Jump to search
m (remove unnecessary code tag)
(Add the rest of the code infrastructure to make a keybind work.)
Line 24: Line 24:


[[Image:Modding - Custom Key Mapping Menu.png|class=scalable]]
[[Image:Modding - Custom Key Mapping Menu.png|class=scalable]]
== Creating a Part to Listen for the Command ==
Create a part to listen for your Key Mapping command on the player.
<sytnaxhighlight lang="c#">
using System;
namespace XRL.World.Parts
{
    [Serializable]
    public class MyMod_CommandListener : IPart
    {
        public static readonly string CmdOne = "ModName_Cmd_One";
        public static readonly string CmdTwo = "ModName_Cmd_Two";
        public override void Register(GameObject Object)
        {
            Object.RegisterPartEvent(this, CmdOne);
            Object.RegisterPartEvent(this, CmdTwo);
            base.Register(Object);
        }
        public override bool FireEvent(Event E)
        {
            if (E.ID == CmdOne)
            {
                //Do something when the keybind for ModName_Cmd_One is pressed!
            }
            if (E.ID == CmdTwo)
            {
                //Do something when the keybind for ModName_Cmd_Two is pressed!
            }
            return base.FireEvent(E);
        }
    }
}
</syntaxhighlight>
== Adding the Part to the Player ==
Add your part to the player when a New Game is started. This section uses the method described in the [[Modding:Adding_Code_to_the_Player]] article. Refer to that article for more information, including how you can also add your part to the player when an existing save game is loaded.
<syntaxhighlight lang="csharp">
  using XRL;
  using XRL.World;
 
  [PlayerMutator]
  public class MyPlayerMutator : IPlayerMutator
  {
      public void mutate(GameObject player)
      {
          // add your command listener to the player when a New Game begins
          player.AddPart<MyMod_CommandListener>();
      }
  }
</syntaxhighlight>


{{Modding Navbox}}
{{Modding Navbox}}

Revision as of 15:26, 2 October 2020

This page is about modding. See the modding overview for an abstract on modding.
This page is about modding. See the modding overview for an abstract on modding.

Mods can add custom key mapping entries to the Key Mapping menu.

Key Mapping.png

Players can then bind a key to the mod's custom command.

When the player presses that key, the mod's custom command is received as an event on the player character. This means that at least a small amount of C# scripting is required to take advantage of the key mapping infrastructure.

Define Key Mapping Options

A mod defines its custom key mapping options in a Commands.xml file. Here's an example file that defines two commands.

<?xml version="1.0" encoding="utf-8"?>
<commands>
  <command ID="ModName_Cmd_One" DisplayText="My Custom Debug Command" Category="Debug"></command>
  <command ID="ModName_Cmd_Two" DisplayText="My Custom Cool Command" Category="Cool"></command>
</commands>

In this case, we've added one command to the existing "Debug" category in the Key Mapping menu. We've added another command to a new category - the "Cool" category. Here's how it looks in game:

Modding - Custom Key Mapping Menu.png

Creating a Part to Listen for the Command

Create a part to listen for your Key Mapping command on the player.

<sytnaxhighlight lang="c#"> using System;

namespace XRL.World.Parts {

   [Serializable]
   public class MyMod_CommandListener : IPart
   {
       public static readonly string CmdOne = "ModName_Cmd_One";
       public static readonly string CmdTwo = "ModName_Cmd_Two";
       public override void Register(GameObject Object)
       {
           Object.RegisterPartEvent(this, CmdOne);
           Object.RegisterPartEvent(this, CmdTwo);
           base.Register(Object);
       }
       public override bool FireEvent(Event E)
       {
           if (E.ID == CmdOne)
           {
               //Do something when the keybind for ModName_Cmd_One is pressed!
           }
           if (E.ID == CmdTwo)
           {
               //Do something when the keybind for ModName_Cmd_Two is pressed!
           }
           return base.FireEvent(E);
       }
   }

} </syntaxhighlight>

Adding the Part to the Player

Add your part to the player when a New Game is started. This section uses the method described in the Modding:Adding_Code_to_the_Player article. Refer to that article for more information, including how you can also add your part to the player when an existing save game is loaded.

  using XRL;
  using XRL.World;
  
  [PlayerMutator]
  public class MyPlayerMutator : IPlayerMutator
  {
      public void mutate(GameObject player)
      {
          // add your command listener to the player when a New Game begins
          player.AddPart<MyMod_CommandListener>();
      }
  }