How to count all resrefs in a numbered list
Valeriya
Member Posts: 57
So say I have a list of random treasure using resrefs with:
Poor_Item001
Poor_Item002
Up until Poor_Item250
And I keep adding to that list day by day. The problem is that I also a script of random treasure from 001 to the last of the poor item I create, which means I need to be constantly updating the number in the random treasure script that gives you a random item between 1 to the last number; is there a way to count all the resrefs with "Poor_Item" so I won't have to change that last number manually every time?
I hope my problem is understood
-Val
Poor_Item001
Poor_Item002
Up until Poor_Item250
And I keep adding to that list day by day. The problem is that I also a script of random treasure from 001 to the last of the poor item I create, which means I need to be constantly updating the number in the random treasure script that gives you a random item between 1 to the last number; is there a way to count all the resrefs with "Poor_Item" so I won't have to change that last number manually every time?
I hope my problem is understood
-Val
0
Comments
If you really needed to compute it, you could CreateItemInObject in an inaccessible container, looping from 001 onwards until it returns OBJECT_INVALID.
// utlity to convert to string with leading zeros if necessary<br>string _intToString3(int n)<br>{<br> string s = IntToString(n);<br> if (n < 10) s = "00"+s;<br> else if (n < 100) s = "0"+s;<br> return s;<br>}<br>// Gives the highest existing resref index of form sBaseResRef<index><br>// e.g. if sBaseResRef="poor_item", it'll search resrefs poor_item001 to poor_item999<br>int GetMaxResRefIndex(string sBaseResRef)<br>{<br> // Cache the result as a local variable on the mod.<br> object oMod = GetModule();<br> string sVar = "RESREF_MAX_INDEX_"+sBaseResRef;<br> int nMaxIndex = GetLocalInt(oMod, sVar);<br><br><br> if (nMaxIndex == 0) // Not set, first call<br> {<br> location l = GetStartingLocation();<br> int low = 1, high = 999; // highest and lowest possible indices<br> do<br> {<br> int mid = (low+high)/2;<br> object oItem = CreateObject(OBJECT_TYPE_ITEM, sBaseResRef + _intToString3(mid), l);<br> if (oItem == OBJECT_INVALID)<br> {<br> // We shot too high, cut interval in half<br> high = mid;<br> }<br> else<br> {<br> // Item at index mid exists, try higher<br> low = mid;<br> DestroyObject(oItem);<br> }<br> } while (high > (low+1));<br><br> nMaxIndex = low;<br> SetLocalInt(oMod, sVar, nMaxIndex);<br> }<br><br> return nMaxIndex;<br>}To go a step further, Trigger Based Spawn System (TBSP) uses this approach quite nicely.