Sliders and Numbers [SOLVED]

Started by Atelier, Sun 04/07/2010 18:10:01

Previous topic - Next topic

Atelier

Hi,

I have four sliders on a GUI (I'll call them A, B, C, D respectively).
I also have an int (MaxWorkforce, which can be anything depending on what the player's done beforehand).

What I need is to set the max values of ABCD according to MaxWorkforce. For example, if MaxWorkforce is say, 50:

If slider A's value is 50, BCD's max can only be 0.
If slider A's value is 20 and B's value is 10, CD's max can only be 20.
Edit: etc etc
If slider A's value is 0, ABCD's maxes are all 50.

Basically, I need to find a way so the player can "dish" out their workforce onto different jobs, while not exceeding their MaxWorkforce.
But how can I do this? Thanks.

ddq

Check in each slider's OnChange function if A+B+C+D > 50. If so, set the current slider's value to 50 minus the sum of the values of the other three sliders.

Khris

I'd do it differently:

In A's OnChange:

Code: ags
  float f = IntToFloat(MaxWorkForce - A) / IntToFloat(B + C + D);
  B = FloatToInt(f * IntToFloat(B));
  C = FloatToInt(f * IntToFloat(C));
  D = FloatToInt(f * IntToFloat(D));


Same for the three other sliders.

This will distribute the remainder among the other three sliders while keeping their proportions.

Atelier

Heh, when the slider's value is set to 0, it points to the first line in your code Khris (the world implodes when dividing by 0 =D) I know this can't be helped, for now I've simply set their default value at 1. But is there some other way around this, so their min value can be 0?

Other than that, thanks again!

Khris

Check if the sum is 0 first and if it is, skip the lines.

Code: ags
  int s = B + C + D;
  float f;
  if (s > 0) {
    f = IntToFloat(MaxWorkForce - A) / IntToFloat(s);
    B = FloatToInt(f * IntToFloat(B));
    C = FloatToInt(f * IntToFloat(C));
    D = FloatToInt(f * IntToFloat(D));
  }

Atelier


SMF spam blocked by CleanTalk