(How) Can I make a dictionary for variable references?

I have many functions working basically the same way, just using different variables. A brief example:

[SyncVar] private Vector3 syncedPlayerPosition;
[SyncVar] private Vector3 syncedHandPosition;

private Transform playerTransform;
private Transform handTransform;

private float lerpPlayerPositionRate;
private float lerpHandPositionRate;

void SetPlayerPosition(){

    if(playerTransform.position != syncedPlayerPosition){
        playerTransform.position = Vector3.Lerp(playerTransform.position, syncedPlayerPosition, Time.deltaTime * lerpPlayerPositionRate);
    }
}

void SetHandPosition(){

    if(handTransform.position != syncedHandPosition){
        handTransform.position = Vector3.Lerp(handTransform.position, syncedHandPosition, Time.deltaTime * lerpHandPositionRate);
    }
}

As you can see, both functions do basically the same thing that they just use different variables.

Now I'm looking for something like this (assuming it Dictionarywill work like that)

[SyncVar] private Vector3 syncedPlayerPosition;
[SyncVar] private Vector3 syncedHandPosition;

private Transform playerTransform;
private Transform handTransform;

private float lerpPlayerPositionRate;
private float lerpHandPositionRate;

/* Add Dictionaries to reference those variables */
private Dictionary<Transform,Vector3> positionDict = new Dictionary<Transform,Vector3>();
private Dictionary<Transform,float> lerpRateDict = new Dictionary<Transform,float>();

private void Start(){
    /* Fill the Dictionaries with Links between the Transforms
     * to the achording variable reference */

    /* Until now not the references are linked but rather values written */
    positionDict.Add(playerTransform, syncedPlayerPosition);
    positionDict.Add(handTransform, syncedHandPosition);

    lerpRateDict.Add(playerTransform, lerpPlayerPositionRate);
    lerpRateDict.Add(handTransform, lerpHandPositionRate);
}

/* Than I could use (and change) only one common function for both e.g like */
void SetPosition(Transform transform){

    if(transform.position != positionDict[transform]){
        transform.position = Vector3.Lerp(transform.position, positionDict[transform], Time.deltaTime * lerpRateDict[transform]);
    }

    /* Add this only as example that also this has to be posible */
    lerpRateDict[transform] += 1;
    positionDict[transform] = Vector3.zero(); 
}

I actually tried it this way, but realized that the values ​​in are Dictionarynot updated, but keep everything where they were when called Start(). Therefore, it seems to me that they are not added to Dictionaryon Reference, but on Value.

Is it possible to configure it so that the dictionaries do not actually store values, but rather links to existing variables? How can I archive something like that?

, . , , .


, : hte Vector3 float Transform s.

+4
1

, :

void SyncPositions(Transform thisTransform, Vector3 targetPosion, float rate)
{
    if (thisTransform.position != targetPosion)
    {
        thisTransform.position = Vector3.Lerp(thisTransform.position, targetPosion, Time.deltaTime * rate);
    }
}

.

SyncPosition(playerTransform,syncedPlayerPosition,lerpPlayerPositionRate);
SyncPosition(handTransform,syncedHandPosition,lerpHandPositionRate);

,

+4

Source: https://habr.com/ru/post/1688932/


All Articles