Skip to content

How to count all resrefs in a numbered list

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

Comments

  • ProlericProleric Member Posts: 1,268
    Given that you need to create the resrefs manually, ensuring there are no gaps, editing the number is surely no big deal? Best defined as a const int at the top of the script, so it's easy to find.

    If you really needed to compute it, you could CreateItemInObject in an inaccessible container, looping from 001 onwards until it returns OBJECT_INVALID.
  • FreshLemonBunFreshLemonBun Member Posts: 909
    Precomputing values where you can is always the best option.

    Even if you did loop through all entries to get the total you would just save the result as a local variable so you don't have to keep doing it every time loot is spawned.
  • ValeriyaValeriya Member Posts: 55
    edited January 2019
    I think I'll be doing it manually then, sounds easier and it's not that big of a problem come to think of it. Thanks
  • SherincallSherincall Member Posts: 387
    I think it's better to compute it all once as the module loads, rather than having an extra manual step. Proleric's idea is good, but you don't need to loop through all of them, you can only try a couple of ones to quickly find where the limit is. Here's a sample code:
    // 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>}

    (untested, and also I'm not sure how it will fare if there are no items at all)
  • antirelicantirelic Member Posts: 20
    edited February 2019
    Can't you just use the bioware tokenizer? Just keep appending new "poor" objects to a string, with a | separating the entries. The tokenizer functions will handle counting the string, and allowing you to pull resrefs by token number.

    To go a step further, Trigger Based Spawn System (TBSP) uses this approach quite nicely.
Sign In or Register to comment.