Difference between revisions of "Talk:Add - Array"

From the Fallout4 CreationKit Wiki
Jump to navigation Jump to search
imported>LAITH10
(Created page with "==Not working on empty properties== I had been writing the following script: I have an array property of structs, that should be filled with new elements by a function. The ar...")
 
imported>LAITH10
m
 
(4 intermediate revisions by 3 users not shown)
Line 4: Line 4:
But when trying to add a new element on this property, it simply does nothing (array is still empty). Using the same script with no modifications, but filling the array with a "dummy" element via CK suddenly works just fine.
But when trying to add a new element on this property, it simply does nothing (array is still empty). Using the same script with no modifications, but filling the array with a "dummy" element via CK suddenly works just fine.
I haven't tested it on non-property arrays thought.
I haven't tested it on non-property arrays thought.
After some further testing I found the issue: If you have a property array, you have to make sure that it is either assigned a value via CK, or initialise it somewhere in the code (preferably in OnInit()). If you do not initialise the array via CK with at least one element, the array will be none and thus no functions such as Add() will work.
I had assumed that CK would do that automatically, given that you cannot use the new command during a declaration of a property. Thus I highly suggest to check the array and initialize it if needed somewhere in the code such as:
<source lang="papyrus">
event OnInit()
  if !MyArray
    MyArray= New MyArrayType[0]
  endif
endevent
</source>
This makes sure that the array is initialized, but still without any elements. Now the array is working as intended.
Likewise, the same issue appears with non-property arrays if you did not initialize it before.
I would recommend to put this information somewhere on the array articles, as this can lead to some serious problems. It got me a good headache just to find that issue. [[User:Shirakami|Shirakami]] ([[User talk:Shirakami|talk]]) 2016-09-25T09:20:53 (EDT)
All you need to do is create a new array with a zero size. The zero size lets the array expand its length when used with the Array.Add and Array.Remove functions. There is no need to check if an array is none before setting its value.
<source lang="papyrus">
event OnInit()
    MyArray= New MyArrayType[0]
    MyArray.Add(someValue)
endevent
</source>
--[[User:Scrivener07|Scrivener07]] ([[User talk:Scrivener07|talk]]) 2016-09-26T18:41:39 (EDT)
That way it will overwrite any elements that had been set via creation kit. If you do a check first, it will not overwrite the changes made in CK, but only initializes it if it is empty.[[User:Shirakami|Shirakami]] ([[User talk:Shirakami|talk]]) 2016-09-26T18:44:14 (EDT)

Latest revision as of 17:44, 26 September 2016

Not working on empty properties[edit source]

I had been writing the following script: I have an array property of structs, that should be filled with new elements by a function. The array initially is empty (no value has been set in CK for it), as it should be done solely per scripts. But when trying to add a new element on this property, it simply does nothing (array is still empty). Using the same script with no modifications, but filling the array with a "dummy" element via CK suddenly works just fine. I haven't tested it on non-property arrays thought.

After some further testing I found the issue: If you have a property array, you have to make sure that it is either assigned a value via CK, or initialise it somewhere in the code (preferably in OnInit()). If you do not initialise the array via CK with at least one element, the array will be none and thus no functions such as Add() will work. I had assumed that CK would do that automatically, given that you cannot use the new command during a declaration of a property. Thus I highly suggest to check the array and initialize it if needed somewhere in the code such as:

event OnInit()
  if !MyArray
    MyArray= New MyArrayType[0]
  endif
endevent

This makes sure that the array is initialized, but still without any elements. Now the array is working as intended. Likewise, the same issue appears with non-property arrays if you did not initialize it before. I would recommend to put this information somewhere on the array articles, as this can lead to some serious problems. It got me a good headache just to find that issue. Shirakami (talk) 2016-09-25T09:20:53 (EDT)


All you need to do is create a new array with a zero size. The zero size lets the array expand its length when used with the Array.Add and Array.Remove functions. There is no need to check if an array is none before setting its value.

event OnInit()
    MyArray= New MyArrayType[0]
    MyArray.Add(someValue)
endevent

--Scrivener07 (talk) 2016-09-26T18:41:39 (EDT)


That way it will overwrite any elements that had been set via creation kit. If you do a check first, it will not overwrite the changes made in CK, but only initializes it if it is empty.Shirakami (talk) 2016-09-26T18:44:14 (EDT)