pepperman35 Posted October 8, 2021 Share Posted October 8, 2021 In the context of the code snippet below, could someone explain to me what %= does? I get that += advances iSoundToPlay by 1, but not getting how to interpret %= with regard to iSoundToPlay. int iLastSound = -1 Function TriggerRandomHallucinationAudio() ;Play a random sound effect near the player ObjectReference PlayerRef = Game.GetPlayer() int iSoundToPlay = Utility.RandomInt(0, 3) if iSoundToPlay == iLastSound iSoundToPlay += 1 iSoundToPlay %= 4 endif Link to comment Share on other sites More sharing options...
SKKmods Posted October 8, 2021 Share Posted October 8, 2021 Its an ECMA standard script expression for remainder assignment. Never used it myself in Papyrus tho. looking at that script maybe this is cleaner: Int iSoundToPlay = iLastSoundWhile iSoundToPlay == iLastSound iSoundToPlay = Utility.RandomInt(0, 3)EndWhile iLastSound = iSoundToPlay Link to comment Share on other sites More sharing options...
pepperman35 Posted October 9, 2021 Author Share Posted October 9, 2021 Thanks for the infos and improvements. Link to comment Share on other sites More sharing options...
SKKmods Posted October 9, 2021 Share Posted October 9, 2021 A side note I am extremely careful using unbounded while loops without an incrementing iBailout counter condition to avoid perpetucal hanging scripts, like: Int iSoundToPlay = iLastSoundInt iBailout = 0While (iSoundToPlay == iLastSound) && (iBailout < 5) ; limit to 5 frames iSoundToPlay = Utility.RandomInt(0, 3) iBailout +=1EndWhile iLastSound = iSoundToPlay In this case a randomint pick where 3 of 4 values break the loop is low risk (unless the RNG is beyond broken) so not really necesary. Link to comment Share on other sites More sharing options...
pepperman35 Posted October 9, 2021 Author Share Posted October 9, 2021 (edited) Makes sense. Now let’s advance the discussion a bit, shall we. Let’s say I have generated a random number between 0 and 20 and I want to act on its values. Now one could set this up with a rather long IF ELSEIF statement but that seems a bit nasty to me. Is there a better mousetrap? BTW, I am pulling this example from DLC03AtomSpringScript. Edited October 9, 2021 by pepperman35 Link to comment Share on other sites More sharing options...
SKKmods Posted October 9, 2021 Share Posted October 9, 2021 If each value triggers a discrete outcome which can not be keyed to the value (an array, formlist or refcollectrion getat index), since Papyrus doesnt have a switch/case structure, If ... elseif is probably the way. Looking at that DLC script I would drag my sound selection into a formlist of sounds and look up the index: Int iSoundToPlay = iLastSoundInt iBailout = 0Int iUpperValue = pMyFormListOfSounds.GetSize() -1 ; the 'ol index starts at zero thing While (iSoundToPlay == iLastSound) && (iBailout < iUpperValue) ; limit to the length of the form list iSoundToPlay = Utility.RandomInt(0, iUpperValue) iBailout +=1EndWhile iLastSound = iSoundToPlay Sound theSound = pMyFormListOfSounds.GetAt(iSoundToPlay) as Sound theSound.Play(pPlayerREF) Link to comment Share on other sites More sharing options...
pepperman35 Posted October 9, 2021 Author Share Posted October 9, 2021 alright then, that sounds a little more efficient. Thanks for the response. I am getting way too involved in this moonshine business, might need a little taste of it myself. Medicinal purposes... cough...cough. Once again, I do appreciate you helping us padawan scripters. Link to comment Share on other sites More sharing options...
Recommended Posts