8,290
edits
m (remove unnecessary code tag) |
(modding -> script modding) |
||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[Category:Modding]] | {{Modding Info}}{{Modding Topic Prerequisites | Modding:C Sharp Scripting}}[[Category:Script Modding]] | ||
Mods can add custom key mapping entries to the Key Mapping menu. | Mods can add custom key mapping entries to the Key Mapping menu. | ||
Line 8: | Line 8: | ||
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. | 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. | ||
__TOC__ | |||
== Define Key Mapping Options == | == Define Key Mapping Options == | ||
Line 24: | Line 26: | ||
[[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. | |||
<syntaxhighlight lang="csharp"> | |||
using System; | |||
namespace XRL.World.Parts | |||
{ | |||
[Serializable] | |||
public class ModName_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 ModName_PlayerMutator : IPlayerMutator | |||
{ | |||
public void mutate(GameObject player) | |||
{ | |||
// add your command listener to the player when a New Game begins | |||
player.AddPart<ModName_CommandListener>(); | |||
} | |||
} | |||
</syntaxhighlight> | |||
== Shipping Your Mod == | |||
Your completed mod should look like this. For more info about the proper Mods directory location, see [[File_locations#.22Offline.22_mods_.28user_created_or_manually_added.29|File locations > Offline mods]]. | |||
<syntaxhighlight lang="yaml"> | |||
<Caves of Qud App Directory> | |||
Mods | |||
MyModFolder | |||
Commands.xml | |||
ModName_CommandListener.cs | |||
ModName_PlayerMutator.cs | |||
</syntaxhighlight> | |||
{{Modding Navbox}} | {{Modding Navbox}} |