Jump to content

Dynamic Lists? and keeping a count


Tighern McDonnell
 Share

You are about to reply to a thread that has been inactive for 2040 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Ok, so it has been a couple of years since I was doing a lot of scripting, trying to relearn things and get back into it. The basic idea of what I am trying to do: a Pouch/Backpack/Chest system for role play. No prim items are passing back and forth it is all in the script. There are multiple items that can be "picked up" or created using another script. I need a way to keep the inventory quantities of the items picked up. I am just trying to relearn lists and haven't gotten it all back. I am thinking of someway to make a dynamic set of lists to be stored. when the item being picked (imagine picking corn) the item script will talk to the pouch giving the pouch script its name and then the pouch script will look to see if there is a list of that name if so add 1 to qty. if now it will add the list and add 1. I cannot seem to grasp my head around how to make this happen. thanks.

Link to comment
Share on other sites

Strided Lists sound right ... though there is the warning in the wiki:

Quote

It should be noted that when manipulating extremely large strided lists, that if you expect to be editing the lists that you may wish to use one list for each "column", this may be more complex but significantly reduces the amount of memory required when manipulating the lists, though it will be a lot more difficult to sort.

The easiest option that came to mind for me was simply having two lists, one that holds the names and the other that holds the values.

//global vars
list gNameList = [];
list gNumList = [];

add_item(string name)
{
    //find it in the name list, returns -1 if not there
    integer posit = llListFindList(gNameList, [name]);

    if (posit == -1)
    {
        //not there so simply add
        gNameList = gNameList + [name];
        gNumList = gNumList + [1];
    }

    else
    {
        //we found it so just need to increment the count

        //get current count and incr
        integer curCount = llList2Integer(gNumList, posit);
        curCount++;

        //now save it
        gNumList = llListReplaceList(gNumList, [curCount], posit, posit);
    }
}

 

Link to comment
Share on other sites

Yes,  and you can make it even simpler use a comma separated string "name", n - which you then add to a single list.

And most simple , if limited  low number of item to be picked up, just a comma separated string stand alone.

Another even more simple elegant solution is just to add the pickup unique name/ID to a list and count the unique pickup name/ID in the list to tell player: You picked up 6 corns, 3 apples etc.

Last the easiest way to handle count.

Edited by Rachel1206
Clarify
Link to comment
Share on other sites

Strided lists are great for uniform data like a fixed number of items, each with a fixed number of associated data points.

If you need more flexibility, the JSON facilities in LSL can come in pretty handy. It is really nice to be able to refer to an item by name instead of by a fixed position in a list. The downside it is it a little slower than using lists. The upside it your script can be dynamic and potentially be designed to deal with future objects.


integer count = (integer)llJsonGetValue( gInventory, ["sword", "count"] );

Essentially you are using a string, gInventory in this case, to hold a JSON data structure (a sort of simplified XML). It is a little confusing at first to wrap your head around, especially when creating/updating values, but it really has a ton more power than lists.

Edited by Phate Shepherd
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 2040 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...