Jump to content

Issues with two scripts for new mod


Recommended Posts

I'll start by mentioning I'm pretty new to scripting and have a weird way of making them (or so I've been told :confused:).The mod I'm trying to make right now only needs a few scripts, but I have some doubts on what I have so far.

 

The first script is for a misc item. It has to add (a lot of) spells to the player when the item is added to the player inventory (after you craft the item):

 

Scriptname LearnSpellsFromItemScript extends ObjectReference

Spell Property Spell1 Auto
Spell Property Spell2 Auto
Spell Property Spell3 Auto

;17 times spell property!

Event OnContainerChanged(ObjectReference akNewContainer, ObjectReference akOldContainer)
If akNewContainer as Actor
(akNewContainer as Actor).AddSpell(Spell1)
(akNewContainer as Actor).AddSpell(Spell2)
(akNewContainer as Actor).AddSpell(Spell3)

;17 spells to add! Use of formlist is possible for spells? Other way to do this?
Endif
If akOldContainer as Actor

;No effect needed!
Endif
EndEvent

 

It would probably work like this, but 17 spell properties? And it should teach the spells only once, not every time it's picked up (might become a quest item at some point).

 

The second one is a unique spell. When cast it should remove a specific weapon from the player inventory and add a different weapon:

 

Scriptname SpellSwitchesItemsScript extends ActiveMagicEffect

FormList Property MyFormList Auto
;16 weapons in formlist
Weapon Property WeaponAdded Auto

Event OnEffectStart(Actor akTarget, Actor akCaster)
int Index = MyFormList.GetSize()
Form WeaponInInventory = MyFormList.GetAt(Index)
;Missing something?
If akCaster.GetItemCount(WeaponInInventory) > 0 ;Which weapon is in inventory?
akCaster.RemoveItem(WeaponInInventory, 1) ;Remove weapon
akCaster.AddItem(WeaponAdded, 1) ;Add weapon
EndIf
EndEvent

 

I'm not sure how to continue this one. It has to check the formlist, then look if one of those weapons is in the inventory of the player and remove that weapon and finally add the specific weapon the spell was made for. No idea how to even start on the marked part...

Link to comment
Share on other sites

For both of these, you want to cycle through formlists with a while loop. You can add multiple spells easily this way:

 

Formlist Property MySpells Auto ;formlist with spells to add.

Event OnContainerChanged(ObjectReference akNewContainer, ObjectReference akOldContainer)
      If akNewContainer as Actor
          Actor akActor = akNewContainer as Actor
          
          Int Index = MySpells.GetSize() ;get how many spells are in the list
          While Index > 0 
              Index -= 1 ;subtrace 1 from index
              Spell CurrentSpell = MySpells.GetAt(Index) as spell 
              akActor.AddSpell(CurrentSpell) 
          EndWhile 
      EndIf
EndEvent

Same for the second script. It searches all the forms in the list using GetItemCount:

FormList Property MyFormList Auto ;16 weapons in formlist
Weapon Property WeaponAdded Auto

Event OnEffectStart(Actor akTarget, Actor akCaster)
    Form akForm = FindInventoryWeapon(akTarget) ;find and return weapon in list
    
    If akForm != None ;does the Target have a weapon in the MyFormList in their inventory? 
        akTarget.RemoveItem(akForm) 
        akTarget.AddItem(WeaponAdded)
    Endif
    
EndEvent

Form Function FindInventoryWeapon(Actor akActor)
    ;Finds a returns a form in the MyFormList if the actor has at least 1 in their inventory 
    
    If akActor.GetItemCount(MyFormList) == 0 ;if akActor doesn't have any of these forms in their inventory, return none. 
        Return None
    Else 
        Int Index = MyFormList.GetSize() ;get how many forms are in the list
        While Index > 0 
            Index -= 1 ;subtranct 1 from index
            Form CurrentForm = MyFormList.GetAt(Index) 
            If akActor.GetItemCount(CurrentForm) > 0 
                Return CurrentForm
            Endif 
        EndWhile 
        
        Return None
    Endif
EndFunction
Link to comment
Share on other sites

MystOfDawn wrote:

int Index = MyFormList.GetSize()
Form WeaponInInventory = MyFormList.GetAt(Index) ;Missing something?

Your code above is not working, because a formlist starts by Zero like next

form firstElement = myList.GetAt(0)

and ends with

form lastElement = myList.GetAt( myList.GetSize() - 1)

About your scripts: Its possible to use one spell only by creating a second formlist of weapons at the same length as the first list. Scripts as follow:

modLearnSpellsFromItemScript

 

Scriptname modLearnSpellsFromItemScript extends ObjectReference
; https://forums.nexusmods.com/index.php?/topic/10113148-issues-with-two-scripts-for-new-mod/

; its attached on a miscitem. It has to add a spell to the player when the item is added to the player inventory (after you craft the item).
; And it should teach the spells only once, not every time its picked up (might become a quest item at some point).

  Spell PROPERTY mySpell auto        ; only one spell keeps left

; -- EVENTs --

EVENT OnInit()
    Debug.Trace(" OnInit() - has been reached for " +self)
ENDEVENT

EVENT OnContainerChanged(ObjectReference akNewContainer, ObjectReference akOldContainer)
IF (akNewContainer == Game.GetPlayer() as ObjectReference)
    gotoState("Done")                ; ### STATE ###  spell teaching is done
    myF_Add(akNewContainer as Actor)
;;; gotoState("")                    ; ### STATE ###  we do not have to go back, teach the spell once only
ENDIF
ENDEVENT


;=======================
state Done
;=========
    EVENT OnContainerChanged(ObjectReference akNewContainer, ObjectReference akOldContainer)
    ENDEVENT
;=======
endState


; -- FUNCTION --

;-----------------------------
FUNCTION myF_Add(Actor player)
;-----------------------------
    IF player.HasSpell(mySpell)
    ELSE
        player.AddSpell(mySpell)
    ENDIF
ENDFUNCTION

 

 

modWeaponSwitchMGEFScript

 

Scriptname modWeaponSwitchMGEFScript extends ActiveMagicEffect
; https://forums.nexusmods.com/index.php?/topic/10113148-issues-with-two-scripts-for-new-mod/

; The second one is for a unique spell.
; When casted, it have to remove a specific weapon from the player inventory and add a different weapon.

  FormList PROPERTY remList auto    ; 16 specific weapons inside new created formlist, to replace
  FormList PROPERTY addList auto    ; 16 different weapons .., to add


; -- EVENT --

EVENT OnEffectStart(Actor akTarget, Actor akCaster)
IF (akCaster == Game.GetPlayer())
ELSE
    RETURN    ; - STOP -    safety first, not player casted
ENDIF
;---------------------
    myF_Action(akCaster as Actor)
ENDEVENT


; -- FUNCTION --

;--------------------------------
FUNCTION myF_Action(Actor player)
;--------------------------------
IF player.GetItemCount(remList) < 1)
    RETURN    ; - STOP -    weapon from formlist is not in players inventory or currently equipped
