LogoLogo
Back to raftmodding.com
  • Welcome to the RaftModding docs!
  • ⬇️Getting started
    • Installing Raft Mod Loader
      • Troubleshooting
        • An error occured while fetching for updates
          • Disabling IPV6
          • Changing the DNS
        • The menu inside the game doesn't show up
        • The game crashes on startup
        • There are error notifications in-game
      • Linux or Steam Deck installation
        • Using Bottles
        • Using Wine
      • Configuring your antivirus
        • Windows Defender
        • Malwarebytes
        • Bitdefender
        • Avast
        • Norton
        • AVG
    • Installing a mod
      • Mods in multiplayer
    • How to run multiple raft instances
  • ⚙️MODDING TUTORIALS
    • How to create a mod project
      • The modinfo.json file
    • How to create an AssetBundle
    • How to create console commands
    • Harmony basics
    • Getting access to the modding repositories
  • ⚒️Modding API
    • RAPI
    • Mod
  • 📚Modding Examples
    • Accessing the player instance
    • Adding private variables
    • Spawning dropped items
    • Get selected hotbar item
    • Get the current SteamID
    • Get the current username
    • Giving items to a player
    • Modifying private variables
  • 🌏WEBSITE
    • Mod Slugs (Unique Identifier)
  • 🖥️RAFT DEDICATED SERVER
    • Raft Dedicated Server
Powered by GitBook
On this page

Was this helpful?

  1. MODDING TUTORIALS

How to create console commands

This tutorial is intended to guide you through creating custom console commands using the new console attributes.

Console commands are method attributes, that means that you just have to add [ConsoleCommand(name: "...", docs:"...")] or [ConsoleCommand("...","...")] above your method. The name argument is the command name itself. The docs argument is the command description.

Your method does not need to be public but it needs to be static!

If you want to get the arguments of your command when its ran you can simply add a string[] parameter named args as shown below.

[ConsoleCommand(name: "useful", docs: "this command is useful.")]
public static void MyCommand(string[] args)
{
    Debug.Log("You entered " + args.Length + " arguments!");
}

The method can also have a string return type and will display the returned value as shown below.

[ConsoleCommand(name: "useful", docs: "this command is useful.")]
public static string MyCommand(string[] args)
{
    return "You entered " + args.Length + " arguments!";
}

As said above, commands needs to be static, so in order to access your non static variables you can use the mod instance as shown below.

public string myNonStaticVariable = "some stuff";
public MyModType instance;

void Start(){
    instance = this;
}

[ConsoleCommand(name: "useful", docs: "this command is useful.")]
public static string MyCommand(string[] args)
{
    return "My Variable : " + instance.myNonStaticVariable;
}

The commands are automatically registered and automatically unregistered when the mod is unloaded.

PreviousHow to create an AssetBundleNextHarmony basics

Last updated 1 year ago

Was this helpful?

⚙️