senterpat Posted November 2, 2018 Share Posted November 2, 2018 Is there a simple way to find which is the highest global of a set of globals in a script?The only way I can think of is if Example > Other && Example > Other2 Etc Link to comment Share on other sites More sharing options...
n000dlz Posted November 2, 2018 Share Posted November 2, 2018 Hi TigerI'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 More sharing options...
Evangela Posted November 2, 2018 Share Posted November 2, 2018 (edited) 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 November 2, 2018 by Rasikko Link to comment Share on other sites More sharing options...
DieFeM Posted November 2, 2018 Share Posted November 2, 2018 @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 More sharing options...
n000dlz Posted November 2, 2018 Share Posted November 2, 2018 @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 More sharing options...
DieFeM Posted November 2, 2018 Share Posted November 2, 2018 Oops... You're right n000dlz! That's a weird behavior, my apologies Rasikko. Link to comment Share on other sites More sharing options...
Evangela Posted November 2, 2018 Share Posted November 2, 2018 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 More sharing options...
DieFeM Posted November 2, 2018 Share Posted November 2, 2018 There's no problem with negative values. The only problem would be if all the values in the array are less than -9999 using your function, in which case would return -9999.0. Link to comment Share on other sites More sharing options...
senterpat Posted November 4, 2018 Author Share Posted November 4, 2018 Awesome, thanks guys :D Link to comment Share on other sites More sharing options...
Evangela Posted November 4, 2018 Share Posted November 4, 2018 (edited) 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 November 4, 2018 by Rasikko Link to comment Share on other sites More sharing options...
Recommended Posts