Difference between revisions of "User:DavidJCobb/Stack dumping"

820 bytes removed ,  19:34, 2 March 2015
m
→‎Code snippets: combined two functionally-identical snippets
imported>DavidJCobb
(→‎Code snippets: More snippets, and explanations for all snippets.)
imported>DavidJCobb
m (→‎Code snippets: combined two functionally-identical snippets)
Line 40: Line 40:
* RegisterForSingleUpdate(''n'') does ''not'' create a stack and suspend it for ''n'' seconds. I have confirmed this experimentally.
* RegisterForSingleUpdate(''n'') does ''not'' create a stack and suspend it for ''n'' seconds. I have confirmed this experimentally.


== Code snippets ==
=== Process a task OnHit, and wait for X seconds during or after the task ===
 
=== Process a task OnHit, and don't re-process for X seconds ===
This code blocks concurrent OnHit calls, so we don't try to process multiple hits on the same actor at the same time. It also blocks calls for X seconds after we've finished processing a hit.
 
<source lang="Papyrus">
Bool Property pbBusy = False Auto Hidden ; prevents concurrent execution
Bool Property pbProcessed = False Auto Hidden ; prevents re-processing
 
Event OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, Bool abPowerAttack, Bool abSneakAttack, Bool abBashAttack, Bool abHitBlocked)
  If pbBusy || pbProcessed
      Return
  EndIf
  pbBusy = True
  ;
  ; Do your processing here.
  ;
  pbProcessed = True
  RegisterForSingleUpdate(x) ; replace "X" with the seconds you want to spend ignoring further OnHit events
  pbBusy = False
EndEvent
 
Event OnUpdate()
  pbProcessed = False
EndEvent
</source>
 
=== Process a task OnHit, and wait for X seconds during the task ===
This simple example blocks concurrent calls, and replaces a Utility.Wait(X) call with a RegisterForSingleUpdate(X) call. If we used Utility.Wait(X), we'd suspend our stack for X seconds; by using RegisterForSingleUpdate and OnUpdate, we get rid of the stack entirely during those X seconds, and create a new stack to continue processing after the delay.
This simple example blocks concurrent calls, and replaces a Utility.Wait(X) call with a RegisterForSingleUpdate(X) call. If we used Utility.Wait(X), we'd suspend our stack for X seconds; by using RegisterForSingleUpdate and OnUpdate, we get rid of the stack entirely during those X seconds, and create a new stack to continue processing after the delay.


Line 74: Line 47:


Event OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, Bool abPowerAttack, Bool abSneakAttack, Bool abBashAttack, Bool abHitBlocked)
Event OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, Bool abPowerAttack, Bool abSneakAttack, Bool abBashAttack, Bool abHitBlocked)
   If pbBusy || pbProcessed
   If pbBusy
       Return
       Return
   EndIf
   EndIf
Line 86: Line 59:
Event OnUpdate()
Event OnUpdate()
   ;
   ;
   ; Continue processing here.
   ; Continue processing here (or do nothing, if your goal was to wait X seconds after processing a task before allowing more tasks to be processed).
   ;
   ;
   pbBusy = False ; We're done!
   pbBusy = False ; We're done!
Anonymous user