Creating A Command
Last updated
Last updated
Commands should be made in your Module in the Start() method. Also, make sure you haveusing UnityEngine
at the top. This tutorial creates a command that will heal or damage the player by a specified amount. For example, typing apply health 1
in the console with heal the player for one full heart. While apply damage 0.5
will damage the player for half a heart.
The first thing you need to do is create a group for your commands
For this tutorial, we will make a command for applying healing and damage to the player. If we want we can also create a message to send when the player just types "apply" in the console.
You can use markup style syntax to show messages in the log with a different color. Here, the text will be red.
Most people want a help function for their mod so you should add that too.
This gets the group you made earlier, "apply" and adds a command to it, "help".
So to have those messages appear in the console you would type apply help
.
Now we will start with the actual commands. Add the following lines to Start
.
what this does is it gets the group that we made earlier and add our new commands to it, healing and damage. Then with the this.ApplyHealing
we assign the method that we are going to use to cause the actual command effect.
Now we create methods for applying and dealing damage.
These also go outside any other methods, at the same level as Start
.
What this does is first, it checks whether or not the command has at least one argument, the number. if not it logs that message to the console. Otherwise, it starts running the actual heal code. First, we need to access the player, so we get the variable PlayerController and assign it to a variable, player
. Then we get the heal amount which is the first argument. Now you may have noticed that the HealAmount is actually a string, not a float. That is because what the player types in the console is a string and we can't assign a string to a float. Now we assign the string to Heal Amount, args[0]. Now you may be wondering why we are assigning args[0], instead of args[1], well that is because arrays in C# start from zero, not from one. If the player typed "wow so cool" in the console, wow = args[0], so = args[1], cool = args[2], etc. so we set heal amount to the number the player types in the console. Now we want to actually apply the healing. we get the players healthHaver since we are editing their health, then we ApplyHealing equal to the number the user put in the console. Now we can't use a string as a float, so that is why we use float.Parse
to turn the string, HealAmount, into a float. Finally, we want to confirm that we did all of that by logging to the player the HealAmount in the console.
Now we move onto the ApplyDamage.
This is the same as ApplyHealing with a few differences, we want to change our string name so we don't get confused ever. we also use ApplyDamage instead, the first part is damage amount, then direction, then the string that shows up on the ammonomicon if it kills you, damage type, damage category, whether or not they should get Invulnerability frames after taking damage from this (which for some reason needs to be set to true if you don't want them to get Invulnerability frames.) hit pixel collider, and whether it should ignore boss dps cap. Finally, we update the message in the console so that it says damage instead of healing.