Modding:Key Mapping (Commands): Difference between revisions

Add the rest of the code infrastructure to make a keybind work.
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}}