Jump to content

Photo

Can someone check my script for mistakes?

scripting

  • Please log in to reply
3 replies to this topic

#1
114529

114529

    Regular

  • Members
  • PipPip
  • 53 posts

Hello,

 

I am making a mod which adds custom flags which you can equip using the torch mechanics as a way to hold the flag.

 

To force NPC's to equip flags, I made a script for an enchanted ring. 

The magic effect for the ring states as condition that the target must have one or more of the flags in their inventory.

When that is true, the script fires. 
It checks which flag is in the npc's inventory and equips that specific flag. When the ring is removed, the npc unequips the flag.

I gave it a 3 second delay, because I have mods which allow me to trade items with non followers. The script/trademod combo didn't work without this delay. My guess is those mods run scripts of their own and I need mine to run after those.

 

So far, the script compiled and works in game.

However, this is still my first script and my first mod for Skyrim.

 

 

Could someone check my script to see if I could improve it?

 

The script goes as follows:

 

 

 

Scriptname FLAGS_BearerDesignation extends activemagiceffect  
 
light property FLAGSauto auto 
Light Property FLAGSSons  Auto 
Light Property FLAGSImperial  Auto
Light Property FLAGSWhiterun  Auto     
MagicEffect Property FLAGS_enchantment  Auto  
 
Form Flag 
 
event OnEffectStart(Actor target, Actor caster)
              If (Target.GetItemCount(FLAGSauto) >= 1)
                  Flag = FLAGSauto 
                  Debug.MessageBox("The person should now equip the normal flag")
              ElseIf (target.GetItemCount(FLAGSImperial) >= 1)
                  Flag = FLAGSImperial
                  Debug.MessageBox("The person should now equip the imperial flag")
              ElseIf (target.GetItemCount(FLAGSWhiterun) >= 1)
                  Flag = FLAGSWhiterun
                  Debug.MessageBox("The person should now equip the Whiterun flag")
              ElseIf (target.GetItemCount(FLAGSSons) >= 1)
                  Flag = FLAGSSons
                  Debug.MessageBox("The person should now equip the Stormcloak flag")
              Else
                 Debug.MessageBox("No flag detected")
              EndIf
              RegisterForSingleUpdate(3.0)
endEvent
 
Event OnUpdate()
             actor target = getTargetActor()
target.EquipItem(FLAG,true,true)
EndEvent
 
Event OnEffectFinish(Actor Target, Actor caster)
              Target.UnequipItem(FLAG)
EndEvent
 


#2
scorrp10

scorrp10

    Old hand

  • Premium Member
  • 701 posts

Ugh...  MessageBox?   Why not use Debug.Notification  instead?

 

In OnEffectStart,  I would set  'Flag = None' in the Else clause.

 

In OnUpdate, add a check:

if (Flag != None)
    target.EquipItem(FLAG,true,true)
EndIf

In OnEffectFinish, you got to be careful.    It is called on object destruction, meaning object's variables  might have already been destroyed, which includes the 'flag'.    

What you coulld do: 

Form _item = target.GetEquippedObject(0)
If(_item as Light != None)  
     target.UnEquipItem(_item, true, true)
EndIf

Which is still not ideal, but a step in right direction...



#3
114529

114529

    Regular

  • Members
  • PipPip
  • 53 posts

Thank you for taking the time to correct my script.

 

 

''In OnEffectStart,  I would set  'Flag = None' in the Else clause.

 

 

In OnUpdate, add a check:''

if (Flag != None)
    target.EquipItem(FLAG,true,true)
EndIf

This is to make the script only equip a flag when there is a flag in the inventory, right? I have already placed conditions in the magic effect itself, so the entire script doesn't fire without a flag in the inventory.

Would this still be necessary?

 

 

''In OnEffectFinish, you got to be careful.    It is called on object destruction, meaning object's variables  might have already been destroyed, which includes the 'flag'.    

What you coulld do: ''

Form _item = target.GetEquippedObject(0)
If(_item as Light != None)  
     target.UnEquipItem(_item, true, true)
EndIf

Got it, thanks. Why would this not be ideal though?



#4
scorrp10

scorrp10

    Old hand

  • Premium Member
  • 701 posts

Because, if effect starts on an actor who has no flag, and then, when it ends, they are actually holding a torch, it will unequip the torch... 







Also tagged with one or more of these keywords: scripting

IPB skins by Skinbox
Page loaded in: 0.560 seconds