What coding techinque is needed to achieve this simple varible control situation?

Here’s my situation:

I have variable A and I want its value to be set to variable B ---- but only when I actively am setting variable B.

All other times, I want variable A to remain independently adjustable. Placing them in the update() area of the code will constantly lock them together but again, I want A to remain independently adjustable only until B’s value is changed, too.

And is there a way to acheive this without using coroutines?

thanks for your help!

-g

You could make B a property.

int A;

int b;
public int B
{
    get { return b; }
    set
    {
        b = value;
        A = value;
    }
}

Not the clearest thing in the world. Another option would be to wrap the assigning of B in a method called SetBAndA() or something. The upshot of it is - anywhere you assign B, also assign A; anywhere you assign A, just assign A and not B.

1 Like

Thanks for the tips. How would I be able to make it into a method? To be clear, I’m doing this all in realtime, so would anything need to be written into the update area at all or can this all happen completely removed from it?

thanks!

-g

Depends on how the value of B is being changed.

@KelsoMRK here’s how I envision the implementation:

You have a slider that controls the value of variable A, which also sets the value of B. You have another slider in which you simply adjust the value of B. When you touch the A slider again, the value of B smoothly goes back ot whatever A is being set to.

define “smoothly”… you mean changes slowly over time?

The reason @LeftyRighty is asking this is because in order to do anything “over time,” pretty much you need a coroutine, unless you want to make all the plumbing yourself.

Which will then bring up the notion of “what kind of time- or displacement-dependent function do you want the transition to use?”

The original @KelsoMRK code snippet is a property, which is sort of a method. If you want it to BE a method, just reformat it to be a function that accepts new values for B, and when called sets it into B and into A.

Not necessarily. You can do it in Update.

void Update()
{
    if (!Mathf.Approximately(A, B))
    {
        A = Mathf.MoveTowards(A, B, rate * Time.deltaTime);
    }
}

Then you don’t need to manage the start/stop of coroutines if the user moves the slider while the other one is lerping into position.

There’s more to this though because you’ll have to handle some kind of state so you know the last slider that was touched and also to stop lerping if the user moves the lerp slider while the lerp is happening.

1 Like