Debugging with Jahro Commands

Elevate Your Unity Experience

Introduction

Welcome to the world of Jahro commands, a key tool in enhancing your Unity game development process. Commands in Jahro are akin to 'cheat codes' - they're shortcuts to testing and changing game states without trudging through lines of code.

These commands not only make testing simpler but also provide a dynamic control panel during development. This enables you to adjust variables in real-time, fine-tune game elements, or even insert secret cheats for your players.

In this guide, we'll take a focused look at how to use Jahro commands, turning your console into a power tool for your Unity projects. Let's get started!

Understanding Commands

Commands in Jahro are snippets of your game logic you've specifically marked for runtime execution. They serve as a bridge between your game code and the Jahro console, enabling you to invoke these pieces of logic just by calling their name.

Naming Your Commands

Name - command name you'd like to see in Jahro.
Please note, use spaces in command names could cause some misexecution in Text Mode.

Description - it's always useful to set few words about the command and what is does

Group Name - group name for this command. You could organize your commands in groups for easy access. Commands with empty group name, will be places in the group Default. See more about grouping here.

Types of Methods

Jahro commands can be both static and non-static methods. A static method is executed without needing an instance of the class, while a non-static method, tied to an instance, can manipulate the object state. Furthermore, these methods could be of private, internal, public, or default access modifier type.

Return Types

Commands in Jahro work with methods of return types void and string. While void methods don't give output, string methods return a value that gets logged in the Text Console. This could be useful for outputting relevant information, making your testing process even more insightful.

Parameters for Your Commands

In addition to performing actions, Jahro commands can also receive and use input parameters. You can specify up to 3 parameters for each command, offering a wide range of possibilities for customizing your game logic at runtime.

Parameter Types

Parameters in Jahro commands support various types, including primitives (int, float, bool, etc.), strings, Unity's Vector2, Vector3, Vector4, and Quaternion, and more. This allows your commands to work with a diverse set of data, providing flexible control over your game.

Method Overloading

Jahro supports method overloading, meaning you can have multiple commands with the same name but different parameter lists. When you execute a command, Jahro will automatically map the inputs to the appropriate overload, giving you even more control and adaptability in your commands.

Remember, to use parameters in commands, simply separate them by spaces in Text Mode. For instance, commandname param1 param2 param3 executes 'commandname' with three parameters. In Visual Mode, you'll have input fields to fill in the parameters.

Using Jahro Attributes

In Jahro, attributes serve as markers that designate your methods as executable commands within the Jahro console.

How to Use Jahro Attributes

To utilize Jahro attributes, you prepend the method with the [JahroCommand("command-name")] attribute. This works for both static and non-static methods.

Here's a simple example:

[JahroCommand("RestartGame")]
public static void RestartGame()
{
    // Your game restarting logic here
}
In this instance, RestartGame becomes a command that you can call from within the Jahro console.

Naming, Describing, and Grouping Commands

Unlike method names in code, command names in Jahro are set explicitly using the [JahroCommand("command-name")] attribute. This provides flexibility in defining more descriptive or simplified command names that can differ from the method names in code.

Additionally, Jahro attributes allow for custom descriptions, and group names. These optional features can be added to the command using the format [JahroCommand("name", "description", "groupname")].

For example:

[JahroCommand("revive-player", "Revives the player with full health", "player-commands")]
public static void RevivePlayer()
{
    // Your player reviving logic here
}
In this case, "revive-player" becomes the command name, the description provides additional context when viewing the command, and "player-commands" categorizes this command into a specific group for easier navigation. If no group name is specified, the command defaults to the "Default" group.

Working with Static Commands

Static commands in Jahro refer to commands that are created from static methods. Static methods belong to the class itself rather than an instance of the class. This means they can be called directly from the Jahro console without needing to register a specific object instance.

Creating Static Commands

Creating static commands is as easy as marking a static method with the JahroCommand attribute. Here's an example:

[JahroCommand("PauseGame", "Pauses the game", "game-commands")]
public static void PauseGame()
{
    // Your game pausing logic here
}
In this case, "PauseGame" becomes a command that you can call directly from the Jahro console, even without any specific game object.

Static commands can also have parameters. Here's an example of a static command that takes two parameters, a float and a Vector3:

[JahroCommand("player-move", "Move player in direction by distance", "player-commands")]
public static void MovePlayer(Vector3 direction, float distance)
{
    // Your player moving logic here
}
In this case, MovePlayer becomes a command that moves a player to a new position when you call it from the Jahro console, using the direction and distance as arguments.

Executing Static Commands

To execute static commands, simply type the command name into the Jahro console. For instance, typing PauseGame in the console will pause the game.

For the MovePlayer command, you would type something like: player-move -1 0 0 7.5, where "7.5" is the distance and "(-1,0,0)" is the direction.
Note: Jahro uses reflection to identify all methods marked with JahroCommandAttribute at the start of your project. By default, all assemblies are searched, but you can specify additional assemblies in Jahro settings. This gives you the freedom to decide which parts of your code Jahro has access to.

Working with Non-Static Commands

Non-static commands are associated with a specific instance of a class, allowing you to execute the command on different instances of a class individually. This gives you more granular control compared to static commands that apply at the class level.

To work with non-static commands, you need to first define the command and then register the specific object instance with Jahro. Here are three different ways to register non-static commands:

Registering a Non-Static Command By Method Name

This method requires marking the method with the JahroCommand attribute and using the Jahro.RegisterCommand() method, specifying the command name, object instance and the method name as arguments.

For example, let's say we have a method in our class that returns the name of the current scene in Unity:

public class SceneController
{
    public string GetCurrentScene()
    {
        return "Current scene is " + SceneManager.GetActiveScene().name;
    }
}
We can register this method as a command with Jahro as follows:

var sceneController = new SceneController();
Jahro.RegisterCommand("current-scene", sceneController, "GetCurrentScene");
After registering, you can type current-scene in the Jahro console to get the name of the current scene.
Registering a Command Callback with a Lambda

This method allows you to create commands that execute a specified lambda expression. For example, let's say we have a class that controls the speed of a game object:

var myObject = new MyObject();

Jahro.RegisterCommand<float>("set-speed", "Sets the speed of the game object",
(speed) =>
{
    myObject.SetSpeed(speed);
});
Now, typing set-speed 5 in the Jahro console will set the speed of our game object to 5.

Registering a Command Callback with a Delegate

This method involves creating a delegate with the same signature as the method you want to register and using this delegate when registering the command. Let's say we have a class that controls player's landing speed in a game:

public class PlayerController
{
    public void SetPlayerSpeed(Vector2 speed)
    {
        Debug.Log("Set speed " + speed);
    }
}
We can register a command that sets the landing speed of our player as follows:

var playerController = new PlayerController();
Jahro.RegisterCommand<Vector2>("land-player-speed", playerController, SetLandPlayerSpeed);
In summary, non-static commands give you fine-grained control over individual instances of classes in your game. By marking methods with the JahroCommand attribute and registering the object instances with Jahro, you can use Jahro's console to directly manipulate your game's variables and behavior.

Unregister Commands

Sometimes, you may want to unregister a command from Jahro, for instance, when it's no longer needed or when the related object instance has been destroyed. Jahro provides two methods to unregister commands:

Unregister Command by Name

The UnregisterCommand(string name) method allows you to unregister a command by its name:

Jahro.UnregisterCommand("my-command");
After calling this method, the command my-command will no longer be available in the Jahro console.

Unregister Command by Name and Group Name

The UnregisterCommand(string name, string groupName) method allows you to unregister a command by its name and group:

Jahro.UnregisterCommand("my-command", "MyGroup");
After calling this method, the command my-command in the group MyGroup will no longer be available in the Jahro console.
Related articles
Getting Started with Jahro: Your First Steps to Efficient Unity Debugging
Embark on your journey with Jahro. Follow our simple step-by-step guide to install the plugin, create commands, and launch your project. Enhance your Unity 3D debugging experience with Jahro's powerful tools. Start now for a smoother, more efficient workflow.
Master Real-Time Debugging in Unity with Jahro Watcher Mode
Learn how Jahro's Watcher Mode can overhaul your Unity debugging process. Start tracking your game's variables in real-time
Making Debugging Easy: The Comprehensive Guide to Jahro Logs in Unity
Learn to use Jahro Logs in Unity for a smoother debugging experience. Check out our detailed guide now!