ENDIF
;---------------------
int i = remList.GetSize()
    WHILE (i)                    ; (i != 0)
        i = i - 1    
        form fm = remList.GetAt(i)
        IF ( fm )
            IF (player.GetItemCount(fm) > 0)
                player.RemoveItem(fm, 1)
                player.AddItem(addList.GetAt(i), 1)
                i = 0                                ; break the loop
            ENDIF
        ENDIF
    ENDWHILE
ENDFUNCTION

;--------------------------------------
FUNCTION myF_RandomAction(Actor player)
;--------------------------------------
IF player.GetItemCount(remList) < 1)
    RETURN    ; - STOP -    weapon from formlist is not in players inventory or currently equipped
ENDIF
;---------------------
    int[] a = Int[128]           ; create temp array with maximum length
    int   n                      ; Zero by default

form fm
int i = remList.GetSize()
    WHILE (i)                    ; (i != 0)
        i = i - 1    
        fm = remList.GetAt(i)
        IF ( fm )
            IF (player.GetItemCount(fm) > 0)
                a[n] = i         ; fill temp array with position of valid weapon in formlist to remove
                n = n + 1        ; increase array counter
            ENDIF
        ENDIF
    ENDWHILE

    n = n - 1                    ; adjust counter

    IF (n == 0)
    ELSE
        n = Utility.RandomInt(0, n)  ; take a random item contained in formlist, which player has
    ENDIF

    i = a[n]

    player.RemoveItem(remList.GetAt(i), 1)
    player.AddItem(addList.GetAt(i), 1)
ENDFUNCTION

 


Edited by ReDragon2013
Link to comment
Share on other sites

Thank you for helping me out. I didn't even consider half of what you both suggested!

Seems like I underestimated what was needed for what I was trying to do :sweat:

I tend to work on mods with others, but they're usually the ones writing scripts (I mostly do the models and CK stuff).

If this mod idea works out, I'll definitely mention your help in the credits :happy:

Link to comment
Share on other sites

  • Recently Browsing   0 members

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