ScarabMonkey Posted February 24, 2012 Share Posted February 24, 2012 I've been trying to implement a container that triggers things based on what's in it - I wanted to record the ObjectReference of each item stored in there by putting this information into an array. Then I hit scope issues, so I tried to make my array a Property... but this seems to assume it has length 0. Does anyone know how to assign an Array Property with a length - or to modify its length? I currently get a "can't do anyhting with a None" (or somesuch wording) These are the relevant parts of my script: Keyword[] Property ChestItems Auto ObjectReference[] Property ChestItemReferences Auto Hidden int currentItem = 0 while (currentItem < ChestItems.Length) ChestItemReferences[currentItem] = GetLinkedRef(ChestItems[currentItem]) currentItem += 1 endWhile Link to comment Share on other sites More sharing options...
Astymma Posted February 24, 2012 Share Posted February 24, 2012 At some point you'll need to initialize your property array with something like the following: ChestItems = new Keyword[10] ChestItemReferences = new ObjectReference[10] note: 10 is just an example of the initialization size Without some initialization, the length will be zero and you have an empty array. Arrays aren't dynamic so you'll have a fixed size... though at any time you can copy the data into a temp array with a size greater or smaller, add or subtract elements, then reassign the property with the new values. An example would be this: ;define two arrays and a counter int[] iArray int[] iTmpArray int iElement ;initialize main array iArray = new int[5] ;fill array with values iArray[0] = 0 iArray[1] = 1 iArray[2] = 2 iArray[3] = 3 iArray[4] = 4 ;create temp array 1 size larger iTmpArray = new int[6] ;fill tmp array with values from original array iElement = 0 while (iElement < iArray.length) iTmpArray[iElement] = iArray[iElement] iElement += 1 endWhile ;add our new value at the end iTmpArray[5] = 5 ;reassign a new array to our original array with the new size iArray = new int[iTmpArray.length] ;fill it with the values from tmp array iElement = 0 while (iElement < iTmpArray.length) iArray[iElement] = iTmpArray[iElement] iElement += 1 endWhile ;clear the tmp array by making it an empty array iTmpArray = new int[] Note I am copying the array elements, not just saying something like: iArray = iTmpArray That would assign the variables to point at the same array and the next time you cleared iTmpArray to add a new element, you'd erase everything in both. I'd suggest implementing property Get and Set functions to do this or create your own management functions that handle resizing of the array. Note thatthe keyword "new" needs to be in a function... the above code won't compile, it's just a sample of the technique. Link to comment Share on other sites More sharing options...
ScarabMonkey Posted February 24, 2012 Author Share Posted February 24, 2012 Ooh, thanks will give that a try! Edit:Can't seem to make that work - it just refuses to compile with a variable in an array declaration:" mismatched input 'iArrayLength' expecting INTEGER" Link to comment Share on other sites More sharing options...
Astymma Posted February 25, 2012 Share Posted February 25, 2012 Ooh, thanks will give that a try! Edit:Can't seem to make that work - it just refuses to compile with a variable in an array declaration:" mismatched input 'iArrayLength' expecting INTEGER" Oh crap, I was afraid of that... You'll have to estimate your upper bounds then and provide array bounds checking to make sure it isn't exceeded. Can also grow it by fixed amounts, like so (in size=10 increments up to size=50): int iLength = iTmpArray.Length if iLength == 11 iArray = New Int[20] elseif iLength == 21 iArray = New Int[30] elseif iLength == 31 iArray = New Int[40] elseif iLength == 41 iArray = New Int[50] elseif iLength > 50 ;we have exceeded our upper bounds, give an error and return or do some sort of first in first out logic (FIFO) ; FIFO could be accomplished by copying the 50 elements in iTmpArray starting at index 1 instead of 0, that will ;discard the oldest entry in favor of the newest entry endif ;copy iTmpArray into iArray with newly resized iArray Link to comment Share on other sites More sharing options...
Recommended Posts