Pointer variable

Hi, I’m just wondering if a better way to shorten my script, to make it more clean and convenient… I have this long code of lines that repeats but manages different variable on each function…

Sorry, I can’t post my script, because it’s long and complicated (dirty), it have:
buffExecution: (apply OnTriggerStay) enum: perSecond, onceAndInstant;
stabilizeExecution: (apply OnTriggerExit) enum: perSecond, instantlyTakeAwayBuff;
buff range restriction;
buff accepts positive or negative (known as debuff?);
pass by other object as parameter

So, instead I’ll make a dummy script…
Here the sample:

//Example only...
public class PlayerTravelController : MonoBehaviour {

private float moveRate = 100f;
private float turnSpeed = 90f;

private float moveRateBuff = 0f;
private float moveRateStabilize = 100f;

private float turnSpeedBuff = 0f;
private float turnSpeedStabilize = 90f;

private float confuseBuildUp = 0f;
private float confuseRecovery = 90f;

void Update
{
    if (moveRateBuff > 0f)
    {
        moveRateBuff -= moveRateStabilize * Time.deltaTime;
        if (moveRateBuff < 0f)
        {
            moveRateBuff = 0f;
        }
    }

    if (turnSpeedBuff > 0f)
    {
        turnSpeedBuff -= turnSpeedStabilize * Time.deltaTime;
        if (turnSpeedBuff < 0f)
        {
            turnSpeedBuff = 0f;
        }
    }

    if (confuseBuildUp > 0f)
    {
        confuseBuildUp -= confuseRecovery * Time.deltaTime;
        if (confuseBuildUp < 0f)
        {
            confuseBuildUp = 0f;
        }
    }
}

void FixedUpdate ()
{
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");
    float turn = Input.GetAxis ("Mouse X");
   
    Vector 3 move;
    move = new Vector3 (moveHorizontal, moveVertical, 0f);
   
    thisObject.AddRelativeForce (move * (moveRate + moveRateBuff));
    thisObject.AddTorque (transform.rotation.z - Mathf.Clamp(turn, -2f, 2f) * (turnSpeed + turnSpeedBuff));

    int randomSpinner;
    randomSpinner = Random.Range (0, 2);
    if (randomSpinner == 0)
        randomSpinner = -1;
    else
        randomSpinner = 1;
   
    thisObject.AddTorque (transform.rotation.z + (confuseBuildUp * randomSpinner));
}

//...

//Use for other object with onTriggerEnter(Stay) in ColliderMechanic.cs
public void setMoveRateBuff (float buffPoints)
{
    if (buffPoints > 0f)
    {
        if (moveRateBuff < buffPoints)
        {
            moveRateBuff = buffPoints;
        }
    }
}

public void setTurnSpeedBuff (float buffPoints)
{
    if (buffPoints > 0f)
    {
        if (turnSpeedBuff < buffPoints)
        {
            turnSpeedBuff = buffPoints;
        }
    }
}

public void setConfuseBuildUp (float buffPoints)
{
    if (buffPoints > 0f)
    {
        if (confuseBuildUp < buffPoints)
        {
            confuseBuildUp = buffPoints;
        }
    }
}

I know I can do like this:

//Use for other object with onTriggerEnter(Stay) in ColliderMechanic.cs
public void setMoveRateBuff (float buffPoints)
{
    setThisBuff (buffPoints, 0);
}

public void setTurnSpeedBuff (float buffPoints)
{
    setThisBuff (buffPoints, 1);
}

public void setConfuseBuildUp (float buffPoints)
{
    setThisBuff (buffPoints, 2);
}

public void setThisBuff (float buffPoints, int buff_id)
{
    if (buffPoints > 0f)
    {
        if (buff_id == 0 && moveRateBuff < buffPoints)
        {
            moveRateBuff = buffPoints;
        }
        else if (buff_id == 1 && turnSpeedBuff < buffPoints)
        {
            turnSpeedBuff = buffPoints;
        }
        else if (buff_id == 2 && confuseBuildUp < buffPoints)
        {
            confuseBuildUp = buffPoints;
        }
    }
}

But is there something I can do this?

//Use for other object with onTriggerEnter(Stay) in ColliderMechanic.cs
public void setMoveRateBuff (float buffPoints)
{
    *buffPointer = moveRateBuff;
    setThisBuff (buffPoints, *buffPointer);
}

public void setTurnSpeedBuff (float buffPoints)
{
    *buffPointer = turnSpeedBuff;
    setThisBuff (buffPoints, *buffPointer);
}

public void setConfuseBuildUp (float buffPoints)
{
    *buffPointer = confuseBuildUp;
    setThisBuff (buffPoints, *buffPointer);
}

public void setThisBuff (float buffPoints, float *thisBuff)
{
    if (buffPoints > 0f)
    {
        if (*thisBuff < buffPoints)
        {
            *thisBuff = buffPoints;
        }
    }
}

}

NOTE: The code above is not my actual code; it is much more complicated and long (not so)…

Please educate me, thank you so much, your help is appreciated…

C#'s ‘ref’ keyword is what you are looking for here. It allows you to pass a variable by reference to a function, which allows you to modify that variable in the function and have that change ‘persist’ once you leave the function’s scope.

There is also the ‘out’ keyword which is similar but probably not what you want here.

1 Like