Jump to content

script question: meaning of %=


Recommended Posts

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

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 = iLastSound

While iSoundToPlay == iLastSound
iSoundToPlay = Utility.RandomInt(0, 3)
EndWhile
iLastSound = iSoundToPlay
Link to comment
Share on other sites

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 = iLastSound

Int iBailout = 0

While (iSoundToPlay == iLastSound) && (iBailout < 5) ; limit to 5 frames
iSoundToPlay = Utility.RandomInt(0, 3)
iBailout +=1
EndWhile
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

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 by pepperman35
Link to comment
Share on other sites

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 = iLastSound

Int iBailout = 0

Int 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 +=1
EndWhile
iLastSound = iSoundToPlay

Sound theSound = pMyFormListOfSounds.GetAt(iSoundToPlay) as Sound

theSound.Play(pPlayerREF)

Link to comment
Share on other sites

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

  • Recently Browsing   0 members

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