- Messages
- 1,320
As a newbie Papyrus scripter here is the source of much of your initial learning and documentation: Category:Papyrus. It offers general tips on variables, functions, events, etc. There is one special part that may trip you up though: Arrays.
Programming languages have inbuilt literals, declared like this:
...or...
Similarly, with a string you can declare:
...or...
In Papyrus, arrays are different. They are not inbuilt literals (not handled natively in the code) - they are objects that you have to explicitly declare and deal with.
In many modern scripting languages you declare arrays something like this:
And it will create an empty array that can hold numbers - the second line will add the number 42 to the array.
The papyrus way is to explicitly declare the array object:
1/ As an array of numbers (or Forms, or Keywords, or ObjectReferences)
2/ With an initial length (between 0 and 128)
3/ Then add something to it
You can then use code like the following:
Etcetcetc. If you haven't declared the array properly though - you've forgotten the "= new int[0]" part - the compiler won't complain. It'll happily let you do it, and run various commands upon your nonexistent array.
Without crashing.
And leaving you pulling your hair out while you go not-so-slowly insane.
===================================================
There is one other thing about arrays: If you declared it, you can't have more than 128 items stored in it.
So you can declare an array that will hold 128 items:
And they will be held in item-slots 0-127 (looks familiar? it should - think "signed byte" - and yes, all arrays are zero-based). You cannot add to the end of the array though.
There are a couple of caveats to this:
1/ Game functions that return arrays can hold any number of items
2/ Arrays declared in the Creation Kit can hold any number of items
You can also get around the 128-item limit by doing some tricky scripting. That's out of the scope of this tutorial though. What I will show is a use for an array. (Next post.)
Programming languages have inbuilt literals, declared like this:
Code:
int iCounter
Code:
int iCounter = 0
Code:
string sWords
Code:
string sWords = "This is the message stored in the string. It is otherwise blank, or empty."
In many modern scripting languages you declare arrays something like this:
Code:
int[] iaNumbers
iaNumbers[] = 42 ; the answer to life, the universe, and everything
The papyrus way is to explicitly declare the array object:
1/ As an array of numbers (or Forms, or Keywords, or ObjectReferences)
2/ With an initial length (between 0 and 128)
3/ Then add something to it
Code:
int[] iaNumbers = new int[0]
iaNumbers.Add(42)
Code:
iaNumbers.Length ; gives you how many items are saved in the array - numbers in this case
int iAnswer = iaNumbers[0] ; stores the first item in the new integer variable
if iaNumbers.Find(42)
Debug.Notification("Yes we found the correct answer to Life, the Universe, and Everything!")
endif
Without crashing.
And leaving you pulling your hair out while you go not-so-slowly insane.
===================================================
There is one other thing about arrays: If you declared it, you can't have more than 128 items stored in it.
So you can declare an array that will hold 128 items:
Code:
int[] iaNumbers = new int[128]
There are a couple of caveats to this:
1/ Game functions that return arrays can hold any number of items
2/ Arrays declared in the Creation Kit can hold any number of items
You can also get around the 128-item limit by doing some tricky scripting. That's out of the scope of this tutorial though. What I will show is a use for an array. (Next post.)
Last edited: