Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

CallbackContext as parameter

Discussion in 'Scripting' started by Karrzun, Jun 17, 2021.

  1. Karrzun

    Karrzun

    Joined:
    Oct 26, 2017
    Posts:
    123
    Hi everyone,

    I'm writing a in-game developer conole at the moment to give myself the possibility to debug games when they're built. If you press a button, the console opens up and closes. Inside the console you can enter commands (like "/quit" to quit the application, or "/gravity <value>" to change the strength of the gravitational pull to the given value). Just for fun, I also wanted to add a command called "/close" that - who would have guessed - closes the console. As mentioned before, I could do that with the keyboard, but I want to practice different approaches. :p


    My current console controller is based on the new InputSystem and listens to a CallbackContext to open and close:

    Code (CSharp):
    1. public class ConsoleBehaviour : MonoBehaviour
    2. {
    3.  
    4.     // fields and assignments here ...
    5.  
    6.     public void Toggle(CallbackContext context)
    7.     {
    8.         if (!context.action.triggered)
    9.             return;
    10.  
    11.         if (uiCanvas.activeSelf)
    12.         {
    13.             CloseConsole();
    14.         }
    15.         else
    16.         {
    17.             OpenConsole();
    18.         }
    19.     }
    20.  
    21. }
    I also have an interface for commands. Input into the console is checked against that interface and when recognized as a proper command, the interface method "Process(string[] args)" is called on the command. This method is supposed to handle all the logic of that specific command.
    My goal is to call the ConsoleBehaviour::Toggle method from that command and give a corresponding CallbackContext as parameter but I'm not sure how I can accomplish that or whether it's possible at all.

    I tried things along the lines of

    // get InputAction from InputActionAsset --> toggle
    ConsoleBehaviour.instance.Toggle(toggle.triggered);
    ConsoleBehaviour.instance.Toggle(toggle.started);


    Until now, without success. Maybe someone can point me into the right direction.


    Thank you in advance!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    Add a layer of indirection. There's no need to try to hook into the input system for this.

    Code (CSharp):
    1. public class ConsoleBehaviour : MonoBehaviour
    2. {
    3.     // fields and assignments here ...
    4.  
    5.     public void OnToggle(CallbackContext context) {
    6.  
    7.         if (!context.action.triggered)
    8.             return;
    9.      
    10.         ToggleConsole();
    11.     }
    12.  
    13.     public void ToggleConsole()
    14.     {
    15.         if (uiCanvas.activeSelf)
    16.         {
    17.             CloseConsole();
    18.         }
    19.         else
    20.         {
    21.             OpenConsole();
    22.         }
    23.     }
    24. }
    Code (CSharp):
    1. ConsoleBehaviour.instance.ToggleConsole();
     
  3. Karrzun

    Karrzun

    Joined:
    Oct 26, 2017
    Posts:
    123
    Yeah, I guess I was so focused on doing something with the New InputSystem, I lost sight of the bigger picture. Thank you for your answer.