Jump to content

[LE] How should I write a script that activates upon switching weapon with hotkeys ?


Recommended Posts

So I'm trying to modify a mod with an open source, the mod either has the mod's sword sheathed or drawn depending on the situation, so if I just draw and sheathe using the r key it works, but if i'm using hotkey, like switching from spell / another weapon to this sword using a number hotkey from favorite menu for example, it doesn't appear and with iequip or skyrimsouls neither, is there a function that lets you know when you're switching weapon with the hotkey ? currently it looks like that

Event OnActorAction(int actionType, Actor akActor, Form source, int slot)
debug.notification("action")
if actionType == 7
if akActor == Game.GetPlayer()
game.getplayer().EquipItem(WeapOut,false,true)
endif
endif
if actionType == 10
if akActor == Game.GetPlayer()
game.getplayer().EquipItem(WeapSheathed,false,true)
endif
endif
endevent
Event OnUpdate()
debug.notification("update")
if MyOwner.IsWeaponDrawn() && MyOwner.IsEquipped(WeapSheathed)
MyOwner.EquipItem(WeapOut,false,true)
endif
if ( MyOwner.IsWeaponDrawn() == false ) && MyOwner.IsEquipped(WeapOut)
MyOwner.EquipItem(WeapSheathed,false,true)
endif
endevent
Edited by Sesoku
Link to comment
Share on other sites

It seems like the event OnActorAction isn't firing when switching with the favorites menu, or iequip, and also looks like the script using OnUpdate polling, which generally I try to avoid because it's bad for performance. A way to get around this, is to make a spell ability that has the condition IsWeaponOut == 2 on the magic effect.

 

First make a new magic effect. Archetype: Script, Casting Type: Constant Effect, Delivery: Self

Check the boxes Not Hit Event, No Hit Effect, Painless and Hide in UI.

Then Compile and add this script to the magic effect:

 

 

Scriptname MyModWeaponDrawnCheckScript extends ActiveMagicEffect 


Weapon Property WeapOut Auto 
Weapon Property WeapSheathed Auto 


Event OnEffectStart(Actor akTarget, Actor akCaster) ;Effect starts when actors weapon is out 
    If akTarget.IsEquipped(WeapSheathed)
        akTarget.EquipItem(WeapOut,false,true)
    Endif 
EndEvent 


Event OnEffectFinish(Actor akTarget, Actor akCaster) ;Effect stops when actor sheaths weapon
    If akTarget.IsEquipped(WeapOut)
        akTarget.EquipItem(WeapSheathed,false,true)
    Endif 
EndEvent 

 

 

Name the script something unique

Then make a new spell. Type: Ability, Casting: Constant Effect, Delivery: Self and put the magic effect you just made on it with the condition IsWeaponOut == 2 so the effect is only active if the actors weapon is drawn.

 

Then put this script on the weapon:

 

 

Scriptname MyModWeaponScript extends ObjectReference 


Spell Property CheckWeaponDrawnSpell Auto ;The spell we just made


Event OnContainerChanged(ObjectReference akNewContainer, ObjectReference akOldContainer)
    If akOldContainer as Actor ;if removed from an actor 
        (akOldContainer as Actor).RemoveSpell(CheckWeaponDrawnSpell)
    Endif
    
    If akNewContainer as Actor ;if weapon is added to an actor 
        (akNewContainer as Actor).AddSpell(CheckWeaponDrawnSpell, false) ;add weapon drawn check spell to actor silently 
    Endif 
EndEvent 

 

 

 

The script will add the check weapon drawn spell to the actor whenever it's added to an actors inventory.

Edited by dylbill
Link to comment
Share on other sites

Without testing anything here and just taken a blind stab at it. Considering you do have it working in one case ... I'd say the "problem" is to do with the actionType calls.

Yeah it seems the actiontype is not recognized by the hotkeys or it is but only sometimes, don't really know why

 

 

It seems like the event OnActorAction isn't firing when switching with the favorites menu, or iequip, and also looks like the script using OnUpdate polling, which generally I try to avoid because it's bad for performance. A way to get around this, is to make a spell ability that has the condition IsWeaponOut == 2 on the magic effect.

 

First make a new magic effect. Archetype: Script, Casting Type: Constant Effect, Delivery: Self

Check the boxes Not Hit Event, No Hit Effect, Painless and Hide in UI.

Then Compile and add this script to the magic effect:

 

 

Scriptname MyModWeaponDrawnCheckScript extends ActiveMagicEffect 


Weapon Property WeapOut Auto 
Weapon Property WeapSheathed Auto 


Event OnEffectStart(Actor akTarget, Actor akCaster) ;Effect starts when actors weapon is out 
    If akTarget.IsEquipped(WeapSheathed)
        akTarget.EquipItem(WeapOut,false,true)
    Endif 
EndEvent 


Event OnEffectFinish(Actor akTarget, Actor akCaster) ;Effect stops when actor sheaths weapon
    If akTarget.IsEquipped(WeapOut)
        akTarget.EquipItem(WeapSheathed,false,true)
    Endif 
