Forum rules - please read before posting.

Rounding to 2 Decimal Places in Action Lists

Hi!

So I've been coding a Pricing minigame lately, However I've run into a problem, 

Two of the inputs of my pricing minigame include adding and subtracting percentages and this is often introducing a third or fourth decimal place in my final calculation - Which considering it's supposed to be running on Money - just isn't going to work.

Is there a way I can round my decimal points in the variable formula? 

Thanks! <3



Comments

  • Not in the formula, no.  Your best option is to write a custom Action that gets called afterward, which takes the value (by reading the variable), does the rounding and assigns it as the value of another String variable.

    You can get a float variable's value with:
    AC.GlobalVariables.GetFloatValue (2); // For a float variable of ID = 2

    And set a string variable's value with:
    AC.GlobalVariables.SetStringValue (3, "My new text"); // For a string variable of ID = 3
  • edited January 2016
    Hi Chris,

    Sorry the tutorial on custom action isn't very clear how it's calling these functions from inside the script. My scripting experience is heavily based in AGS, So I'm not entirely sure how the script is being run.

    I currently have my code sitting in public override string SetLabel () from the template but I just don't see anything elsewhere in that script that is calling for that function to run.

    This is what I currently have in the function - 

    public override string SetLabel ()
    {
    // Return a string used to describe the specific action's job.
    if (Puz_Price_Float > 0) { \\Initial float value has been set to 1 and will never be zero based on scripting
    Puz_Price_Float = AC.GlobalVariables.GetFloatValue (9);
    Puz_Price_String = Puz_Price_Float.ToString ("F2");
    AC.GlobalVariables.SetStringValue (22, Puz_Price_String);
    return "";
    }
    return "";
    }

    It seems happy enough and I went testing it I did see it update the string to two decimal places correctly but it only happened once and I could not get it to run again. Which considering it's been set up to run every time one of 5 buttons is pressed is a little weird.

    Sorry to be a pain and an annoyance - It's probably one tiny tiny little thing I'm missing but without that link I'm just going around in circles.

    Thanks again. <3

  • edited January 2016
    The Run() function is where the actual job of the Action is performed.  SetLabel() is merely used to print details of the Action in the header of it's UI - as stated by the comment inside it by default.

    You can safely remove SetLabel from your script, and move the code you've written there into your Run function, returning 0f as it only needs to be run once.

    A more detailed description of how Actions work can be found in the ActionTemplate.cs file itself, which has comments throughout.
  • Awesome, Works perfectly. 

    Thank you so much for your help. <3
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.