Jump to content

Add Spells from Array?


xcafe

Recommended Posts

So I'm trying to make a mod that stores the player's spells in a spellbook. In theory, it would add all the spells to the player when the player equips it, and I've got a script set up that adds the spells the player learns to an array. Is there a way to have a script run through the array and add all the spells in it to the player? Please and thank you :D

Link to comment
Share on other sites

If it is possible that the player might exceed knowing 128 spells, you'll want to look at using a formlist instead of an array.

 

Anyway, here is a brief example of how one might run through an array and add any spell in it to the player. This assumes the array is called MyArray and that the player is stored in a variable called PlayerRef.

Int ArraySize = MyArray.Length
Int Index = 0
While Index < ArraySize
  Form Entry = MyArray[Index]
  If (Entry as Spell)
    PlayerRef.AddSpell(Entry as Spell)
  EndIf
  Index += 1
EndWhile

If you are using two different scripts, you'll definitely want to look into a formlist rather than an array. Arrays are typically only viable on the script that they are defined on. Whereas formlists are records in the ESP which can be accessed by any script if need be.

Link to comment
Share on other sites

Thank you! But aren't form lists persistent? If I make a new save game I don't want to equip the spellbook item and have all the other character's spells added. And since the array I'm using is an auto property, I think I can access it from another script, right? using the "[scriptref] property [propertyname] auto" format?

Link to comment
Share on other sites

A formlist might become persistent within a given game but that is not the same thing as being persistent across different characters. At the start of each character's game, the formlist would be the same as that which you defined in the CK. Anything done to the formlist during a particular game session will not be reflected in any other character's game.

 

Formlists and arrays are basically the same thing in function. Some differences are:

1. arrays can be made up of more types than a formlist. i.e. you can have an array of Ints while you cannot have such a formlist.

2. formlists can exceed 128 entries, arrays cannot

3. arrays are limited to the type that it is defined as, formlists can contain different types at the same time.

 

I pretty much use formlists whenever possible and arrays whenever a formlist will not work. In other words, arrays for INTS, FLOATS, STRINGS and formlists for any object type or form found in the game (i.e. weapon, armor, miscobject, etc) All I am saying is, keep your options open and not to be afraid to switch if you need to.

 

As far as your idea of accessing the array across scripts, it might work. You would have to test it.

Link to comment
Share on other sites

Well I'd definitely be open to using a formlist if you'd happen to know how to add all the spells out of one of those! In any case, the script won't compile, the property I made is wrong but idk how to ix it :(

Actor Property Playerref
Spelltomereadscript Property SpellsToAdd Auto


Function OnEquipped(Actor akActor)
Int ArraySize = SpellsToAdd.Length
Int Index = 0
While Index < ArraySize
  Form Entry = SpellsToAdd[Index]
  If (Entry as Spell)
    PlayerRef.AddSpell(Entry as Spell)
  EndIf
  Index += 1
EndWhile
EndFunction
Link to comment
Share on other sites

  • Recently Browsing   0 members

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