I am still quite new to programming in Unity and I am currently experimenting on a spell-based platformer.
My problem is following: I've got a struct containing 6 floats (this number will increase as I am to implement new spell proporties) and I need to add another float to each of the floats inside of this struct. I only got 6 elements and I've done it manually by adding each float, but as I intend to expand this struct I need a better solution then this brute and useless implementaion.
Furthermore, the code below shows the example for one spell, I am working on two at the same time, so there is two times more lines of code for such a simple thing
//Adding proporties to controller
manager.GetComponent<Controller> ().firstSpell.damage += adder.damage;
manager.GetComponent<Controller> ().firstSpell.force += adder.force;
manager.GetComponent<Controller> ().firstSpell.cooldown += adder.cooldown;
manager.GetComponent<Controller> ().firstSpell.damageOverTime += adder.damageOverTime;
manager.GetComponent<Controller> ().firstSpell.size += adder.size;
manager.GetComponent<Controller> ().firstSpell.gravity += adder.gravity;
manager.GetComponent<Controller> ().firstSpell.damage = Mathf.Clamp (manager.GetComponent<Controller> ().firstSpell.damage, Spells.damageMin, Spells.damageMax);
manager.GetComponent<Controller> ().firstSpell.force = Mathf.Clamp (manager.GetComponent<Controller> ().firstSpell.force, Spells.forceMin, Spells.forceMax);
manager.GetComponent<Controller> ().firstSpell.cooldown = Mathf.Clamp (manager.GetComponent<Controller> ().firstSpell.cooldown, Spells.cooldownMin, Spells.cooldownMax);
manager.GetComponent<Controller> ().firstSpell.damageOverTime = Mathf.Clamp (manager.GetComponent<Controller> ().firstSpell.damageOverTime, Spells.damageOverTimeMin, Spells.damageOverTimeMax);
manager.GetComponent<Controller> ().firstSpell.size = Mathf.Clamp (manager.GetComponent<Controller> ().firstSpell.size, Spells.sizeMin, Spells.sizeMax);
manager.GetComponent<Controller> ().firstSpell.gravity = Mathf.Clamp (manager.GetComponent<Controller> ().firstSpell.gravity, Spells.gravityMin, Spells.gravityMax);
As you can see in the above code I have to Clamp those values as well. Is there a simple way to add two struct variables of the same type (and also clamp their values on the way)?
EDIT This is how I changed it, now there is a public class:
public class spell{
public float damage;
public float force;
public float cooldown;
public float damageOverTime;
public float size;
public float gravity;
public void addValues(spell other){
damage += other.damage;
force += other.force;
cooldown += other.cooldown;
damageOverTime += other.damageOverTime;
size += other.size;
gravity += other.gravity;
}
public void clampValues(){
damage = Mathf.Clamp(damage, Boundaries.damageMin, Boundaries.damageMax);
force = Mathf.Clamp(force , Boundaries.forceMin, Boundaries.forceMax);
cooldown = Mathf.Clamp(cooldown, Boundaries.cooldownMin, Boundaries.cooldownMax);
damageOverTime = Mathf.Clamp(damageOverTime, Boundaries.damageOverTimeMin, Boundaries.damageOverTimeMax);
size = Mathf.Clamp(size, Boundaries.sizeMin, Boundaries.sizeMax);
gravity = Mathf.Clamp(gravity, Boundaries.gravityMin, Boundaries.gravityMax);
}
}
And this is what I do with it:
Controller managerController = manager.GetComponent<Controller> ();
managerController.firstSpell.addValues (adder);
managerController.firstSpell.clampValues ();