Simple math with variables and script calls?

Discussion in 'Learning Javascript' started by Parallax Panda, Aug 15, 2019 at 5:09 PM.

  1. Parallax Panda

    Parallax Panda Got into VxAce ~2014 and never stopped... Veteran

    Messages:
    599
    Likes Received:
    989
    Location:
    Fukuoka, Japan
    First Language:
    Swedish
    Primarily Uses:
    VNM
    Okay, maybe this isn't really Javascript? Maybe it is? I dunno because I'm a complete chump when it comes to programming (and math in general), but I think this might be the correct section of the forums to ask this. At least most people on this board probably know how to do what I'm asking.

    So, I want to do some simple math calculations using the values I got stored in different variables, and put the answer into a new variable. Basically math similar to what you could do in the damage formulas (add, subtract, multiply, divide). I think this should be doable with a script call, correct?

    What I don't know is how to do it. I've never really done something like it before in MV and it's probably pretty straight forward but after googling for a while, I couldn't find an answer to how to write the formulas.

    Here's one of my non-working stabs at doing some math with the control variable and script call commands:

    $gameVariables.value(105)-$gameVariables.value(104)*10-$gameVariables.value(103)

    ...didn't work. :kaocry:

    Thanks in advance.
     
    #1
  2. Shaz

    Shaz Veteran Veteran

    Messages:
    37,317
    Likes Received:
    11,126
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    So you can use the Control Variables event command, putting the variable you want the result in as the top part, and the formula in the script box.

    If you do the following:
    Code:
    Control Variables [100: result] = Script: $gameVariables.value(105) - $gameVariables.value(104) * 10 - $gameVariables.value(103)
    that will give you a result, but it might not be what you expect.

    Remember the mathematical order of operations - the multiply ($gameVariables.value(104) * 10) is the first thing that will happen. Then that will be deducted from variable 105, and finally variable 103 will be deducted from the result.

    So if you have var 105 = 10, 104 = 5, and 103 = 2, it'll be this:
    10 - 5 * 10 - 2 which is
    10 - 50 - 2 which is
    -38

    If you can't figure it out from there, give us examples of what's in the variables, what you expect to get as a result, and what you're getting as a result.
     
    #2
  3. Parallax Panda

    Parallax Panda Got into VxAce ~2014 and never stopped... Veteran

    Messages:
    599
    Likes Received:
    989
    Location:
    Fukuoka, Japan
    First Language:
    Swedish
    Primarily Uses:
    VNM
    @Shaz
    Thank you. Actually, I was so heavily focused on the formula that I didn't notice that the rest of my eventing was screwed up and didn't work - which is why I didn't get any result at all. After you pointed out the order of operations I corrected the formula and it works as intended for now.

    Follow up question though. Is it possible to add a "if the result is above/below this, then show X instead?".
     
    #3
  4. Shaz

    Shaz Veteran Veteran

    Messages:
    37,317
    Likes Received:
    11,126
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    I would need to see the context, as what I've given you is just a formula to calculate a variable - nothing about conditions in there. Can you post a pic of your event?
     
    #4

Share This Page