tAPI [Tutorial] - Keybindings and other Mod Options

Wulf'frid

Terrarian
Hello, welcome to this tutorial on how to do keybindings and other modoptions stuffs.

Basic JSON usage, and a little bit of c# (how to set up a cs file, namespace, class, etc)
Alrighty, the first of the mod options, the Boolean.
A Boolean, if you didn't know, is a true of false value or statement, true means on, false means off.

In your ModOptions.json you want to add this
Code:
{
       "options":
             [
                {
                     "name": "Option name",
                     "type": "Boolean",
                     "default": true or false, whatever it's set to by default
                     "notify": true,
                     "notifyOnChange": true
               },
            ]
}
In your modbase or MBase.cs file, you want to add this
Code:
public static bool somename
public override void OptionChanged(Option option)
{
        if (option.name == "The Name in modoptions.json, in my case Option name") { somename = (bool)option.Value;}
}
To then access this, like say if it's true, you want it to do something, is you insert
Code:
if (MBase.somename) //Replace somename with whatever you set the name as in MBase, in my case somename
{
//whatever you want it to do, or be able to do if the bool is true
}
A keybinding is a little more complex, but not too bad.
What it does, is when you press the keybinding, it sets a bool value to true, allowing, or doing something
in your ModOptions.json file add
Code:
{
"name":"Keybinding Option",
"default":"X", //Whatever you want the binding (key) to be
"type":"Keybinding",
"notify":true,
"notifyOnChange":true
},
in your MBase.cs file, add
Code:
public static Keys someothername;
and under the OptionChanged method from earlier in the tutorial, put
Code:
if (option.name == "Keybinding Option") { someothername = (Keys)option.Value;}
then, add this
Code:
public static bool KeyPressed(Keys key)
      {
            if (Main.chatMode || Main.player[Main.myPlayer].talkNPC !=-1)
              {
                   return false;
              }
             return key.Down();

     }
Then, let's just say we have an item, and we want it to shoot a different projectile if the X key is pressed,
under that item's cs file, you want to add this
Code:
if(MBase.KeyPressed(MBase.someothername))
{
// the code for the other projectile
}
I've got a bunch of real life stuff to do, but soon I'll have a list option, and maybe an integer option, if such a thing exists
 
Last edited:
Back
Top Bottom