Chaos Project

RPG Maker => General Discussion => Topic started by: Yuukanna on December 29, 2009, 08:17:25 pm

Title: Highest Valued Variable
Post by: Yuukanna on December 29, 2009, 08:17:25 pm
I'm using RMXP.

I was about to create an event in which I am going to need to determine which variable in a series of 20 variables holds the highest value. Before I started programming it out a piece at a time with conditional branches I thought to myself, "There has GOT to be an easy way to do this"

My question is: Is there a way to determine which variable has the highest number value in a series of numbers easily without having to tell the computer to compare each individual value, over and over and over?
Title: Re: Highest Valued Variable
Post by: Blizzard on December 30, 2009, 07:27:37 am
Not without using a small script.
Title: Re: Highest Valued Variable
Post by: Yuukanna on December 30, 2009, 01:10:31 pm
That's what I thought. Maybe I'll figure out how the script would work, but for now I guess I'll stick with eventing and using a series of if x > n and so on. As far as the extent of my knowledge in scripting, I'd be doing the same thing in the script.

Thanks anyway.
Title: Re: Highest Valued Variable
Post by: Blizzard on December 30, 2009, 04:20:30 pm
You can put this in a script call:

ids = [Y, Z, U, V]
max = $game_variables[ids.shift]
ids.each {|id|
 if max < $game_variables[id]
   max = $game_variables[id]
 end}
$game_variables[X] = max


Change X to the variable ID that will contain the maximum value in the end. Change Y, Z, U, V to IDs of the variables that need to be checked.
I haven't tested this, I just wrote it out of my head.
Title: Re: Highest Valued Variable
Post by: Yuukanna on December 31, 2009, 02:20:29 pm
Wow, I didn't know about the "max" command edit:Ok, N/M I see what you did there. That's awesome, I'll test it out and see how it goes. thanks!

Edit: Ok, I tested it, it worked as great as expected. But it gave me the value that the variable held, not the variable ID number which is what I was aiming for.

Here's what the code ended up looking like:

ids = [11, 12, 13, 14, 15, 16, 17, 
18, 19, 20, 21, 22, 23, 24, 26, 27,
28, 30]
max = $game_variables[ids.shift]
ids.each {|id|
 if max < $game_variables[id]
   max = $game_variables[id]
 end}
$game_variables[31] = max


In my test, variable ID #28 ended up with the highest valued number at 7.
I tested the code by using a simple message that said "test \v[31]"
It said "test 7" rather than "test 31" as I hoped.

Is there a way to identify change the max variable to equal the variable ID # rather than it's value?

Edit: I'm pretty much a n00b with scripting, and this now seems to be moving on to a script request topic, but here is what I came up with, does it make any sense at all? (I tried to test it but it's too large to fit in the script call, and i'm not sure how to arrange it not to error in the script editor)

ids = [11, 12, 13, 14, 15, 16, 17, 
18, 19, 20, 21, 22, 23, 24, 26, 27,
28, 30]
max = $game_variables[ids.shift]
ids.each {|id|
  if max < $game_variables[id] &
        max = $game_variables[11] : max = 11
  elsif max = $game_variables[12] : max = 12
  elsif max = $game_variables[13] : max = 13
  elsif max = $game_variables[14] : max = 14
  elsif max = $game_variables[15] : max = 15
  elsif max = $game_variables[16] : max = 16
  elsif max = $game_variables[17] : max = 17
  elsif max = $game_variables[18] : max = 18
  elsif max = $game_variables[19] : max = 19
  elsif max = $game_variables[20] : max = 20
  elsif max = $game_variables[21] : max = 21
  elsif max = $game_variables[22] : max = 22
  elsif max = $game_variables[23] : max = 23
  elsif max = $game_variables[24] : max = 24
  elsif max = $game_variables[26] : max = 26
  elsif max = $game_variables[27] : max = 27
  elsif max = $game_variables[28] : max = 28
  elsif max = $game_variables[30] : max = 30
  end}
$game_variables[31] = max
Title: Re: Highest Valued Variable
Post by: Blizzard on December 31, 2009, 04:08:08 pm
ids = [Y, Z, U, V]
max = ids.shift
ids.each {|id|
  if $game_variables[max] < $game_variables[id]
    max = id
  end}
$game_variables[X] = max


Now variable X will contain the ID of the variable which has the highest value.
Title: Re: Highest Valued Variable
Post by: Yuukanna on December 31, 2009, 04:43:01 pm
Works perfectly! Thanks!  :haha:

I guess I showed off how little I really know about Ruby & RGSS.
Title: Re: Highest Valued Variable
Post by: DrakoShade on January 11, 2010, 09:15:44 am
I was going to say this thought process would be easier, but it turned out to be roughly similar and just taking it from a different direction.
ids = [A, B, C, D, ... ]
vars = []
ids.each {|id|
 vars[i] = $game_variables[ids[i]]}
$game_variables[X] = vars.max

(Note, this should do the same as the first code posted and give you the value of the highest variable, rather than the id.  I should have kept going past the first solution posted before a reply burned its way through my keyboard.   :oops: