How to make your own mkey
For the T-Engine version 3.0.0 alpha14b, by LukeHaub
Introduction
This is just a basic explanation of how to create your own mkey.
If you don't know what one is then let me explain. An mkey is an item in the menu that appears when you press the m key, hence the name. When you select an item in the menu it looks up the number of the item you selected and runs the function corresponding to that mkey.
Creating Your Mkey
There are three basic ways to create an mkey
first we have:
add_mkey
{
mkey = "name of mkey",
type = "the mkey type",
fct = function()
-- all the code that your mkey does when activated goes here
end
}
name of mkey is just a string naming the mkey e.g. "Froggy Magic" the mkey type is either "skill" or "ability" depending on weather this mkey will show up after you gain a specific skill or ability You'll see this way used often in the code but I personally prefer not to use it because your skill/ability must be the same name as the mkey. Normally this isn't a problem but it just means you have to remember to change the name/type of the mkey if you change what skill it's attached to which is a bit of a hassle if you're always rearranging things like I am.
the second method is:
GLOBAL_CONSTANT = add_mkey
{
fct = function()
-- all the code that your mkey does when activated goes here
end
}
where GLOBAL_CONSTANT is a just what it says, a global constant. Technically it doesn't have to be a constant, you could change it after you call add_mkey but i can't really see any reason why you'd want to do this.
therefore your code would look like this:
declare_global_constants {
"A_GLOBAL_CONSTANT",
}
A_GLOBAL_CONSTANT = add_mkey
{
...
}
the third way is:
add_mkey
{
mkey = a_number,
fct = function()
-- all the code that your mkey does when activated goes here
end
}
a_number can either be just that, a number e.g. 113 or it could be a global constant defined as above but which you initialize to be a specific number e.g.
declare_global_constants {
"A_GLOBAL_CONSTANT",
}
A_GLOBAL_CONSTANT = 113
add_mkey
{
mkey = A_GLOBAL_CONSTANT,
...
}
There isn't really any point to this as it offers no real advantages over the second method and just adds extra code.
Allowing The User To Select The Mkey
Now that you've defined your mkey you have to make it show up so you can select it. If you used the first method above you will have to add the following code to the skill in skills.lua that has the same name as your mkey.
action = { nil, "string that shows up when you press m" }
if you used the latter methods you have to specify which mkey to activate so you would use this:
action = { A_GLOBAL_CONSTANT, "string that shows up when you press m" }
alternatively these lines can be added to an ability instead of a skill
One Last Tidbit
Normally once you aquire the skill or ablility an mkey is attached the mkey becomes active and will show up in the menu. If this is unacceptable then there is another field you can fill out when creating an mkey which is hide. This is a function which should return either true or false. If it returns true then the mkey will not be shown. e.g.
A_CONSTANT = add_mkey
{
hide = function()
if player.chp() < 300 then
return true
else
return false
end
end,
fct = function()
-- code goes here
end
}
this will hide the mkey if the player has less than 300 current hit points
ToME Wiki