EndEvent 

 

 

Name the script something unique

Then make a new spell. Type: Ability, Casting: Constant Effect, Delivery: Self and put the magic effect you just made on it with the condition IsWeaponOut == 2 so the effect is only active if the actors weapon is drawn.

 

Then put this script on the weapon:

 

 

Scriptname MyModWeaponScript extends ObjectReference 


Spell Property CheckWeaponDrawnSpell Auto ;The spell we just made


Event OnContainerChanged(ObjectReference akNewContainer, ObjectReference akOldContainer)
    If akOldContainer as Actor ;if removed from an actor 
        (akOldContainer as Actor).RemoveSpell(CheckWeaponDrawnSpell)
    Endif
    
    If akNewContainer as Actor ;if weapon is added to an actor 
        (akNewContainer as Actor).AddSpell(CheckWeaponDrawnSpell, false) ;add weapon drawn check spell to actor silently 
    Endif 
EndEvent 

 

 

 

The script will add the check weapon drawn spell to the actor whenever it's added to an actors inventory.

 

Sorry I haven't posted the full script, would that still work if the weapout and weapsheathed properties are both armor and not weapon ?

Also do the spell needs to be created through creation kit ?

 

 

Scriptname HDTWeapon_Draw_n_Sheathe extends activemagiceffect
; Unsheathed version
Armor Property WeapOut Auto
; Sheathed version
Armor Property WeapSheathed Auto
Actor property MyOwner Auto
Event OnEffectStart(Actor akTarget, Actor akCaster)
if akTarget == game.getplayer()
; this event is supposed to work for player
; draw begin
RegisterForActorAction(7)
; Sheathe End
RegisterForActorAction(10)
else
; NPC equipped this weapon
MyOwner = akTarget
registerforupdate(0.5)
endif
endevent
Event OnEffectFinish(Actor akTarget, Actor akCaster)
UnRegisterforupdate()
UnRegisterForActorAction(7)
UnRegisterForActorAction(10)
endevent
; this event only works for player
Event OnActorAction(int actionType, Actor akActor, Form source, int slot)
debug.notification("action")
if actionType == 7
if akActor == Game.GetPlayer()
game.getplayer().EquipItem(WeapOut,false,true)
endif
endif
if actionType == 10
if akActor == Game.GetPlayer()
game.getplayer().EquipItem(WeapSheathed,false,true)
endif
endif
endevent
Event OnUpdate()
debug.notification("update")
if MyOwner.IsWeaponDrawn() && MyOwner.IsEquipped(WeapSheathed)
MyOwner.EquipItem(WeapOut,false,true)
endif
if ( MyOwner.IsWeaponDrawn() == false ) && MyOwner.IsEquipped(WeapOut)
MyOwner.EquipItem(WeapSheathed,false,true)
endif
endevent

also I found the same script on google, don't know if the author of the mod based his mod off this script or if he is the same person but he uses 2 armors version for his hdt sheathed and drawn weapon https://www.loverslab.com/topic/39329-wip-a-multitail-whip-as-a-weapon-with-havoc-physics/
Edited by Sesoku
Link to comment
Share on other sites

Hey, yes it should still work if they're armor properties, and yes the spells should be made in the creation kit. Also one tweak to the weapon script:

 

 

 

Scriptname MyModWeaponScript extends ObjectReference 

Spell Property CheckWeaponDrawnSpell Auto ;The spell we just made

Event OnContainerChanged(ObjectReference akNewContainer, ObjectReference akOldContainer)
    If (akOldContainer as Actor) && (akoldContainer.GetItemCount(Self.GetBaseObject()) == 0)  ;if removed from an actor
        (akOldContainer as Actor).RemoveSpell(CheckWeaponDrawnSpell)
    Endif
    
    If akNewContainer as Actor ;if weapon is added to an actor 
        (akNewContainer as Actor).AddSpell(CheckWeaponDrawnSpell, false) ;add weapon drawn check spell to actor silently 
    Endif 
EndEvent 

Link to comment
Share on other sites

 

Hey, yes it should still work if they're armor properties, and yes the spells should be made in the creation kit. Also one tweak to the weapon script:

 

 

 

Scriptname MyModWeaponScript extends ObjectReference 

Spell Property CheckWeaponDrawnSpell Auto ;The spell we just made

Event OnContainerChanged(ObjectReference akNewContainer, ObjectReference akOldContainer)
    If (akOldContainer as Actor) && (akoldContainer.GetItemCount(Self.GetBaseObject()) == 0)  ;if removed from an actor
        (akOldContainer as Actor).RemoveSpell(CheckWeaponDrawnSpell)
    Endif
    
    If akNewContainer as Actor ;if weapon is added to an actor 
        (akNewContainer as Actor).AddSpell(CheckWeaponDrawnSpell, false) ;add weapon drawn check spell to actor silently 
    Endif 
EndEvent 

 

It took me a while, but thanks to your help and your advices, I managed to make it work for hotkey, and kinda for iequip too ! Thanks a lot for your help !

Link to comment
Share on other sites

  • Recently Browsing   0 members

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