Mass average calculation

Solo

Veteran
Veteran
Joined
Jul 26, 2013
Messages
1,104
Reaction score
154
First Language
English
Primarily Uses
RMVXA
I need to calculate the average of 190 variables. Is there any way to do this efficiently with a script call? Unfortunately, they're not all in a row, if that makes a difference.

As a test, I tried assigning a range to a variable, but I got the error "Array can't be coerced into Fixnum," so... I came here for help.
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,203
First Language
Binary
Primarily Uses
RMMZ
# Script Call :# Solo_Variable.get_avg# module Solo_Variable # Array of variable ID's Vars = [1,2,3,4,5,6,7,8,9] # Method. Do not change. def self.get_avg Vars.compact.inject(1) {|r,i| r += ($game_variables.to_i rescue 0) } / Vars.compact.size endend ;)
 
Last edited by a moderator:

Solo

Veteran
Veteran
Joined
Jul 26, 2013
Messages
1,104
Reaction score
154
First Language
English
Primarily Uses
RMVXA
Is there a way to assign the obtained average to a variable and display it (like with "\v[x]" in a text box)?
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,203
First Language
Binary
Primarily Uses
RMMZ
# Script Calls :# Solo_Variable.get_avg << returns the value of the variable average.# # Solo_Variable.set_avg << Sets average value into variable Text Var ID# module Solo_Variable # Array of variable ID's Vars = [1,2,3,4,5,6,7,8,9] Text_Var_ID = 10 # # Methods. Do not change. def self.get_avg Vars.compact.inject(1) {|r,i| r += ($game_variables.to_i rescue 0) } / Vars.compact.size end def self.set_avg $game_variables[Text_Var_ID] = get_avg endendThen use \v[number] in text box :)
 

Solo

Veteran
Veteran
Joined
Jul 26, 2013
Messages
1,104
Reaction score
154
First Language
English
Primarily Uses
RMVXA
I can't get it to work... ;_; It just displays either 0 or 1 in the text box.


I can change "Vars = [x]" to a range, right? Like "[21..39]"?
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,203
First Language
Binary
Primarily Uses
RMMZ
No, you have to manually insert each variable id. Cause you said they werent all in a row i wrote it that way. Unfortunately, it means you have to type out 190 variable id numbers, but it will work :D

just pop a comma in between each variable ID number. You can go to a new line as well providing the last character on the previous line was a comma.. like this...

Code:
Vars = [1,2,3,4,5,6,7,8,9,10,            11,12,13,14,15,16,17,18,19,20]
 

Solo

Veteran
Veteran
Joined
Jul 26, 2013
Messages
1,104
Reaction score
154
First Language
English
Primarily Uses
RMVXA
I appreciate all your help, but I still must not be doing something right...


Prior to the script call, I set variables 21-28 to 250, and 29-39 to 500. I manually enter variables 21-39 into the "Var" array in your script. I have "Text_Var_ID" set to 60. I put "\v[60]" in a message box, and it comes out 0 for some reason. :(


I'm sorry to hassle you like this. Also, I might be willing to restructure my variables if a method for a range (possibly several ranges separated by commas, e.g. "21..39, 54..62" etc.) could easily be created. I'm not sure all 190 variables would fit in the script box anyway... even though the update made the box a lot bigger, it still seems to have a height/line limit that's about half of the visible area.
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,203
First Language
Binary
Primarily Uses
RMMZ
Script box??

The only thing in the script box should be the one line script call...

Solo_Variable.set_avg

The rest of it is code to be inserted somewhere in your script editor below materials and above main. Actually, as long as its before main...

Then you use the script call and display the variable text as normal.
 

Solo

Veteran
Veteran
Joined
Jul 26, 2013
Messages
1,104
Reaction score
154
First Language
English
Primarily Uses
RMVXA
Script box??

The only thing in the script box should be the one line script call...

Solo_Variable.set_avg

The rest of it is code to be inserted somewhere in your script editor below materials and above main. Actually, as long as its before main...

Then you use the script call and display the variable text as normal.
...

I am a funny person...

So, so funny...

...Thanks. :blush:
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,203
First Language
Binary
Primarily Uses
RMMZ
...

I am a funny person...

So, so funny...

...Thanks. :blush:
lol so did you get it working ok now? thats the main thing really. :D
 

Solo

Veteran
Veteran
Joined
Jul 26, 2013
Messages
1,104
Reaction score
154
First Language
English
Primarily Uses
RMVXA
lol so did you get it working ok now? thats the main thing really.
Yes... yes I did. Thank you so much. :D

I was putting something in the script box that belonged in the script editor... that is so delightfully amusing.
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,513
Reaction score
3,203
First Language
Binary
Primarily Uses
RMMZ
lol live and learn ^_^

I could accept partial responsibility for not initially stating where to pop it :D

Sits waiting for Shaz to close another thread I have solved before her Mwahaha...

Detective Dek - on the case...
 
Last edited by a moderator:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c

Forum statistics

Threads
105,857
Messages
1,017,019
Members
137,564
Latest member
McFinnaPants
Top