Jump to content

Get Highest Value


senterpat

Recommended Posts

Hi Tiger

I'm pretty new to Papyrus but I think the best way is to add each value to an array then use a while loop(while int i < array.length) to itterate through the array, saving the highest value to a variable.

 

Hope that helps!

Link to comment
Share on other sites

Make a globalvariable array and put the globals in it, then pass the array to this function.

Float Function GetHighestGlobalValue(GlobalVariable[] akGlobal)
    Int index = akGlobal.Length
    Float fHighestValue = -9999.0 ; a large negative number so you can handle negative values.
 
    While index
        index -= 1
        Float CurrentValue = akGlobal[index].GetValue()
        if CurrentValue >= fHighestValue
            fHighestValue = CurrentValue
        endif
    EndWhile
 
    return fHighestValue
EndFunction
Edited by Rasikko
Link to comment
Share on other sites

@Rasikko, I hate doing this, I'm sorry for correcting you, but your loop is infinite (the condition of the while loop must become false at some point), that's my approach based on your function:

GloabalVariable[] Property MyArrayOfGlobals Auto Const

Float Function GetHighestGlobalValue(GlobalVariable[] akGlobal)
	Float fHighestValue
	Int i = 0
	While i < akGlobal.Length
		Float CurrentValue = akGlobal[i].GetValue()
		If (i == 0 || CurrentValue > fHighestValue)
			fHighestValue = CurrentValue
		EndIf
		i += 1
	EndWhile
	Return fHighestValue
EndFunction

Event Whatever()
	Debug.MessageBox(GetHighestGlobalValue(MyArrayOfGlobals))
EndEvent
Link to comment
Share on other sites

 

@Rasikko, I hate doing this, I'm sorry for correcting you, but your loop is infinite (the condition of the while loop must become false at some point), that's my approach based on your function:

GloabalVariable[] Property MyArrayOfGlobals Auto Const

Float Function GetHighestGlobalValue(GlobalVariable[] akGlobal)
	Float fHighestValue
	Int i = 0
	While i < akGlobal.Length
		Float CurrentValue = akGlobal[i].GetValue()
		If (i == 0 || CurrentValue > fHighestValue)
			fHighestValue = CurrentValue
		EndIf
		i += 1
	EndWhile
	Return fHighestValue
EndFunction

Event Whatever()
	Debug.MessageBox(GetHighestGlobalValue(MyArrayOfGlobals))
EndEvent

Personally, I think Rasikko's loop will stop at 0(false)? Strange way to do it tho. Yours is the usual go-to method from my experience.

Link to comment
Share on other sites

It's all good man. Either approach is fine. 'While index' looks like it will go infinity however, the following 'Index -=1' means it keeps looping til its gone through the entire array. That's not to say infinito cant happen. I've done it a few times lol..

 

However for future reference, should you use your approach for any modding - do account for negative values. If all globals/values are negative, you'll be scratching your head wondering why the function keeps returning 0.

Link to comment
Share on other sites

I couldn't replicate the results with mine, so I broke yours down to see what was being calculated differently. It seems that your i == 0 check allows your function to account for negative values without initializing the variable to a high negative value first. I remember when I first wrote that code some years back and I couldn't get around the issue without first making the variable a high negative number.

Edited by Rasikko
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...