Shortening mathematic expressions?

For questions about using Classic.

Post » Thu Apr 10, 2014 7:36 pm

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?
You do not have the required permissions to view the files attached to this post.
B
12
S
4
G
3
Posts: 426
Reputation: 2,458

Post » Fri Apr 11, 2014 11:38 pm

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)
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
52
S
22
G
18
Posts: 2,122
Reputation: 17,093

Post » Sun Apr 13, 2014 1:55 am

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!
You do not have the required permissions to view the files attached to this post.
Last edited by MrMiller on Sun Apr 13, 2014 1:44 pm, edited 1 time in total.
B
12
S
4
G
3
Posts: 426
Reputation: 2,458

Post » Sun Apr 13, 2014 11:45 am

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
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
52
S
22
G
18
Posts: 2,122
Reputation: 17,093

Post » Sun Apr 13, 2014 1:54 pm

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 :D

Thanks again!
B
12
S
4
G
3
Posts: 426
Reputation: 2,458


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 3 guests