- Home /
Question by
Igor_Vasiak · Nov 29, 2017 at 11:30 PM ·
scripting problemparametersmethodsif-else
How to pass a parameter only if certain condition is true?
I have a script that's getting a bit messy:
float value = MGS.Math.PercentOf(current_health, 0, max_health, min_color, max_color);
if (red && !blue && !green)
MGS.Color.ChangeColor(SpriteR, (int)value, 255, 255, 255);
else if (!red && blue && !green)
MGS.Color.ChangeColor(SpriteR, 255, (int)value, 225, 255);
else if (!red && !blue && green)
MGS.Color.ChangeColor(SpriteR, 255, 225, (int)value, 255);
else if (red && blue && !green)
MGS.Color.ChangeColor(SpriteR, (int)value, (int)value, 225, 255);
if (red && !blue && green)
MGS.Color.ChangeColor(SpriteR, (int)value, 255, (int)value, 255);
if (!red && blue && green)
MGS.Color.ChangeColor(SpriteR, 255, (int)value, (int)value, 255);
if (red && blue && green)
MGS.Color.ChangeColor(SpriteR, (int)value, (int)value, (int)value, 255);
else
return;
To avoid this mess, I wanted to do something like this:
float value = MGS.Math.PercentOf(current_health, 0, max_health, min_color, max_color);
MGS.Color.ChangeColor(SpriteR, if (red) { (int)value } else { 255 }, if (blue) { (int)value } else { 255 }, if (green) { (int)value } else { 255 }, 255);
Is something like this even possible? Thanks in advance.
Comment
Best Answer
Answer by MacDx · Nov 29, 2017 at 11:38 PM
Determine the values beforehand using the ? operator, like this:
float value = MGS.Math.PercentOf(current_health, 0, max_health, min_color, max_color);
int r = red ? (int)value : 255;
int g = green ? (int)value : 255;
int b = blue ? (int)value : 255;
MGS.Color.ChangeColor(SpriteR, r,g,b,255);
Your answer
Follow this Question
Related Questions
public variable in a script on a button 0 Answers
Make method run when a variable like frame count increments 2 Answers
rotation around axis by degree smoothly for repeating random rotations 0 Answers
Sequence of methods stored in the list with parameters 1 Answer
Set up method parameter in Editor ? 2 Answers