Load and use attachments from inventory

From libopenmetaverse - libomv - Developer Wiki

Jump to: navigation, search

Prerequisites

The following prerequisites are required in order for you to create your first bot by following this tutorial.

Real World

  • This tutorial assumes that you were able to successfully build libsecondlife! If you have not compiled libsl, please follow the instructions on the Getting Started wikipage.
  • Also, if you have no experience with the C# (c-sharp) programming language, I highly recommend that you stop now and please follow the list of links to csharp tutorials located here.
  • Placing the Torch! from the library into your Objects folder for your bot. You should do this manually before using this code, as it's needed to attach.
  • Your using version 0.9.0.0 (as shown on the DLL) of libsecondlife.dll

The Code

Note: We are assuming that our main "SecondLife" variable is named "client." If you have a different name for this variable, please substitute the name accordingly

 
            //initialize our list to store the folder contents
            LLUUID inventoryItems;
 
            //make a string array to put our folder names in.
            String[] SearchFolders = { "" };
 
            //Next we grab a full copy of the entire inventory and get it stored into the Inventory Manager
            client.Inventory.RequestFolderContents(client.Inventory.Store.RootFolder.UUID, client.Self.AgentID, true, true, InventorySortOrder.ByDate);
            
            //Next we want to step through the directory structure until we get to the item.
            SearchFolders[0] = "Objects";
 
            //Now we can grab the details of that folder and store it to our list.
            inventoryItems = client.Inventory.FindObjectByPath(client.Inventory.Store.RootFolder.UUID, client.Self.AgentID, SearchFolders[0], 1000);
 
            //now that we have the details of the objects folder, we need to grab the details of our torch.
            SearchFolders[0] = "Torch!";
            inventoryItems = client.Inventory.FindObjectByPath(inventoryItems, client.Self.AgentID, SearchFolders[0], 1000);
 
            InventoryItem myitem;
 
            // Convert the LLUUID to an inventory item
            myitem = client.Inventory.FetchItem(inventoryItems, client.Self.AgentID,1000);
 
            //finally we attach the object to it's default position
            try
            {
                // Catch any errors that may occur (not having the "Torch!" item in your inventory for example)
                client.Appearance.Attach(myitem as InventoryItem, libsecondlife.AttachmentPoint.Default);
            }
            catch
            {
                // Put any code that handles any errors :)
            }