I’m pretty new at the coding thing but I wanted to know if there was any difference at all from loading into a script a certain amount variables or having the script do the calculations. For instance:
x = 100
y = 20
z = x * 5(y)
or
x = 100
y = x/5
z = x *5(y)
y is already known to be 20 as opposed to the calculation x/5 needing to be performed.
The reason being, I’d like to limit the amount of calculations on variables during a specific time of game play so that it doesn’t consume unnecessary processing power when the calculation can be done at a “slower” processing time. Doing it this way I hope to reduce the amount of calculations during this specific time by about 30. Does this sound like a sound way to go about it?
Math operations on this scale shouldn’t really effect performance. Really its about functionality.
I.E.
If you know that X wont change that often
public int x = 100;
public int y;
public void CalculateY ()
{
y = x/5;
}
public void CalculateZ ()
{
z = x*5(y);
}
This way you can CalculateY only when x changes. IE
public void UpdateX (int newX)
{
x = newX;
CalculateY();
)
At the end of the day though you want code understand-ability:
IE if your trying to communicate say magic damage then as follows:
public int MagicDamage ()
{
int magicDamage = 100; // This is "x"
int magicMultiplier = 20; // This is "y"
int magicBuff = 5; // this is "5" in your z
int MagicDamageAmount = MagicDamage*MagicBuff(MagicMultiplyer);
return MagicDamageAmount;
}
Great this looks good, but if you are trying to say that there is a magicDebuff that is devided by y to get your multiplier then you want to do this:
public int MagicDamage ()
{
int magicDamage = 100; // This is "x"
int magicMultiplier; // This is "y"
int magicDebuff = 5; // This is the "5" in your second example in your y
int magicBuff = 5; // this is "5" in your z
magicMultiplier = magicDamage/magicDebuff;
int MagicDamageAmount = MagicDamage*MagicBuff(MagicMultiplyer);
return MagicDamageAmount;
}
You wouldn’t want to take out that division and just set it to 20 because if you are reading your code at a later date you want it to make sense. You want to know that you are debuffing your magic att and that way you can affect the code latter if you need it to change and it is easily understandable how to do it.
This actually isn’t anything to be concerned about, since it’s micro-optimizing at best. Performing calculations can be faster than fetching values from RAM, but don’t try to optimize first, just concentrate on writing understandable code. It’s likely that you’d look elsewhere for performance issues (shaders, use of level geometry, etc.).