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. Dismiss Notice

Question Is it possible to change a variable in a function based on the fuctiuon input?

Discussion in 'Scripting' started by KnoblePersona, Jan 30, 2021.

  1. KnoblePersona

    KnoblePersona

    Joined:
    Oct 13, 2020
    Posts:
    15
    I'm trying to to add a buffer to my jump input on my game and I wan to use function because to code will be the same ever time, the only change will be how long the buffer will last (bufferLength) and the buffer variable (buffer) but I don't know if it's possible to change buffer in the script.

    Example: I wan to input a float 'bufferedJump' and it will test if it's in the buffer passes the requirements, but I also want to change bufferedJump to -1. But it dose not work.

    So this is what I need to know: Can this be done with a function and is there a better way to achieve this?

    Code (CSharp):
    1. public void FixedUpdate()
    2. {
    3.     if (CheckBufferedInput(inputJumpBuffered, inputBufferLength) /*& grounded*/)
    4.     {
    5.         //Player Jumps
    6.     }
    7.     if (inputJumpBuffered <= inputBufferLength && inputJumpBuffered != -1 )
    8.     {
    9.         inputJumpBuffered += Time.deltaTime;
    10.     }
    11. }
    12.  
    13. bool CheckBufferedInput(float buffer, float bufferLength)
    14. {
    15.     if (buffer <= bufferLength &&  buffer != -1)
    16.     {
    17.         return true;
    18.     }
    19.     else
    20.     {
    21.         return false;
    22.     }
    23.  
    24.     buffer = -1; // <--- This no good
    25. }
    26.  
    27. public void OnJump(InputAction.CallbackContext context)
    28. {
    29.     if (context.started)
    30.     {
    31.         inputJumpBuffered = 0;
    32.     }
    33. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    You can do this but you need to use the
    ref
    keyword.

    The reason is that
    float
    variables are value types, so the local
    buffer
    argument is a fresh copy and when you change it, it does not change
    inputJumpBuffered


    Look up using the ref keyword. You would use it in the function declaration AND you must use it at the call site to sort of confirm that's what you mean.

    Code (csharp):
    1. void MyFunc( ref int x)
    2. {
    3.  x++;
    4. }
    and to call it

    Code (csharp):
    1. MyFunc( ref myIntegerVariable);
    Line 3 above will actually increment
    myIntegerVariable
     
    Last edited: Jan 31, 2021
    Bunny83 and Vryken like this.
  3. KnoblePersona

    KnoblePersona

    Joined:
    Oct 13, 2020
    Posts:
    15
    Okay, this makes a lot of sense. Can I still set values in the function based on the ref variable as if it wasn't a reference?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    If you make another variable, sure.

    As long as that variable is declared as
    ref
    in the function header, it MUST be passed in as
    ref
    , and when you change it, you are really changing the original variable. That can't be changed dynamically without extra code deciding to copy it or whatever.
     
  5. KnoblePersona

    KnoblePersona

    Joined:
    Oct 13, 2020
    Posts:
    15
    I tried adding ref to float buffer as shown in the code above, but I get an error "Unreachable code detected" now
     
  6. KnoblePersona

    KnoblePersona

    Joined:
    Oct 13, 2020
    Posts:
    15
    I just figured out why. Never mind. It was after return. Thank you so much, this will help out a lot!
     
    Bunny83 and Kurt-Dekker like this.