I have a lot of functions working basically the same way just using different variables. Short 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 exactly the same thing they are just using different Variables.
Now I'm looking for something like this (assuming Dictionarys would 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 the values in the Dictionarys don't get updated but stay whatever they where when Start() was called. So it seems to me they are not added to the Dictionarys by Reference but by Value.
Is it possible to set it up in a way so that in Dictionaries actually are not stored the values but rather the references to the existing variables? How can I archieve something like that?
I have really a lot of those functions and every small change has to be applied to them one-by-one. A setup like I'm looking for would allow me to make those changes only once for all.
EDIT
I forgot to mention that: Within those functions sometimes also hte Vector3 and float values might get changed not only the Transforms.