Jump to content

Photo

How do I limit a script event to trigger once a second?


  • Please log in to reply
11 replies to this topic

#1
123hamster

123hamster

    Enthusiast

  • Members
  • PipPip
  • 106 posts

So I have a simple script that cast a spell whenever I get hit, which is attached to a permanent magic effect with conditions.  Problem is, when I get hit by flame or frost it triggers like 100 times a second and crash my PC, before my conditions kick in to make it stop.  How do I limit the event to trigger only once per second?

 

Scriptname MagicCastOnHit extends ActiveMagicEffect  
{This script cast a spell on hit.}

;======================================================================================;
;  PROPERTIES  /
;=============/

SPELL PROPERTY armorSpell AUTO

;======================================================================================;
;  VARIABLES   /
;=============/

Actor TargetActor

;======================================================================================;
;   EVENTS     /
;=============/
Event OnEffectStart(Actor Target, Actor Caster)
    TargetActor = Target
EndEvent

Event OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack, bool abSneakAttack, bool abBashAttack, bool abHitBlocked)

        armorSpell.cast(TargetActor, TargetActor)

EndEvent

 


Edited by 123hamster, 07 October 2021 - 01:15 am.


#2
rkkn

rkkn

    Enthusiast

  • Members
  • PipPip
  • 189 posts

One way to do it is to have the script go into a cooldown state as soon as the OnHit event is called, and then have the OnHit event do nothing while in cooldown state

 

example:

Event OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack, bool abSneakAttack, bool abBashAttack, bool abHitBlocked)

	GoToState("Cooldown")
	
	armorSpell.cast(TargetActor, TargetActor)

	RegisterForSingleUpdate(1)

EndEvent

State Cooldown
	Event OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack, bool abSneakAttack, bool abBashAttack, bool abHitBlocked)
	EndEvent
EndState

Event OnUpdate()
	GoToState("")
EndEvent

Edited by rkkn, 07 October 2021 - 01:33 am.


#3
123hamster

123hamster

    Enthusiast

  • Members
  • PipPip
  • 106 posts

Thanks, it works great.

 

Edit:  Though I don't immediately crash, my video card still wind up freezing up after a while.  I probably should avoid using this script.


Edited by 123hamster, 07 October 2021 - 04:31 am.


#4
123hamster

123hamster

    Enthusiast

  • Members
  • PipPip
  • 106 posts

Thanks again for the code, this wind up being very useful as a replacement for the original MagicPlayEffectShaderOnHitScript.  I didn't realize the original script was this problematic.

Event OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack, bool abSneakAttack, bool abBashAttack, bool abHitBlocked)

	GoToState("Cooldown")
	
	MagicEffectShader.play(TargetActor,ShaderDuration)

	RegisterForSingleUpdate(1)

EndEvent

State Cooldown
	Event OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack, bool abSneakAttack, bool abBashAttack, bool abHitBlocked)
	EndEvent
EndState

Event OnUpdate()
	GoToState("")
EndEvent

 



#5
ReDragon2013

ReDragon2013

    Old hand

  • Members
  • PipPipPip
  • 740 posts

vanilla script as follow:

Spoiler

 

MagicPlayEffectShaderOnHitScript

Spoiler

Edited by ReDragon2013, 20 November 2021 - 12:53 am.


#6
123hamster

123hamster

    Enthusiast

  • Members
  • PipPip
  • 106 posts

Thanks for your interest.

 

So just a n00b question, would

Float PROPERTY ShaderDuration auto  

Work without modify every magical effect that uses this script?  Do we have to go into each mage armor (and whatever else that uses it), and set property to 0.0?



#7
IsharaMeradin

IsharaMeradin

    The Pale Redguard

  • Members
  • PipPipPipPipPip
  • 10,920 posts

Thanks for your interest.

 

So just a n00b question, would

Float PROPERTY ShaderDuration auto  

Work without modify every magical effect that uses this script?  Do we have to go into each mage armor (and whatever else that uses it), and set property to 0.0?

 

Modify the original script and keep the property line as-is rather than changing it to the way ReDragon had it.  You will then be able to have the default value of 0.0 for any records that do not have an overriding value assigned in the Creation Kit's property interface.

float property ShaderDuration = 0.00 auto


#8
ReDragon2013

ReDragon2013

    Old hand

  • Members
  • PipPipPip
  • 740 posts

123hamster asked:

 

1. "Work without modify every magical effect that uses this script?"

YES

 

2. "Do we have to go into each mage armor (and whatever else that uses it), and set property to 0.0?"

not really, see answer of IsharaMeradin

 

Just in addition to IsharaMeradins posting to make the things hopefully more clearly.

PapyrusSampleScript

Spoiler

 

 Script initialization runs before event OnInit() is ready to start any code inside!

 And if event OnInit() has been finished any other event or function inside the script can be triggered or called. Thats the reason why anyone should avoid this:

 

DO NOT DO THAT!

Spoiler

What is the different between (A)

  Float PROPERTY fTimer       auto            ; [default=0.0], the property starts with this value
  Int   PROPERTY iCounter     auto            ; [default=0]
  Bool  PROPERTY bRun         auto            ; [default=False]

and these with property pre-selection by script (B)

  Float PROPERTY fTimer = 0.0 auto            ; [default=0.0]
  Int   PROPERTY iCounter = 0 auto            ; [default=0]
  Bool  PROPERTY bRun = False auto            ; [default=False]
Spoiler

Edited by ReDragon2013, 20 November 2021 - 04:56 pm.


#9
123hamster

123hamster

    Enthusiast

  • Members
  • PipPip
  • 106 posts

alright cool, so I can just copy and compile as is with no esp changes?  Thanks.

 

Never would have thought that int/float/bool would make a difference when the code will run either way (but slower).

 

Hopefully Mage Armor stop crashing my game when hit by multiple flame spells.


Edited by 123hamster, 21 November 2021 - 08:10 am.


#10
123hamster

123hamster

    Enthusiast

  • Members
  • PipPip
  • 106 posts

77820908-1637478745.jpg

It works!  60 FPS.  Would you mind uploading this fix on Nexus or should I do it?  I can't be the only one who needed this fix.






IPB skins by Skinbox
Page loaded in: 0.281 seconds