Shortening mathematic expressions?

This forum is currently in read-only mode.
From the Asset Store
Choose the right elementary arithmetic and complete the equation!
  • I've attached the .cap and .ini to this post in a zip file

    I have an .ini file with three values:

    Value1

    Value2

    Value3

    I'm adding all three values, then dividing them by three to get the sum. Here is expression:

    INI.ItemValue("Math", "Value1")/3+INI.ItemValue("Math", "Value2")/3+INI.ItemValue("Math", "Value3")/3

    It works fine, the math comes out right of course, but I plan to do this with as many as 20 values so the expression is going to wind up very long.

    I know I'm probably having wishful thinking, probably nitpicking myself, but is there a way to shorten it without screwing up the math?

  • I've never used Construct classic for a long time, so maybe it'll not work, but maybe :

    First initialize a variable:

    CurrentSum = 0

    then do a repeat loop (here you would repeat 3 times):

    add INI.ItemValue("Math", "Value"&loopindex+1) to CurrentSum

    after the loop, divide CurrentSum by 3, and here you have it

    (if you want to do it 20 times, then repeat 20 times, and divide 20 times the CurrentSum value instead of 3)

  • Very practical code setup there. I just had to add a "Trigger once" condition because it wouldn't stop adding without it, I also dropped the +1 for loopindex (no effect, no issue) and instead of using a loop repeat event (wasn't responding but no effect, no issue) I used a "For" and named "CurrentSum" as the loop, then set it 0 to 3 :

    + System: Start of layout

    -> INI: Set INI file to AppPath + "math.ini"

    + System: Trigger once

    + System: For "CurrentSum" from 0 to 3

    -> System: Add INI.ItemValue("Math", "Value"&loopindex) to global variable 'CurrentSum'

    -> Text: Set text to global('CurrentSum')/3

    Thanks so much, Aphrodite! This prevented an ugly expression. I really appreciate the help!

  • Glad to see you sorted it out ^-^

    I did not have CC so I couldn't test on it, sorry about that.

    One thing that could be done is maybe to replace the 3 with a variable value (like counting the elements inside the file, or maybe pass the value as a function parameter, and have the return value as the last currentsum/3, I don t even know if CC have functions implemented the same way that C2 does), but if that is currently enough for you, no problem

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thanks for the follow-up, Aphrodite. Sounds like some more good ideas to explore. No need to apologize, your tip was exactly what I needed

    Thanks again!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)