Non-Static

Non-Static Commands Usage

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.