Is there any way to use variables with decimals?

Discussion in 'RPG Maker MV' started by shinichi999, Aug 8, 2017.

  1. shinichi999

    shinichi999 Veteran Veteran

    Messages:
    132
    Likes Received:
    13
    First Language:
    Spanish
    Greetings. I’m designing a videogame to learn English, so after some classes you will have an exam with the contents.

    In Chile the grades are from 1.0 to 7.0, but I can’t calculate the grade with decimals since RPG Maker MV does not allow it.

    For example, in a 16-question test, if the player has all 16 correct questions, the game would use the following formula:

    16/16 * 7 = 7

    If 15 questions are correct, then it would be 15/16 * 7 = 6.5.

    And so on.

    My question is: Is there a way to calculate and display grades (or variables) including decimals?

    As always, thank you very much in advance! :)
     
    #1
    TheTitan99 likes this.
  2. Tai_MT

    Tai_MT Veteran Veteran

    Messages:
    5,025
    Likes Received:
    4,108
    First Language:
    English
    Primarily Uses:
    RMMV
    I don't think you'd be able to display decimals, but instead of using 1.0, you could just use 10 instead. Maybe there's a way to get the text to display a decimal point in there where you desire. Maybe via conditional branches?

    If Variable "Score" is 16, display text: "You got a 1.6 score on the test". With a limited amount of questions such as 16, it'd be pretty easy to event the Variable and the text score for each, as they'd be limited numbers. You'd only need to find the amount of each percentage. Maybe make your variable be "between two numbers" in case you've got "runoff" of some kind?

    There might be a plugin for it though. However, I'm not sure, since most people don't really have any use for a decimal place in variables since a variable is usually used to keep track of something as a whole number. A stat, step numbers, time, item counts, used to calculate random numbers between two other numbers, etcetera.

    To be honest though, you don't really need to be using a variable as a decimal here anyway. You could just use the variable to keep track of "right answers". Variable increases by 1 for each right answer. If variable is 16, display text, "Perfect Score!", if variable is not 16, check to see if variable is 15. If it's 15, display text, "Nearly perfect!". If variable is not 15, check to see if variable is 14. Instead of actual text, you could just display the number variable as you want to here. It'll take about 10-20 minutes to put it into a single event on a single page, but it'll work perfectly as executed.
     
    #2
  3. TheTitan99

    TheTitan99 Veteran Veteran

    Messages:
    165
    Likes Received:
    65
    First Language:
    English
    Primarily Uses:
    RMMV
    In rpg_objects, on line 565, there is this code:

    if (typeof value === 'number') {
    value = Math.floor(value);
    }

    Basically, if what value is being entered into a variable is a number, which it usually is, the game will automatically round it down, so that it will be the nearest integer below.

    You can erase/comment out these lines, or if you prefer not editing the main javascript files, you can make a short simple plugin to overwrite the function.
     
    #3
  4. shinichi999

    shinichi999 Veteran Veteran

    Messages:
    132
    Likes Received:
    13
    First Language:
    Spanish
    Words perfectly! But the problem is:

    [​IMG]

    Is there a way to only show one decimal? (3.9 in this case).
     
    #4
    LTN Games likes this.
  5. LTN Games

    LTN Games Veteran Veteran

    Messages:
    603
    Likes Received:
    450
    Location:
    Canada
    First Language:
    English
    Primarily Uses:
    RMMV
    Just put this in the same spot you commented out the above code, keep the commented code in place and just place this above the comment.
    To change the decimal places change the number inside toFixed(2)
    Code:
    if (typeof value === 'number') {
    value = Number(value.toFixed(2));
    }
     
    #5
    shinichi999 and TheTitan99 like this.
  6. shinichi999

    shinichi999 Veteran Veteran

    Messages:
    132
    Likes Received:
    13
    First Language:
    Spanish
    Works like a charm!

    Thank you dude! :)

    [​IMG]
     
    #6
  7. shinichi999

    shinichi999 Veteran Veteran

    Messages:
    132
    Likes Received:
    13
    First Language:
    Spanish
    Sorry but I'm having another problem.

    Let's think in a test with 22 points. The formula that I use is:

    X=22 / 22 * 7

    So.. if you got 22 points, then the result (1) is multiplied by 7 and then you got a 7 (the maximum grade).

    With 21 points I got a 6.6 in the calculator (21 / 22 * 7), but I still got a 7 in the game. That's what I can't understand.

    Is there a way to fix this? Please I really need help with this.

    Thank you very much in advance!

    Oh by the way... I can't use the conditional branch because I need to save the grade or variable in order to be showed after.
     
    #7
  8. Verdelite

    Verdelite Wannabe Developer Veteran

    Messages:
    144
    Likes Received:
    1,041
    First Language:
    German
    Primarily Uses:
    RMMV
    I think the problem is that 21/22 is ~ 0.95 which if rounded to one decimal will become 1.0.
    Does it work if you calculate ((21 * 7) / 22) instead?
     
    #8
    shinichi999 likes this.
  9. shinichi999

    shinichi999 Veteran Veteran

    Messages:
    132
    Likes Received:
    13
    First Language:
    Spanish
    I'm crying haha... works perfectly now! Thank you very much, dude! :D
     
    #9
    Verdelite likes this.

Share This Page