Jump to content

Limiting Code a Specific Item?


WeAreBaked

Recommended Posts

-- Our global TableUIDestroyItems = {};----- This will create a new context menu entry.-- @param _player - The player who clicked the inventory.-- @param _context - The context menu to which we add our option.-- @param _items - The items which have been clicked.--function UIDestroyItems.createMenu(_player, _context, _items)    local player = getSpecificPlayer(_player);    local clickedItems = _items;    -- Will store the clicked stuff.    local item;    local stack;    -- stop function if player has selected multiple item stacks    if #clickedItems > 1 then        return;    end        -- Iterate through all clicked items    for _, entry in ipairs(clickedItems) do        -- test if we have a single item        if instanceof(entry, "InventoryItem") then            item = entry; -- store in local variable            break;        elseif type(entry) == "table" then            stack = entry;            break;        end    end    -- Adds context menu entry for single items.    if item then        _context:addOption("Destroy Item", clickedItems,            UIDestroyItems.onDestroyItem, player, item);    end    -- Adds context menu entries for multiple bags.    if stack then        -- We start to iterate at the second index to jump over the dummy        -- item that is contained in the item-table.        for i = 2, #stack.items do            local item = stack.items[i];            if instanceof(item, "InventoryItem") then                _context:addOption("Destroy Item", clickedItems,                    UIDestroyItems.onDestroyItem, player, item);            end        end    endend----- Remove the clicked item from the inventory.-- @param _items-- @param _player-- @param _item--function UIDestroyItems.onDestroyItem(_items, _player, _item)    _item:getContainer():Remove(_item);    _player:Say("I love pz-mods.net");end-- Call our function when the event is firedEvents.OnPreFillInventoryObjectContextMenu.Add(UIDestroyItems.createMenu);

this is from the site tutorial and it works fine but how would i limit this to specific items...i only want my new menu to show for certain stuff

 

Link to comment
Share on other sites

Hm this is one of my old tutorials, afaik it isn't working 100% correctly anymore. Back when I wrote it there was a "dummy item" in the table for clicked items...

 

This should fix it (not sure though):

    -- Adds context menu entries for multiple bags.    if stack then        -- We start to iterate at the second index to jump over the dummy        -- item that is contained in the item-table.        for i = 1, #stack.items do            local item = stack.items[i];            if instanceof(item, "InventoryItem") then                _context:addOption("Destroy Item", clickedItems,                    UIDestroyItems.onDestroyItem, player, item);            end        end    end
Also if you only want options for a certain item you will have to do something like this (pseudo):

if instanceof(item, "InventoryItem") and item:functionThatReturnsItemName() == 'yourAwesomeItem' then    _context:addOption("Do stuff", clickedItems, doStuffFunction(), player, item);end
Link to comment
Share on other sites

i was just browsing in the old unpack bags mod and something like this wouldnt work?

-- Our global TableUIDestroyItems = {};----- This will create a new context menu entry.-- @param _player - The player who clicked the inventory.-- @param _context - The context menu to which we add our option.-- @param _items - The items which have been clicked.--function UIDestroyItems.createMenu(_player, _context, _items)    local player = getSpecificPlayer(_player);    local clickedItems = _items;    -- Will store the clicked stuff.    local item;    local stack;    -- stop function if player has selected multiple item stacks    if #clickedItems > 1 then        return;    end        -- Iterate through all clicked items    for _, entry in ipairs(clickedItems) do        -- test if we have a single item        if instanceof(entry, "InventoryItem") and instanceof(item, "Base.Lighter") thenthen            item = entry; -- store in local variable            break;        elseif type(entry) == "table" then            stack = entry;            break;        end    end    -- Adds context menu entry for single items.    if item then        _context:addOption("Destroy Item", clickedItems,            UIDestroyItems.onDestroyItem, player, item);    end    -- Adds context menu entries for multiple bags.    if stack then        -- We start to iterate at the second index to jump over the dummy        -- item that is contained in the item-table.        for i = 2, #stack.items do            local item = stack.items[i];            if instanceof(item, "InventoryItem") and instanceof(item, "Base.Lighter") then                _context:addOption("Destroy Item", clickedItems,                    UIDestroyItems.onDestroyItem, player, item);            end        end    endend----- Remove the clicked item from the inventory.-- @param _items-- @param _player-- @param _item--function UIDestroyItems.onDestroyItem(_items, _player, _item)    _item:getContainer():Remove(_item);    _player:Say("I love pz-mods.net");end-- Call our function when the event is firedEvents.OnPreFillInventoryObjectContextMenu.Add(UIDestroyItems.createMenu);

btw i added

and instanceof(item, "Base.Lighter")
Link to comment
Share on other sites

This shouldn't work since instanceof checks if an object is of a certain type. For example all items are of type "InventoryItem" and all bags are "InventoryContainers".

 

I just remembered, that I have written a inventory context menu for my (unreleased) story mod ... it features a better code and might be closer to what you are trying to do:

 

-- =============================================================================-- Muldraugh Tales-- by RoboMat---- Created: 04.11.13 - 18:58-- =============================================================================require'TimedActions/ISTimedActionQueue';require'StoryHandling/StoryLoader';require'UI/UIStoryManager';require'Util/LibString';-- -------------------------------------------------- Local Functions-- ------------------------------------------------local function onReadNote(_items, _player, _story)    local story = _story;    local panel = UIStoryPanel:new(story.tags['<title>'], story.content);    panel:initialise();    panel:addToUIManager();end----- @param _player - The player who clicked the menu.-- @param _context - The context menu to add a new option to.-- @param _items - A table containing the clicked items / stack.--local function createMenu(_player, _context, _items)    local stories = StoryLoader.getStories();    local itemTable = _items; -- The table containing the clicked items.    local context = _context;    local player = getSpecificPlayer(_player);    if #itemTable == 1 then -- We have either one clicked item or a folded stack of items.        -- We iterate through the table of clicked items. We have        -- to seperate between single items and folded        for _, clickedItem in ipairs(itemTable) do            if instanceof(clickedItem, "InventoryItem") then -- We have a single clicked item.                if clickedItem:getModule() == "MuldraughTales" then                    local modData = clickedItem:getModData();                    local id = modData.id;                    local story = stories[id];                    context:addOption("Read " .. story.tags['<title>'], itemTable, onReadNote, player, story);                end            elseif type(clickedItem) == "table" then -- We have a folded stack of items.                -- We start to iterate at the second index to jump over the dummy                -- item that is contained in the item-table.                for i2 = 2, #clickedItem.items do                    local item = clickedItem.items[i2];                    if instanceof(item, "InventoryItem") then                        if item:getModule() == "MuldraughTales" then                            local modData = item:getModData();                            local id = modData.id;                            local story = stories[id];                            context:addOption("Read " .. story.tags['<title>'], itemTable, onReadNote, player, story);                        end                    end                end            end        end    elseif #itemTable > 1 then -- We have an unfolded stack or multiple stacks.        for i, clickedItem in ipairs(itemTable) do            if type(clickedItem) == "table" then -- Multiple Folded stacks.                for i2 = 2, #clickedItem.items do                    local item = clickedItem.items[i2];                    if instanceof(item, "InventoryItem") then                        if item:getModule() == "MuldraughTales" then                            local modData = item:getModData();                            local id = modData.id;                            local story = stories[id];                            context:addOption("Read " .. story.tags['<title>'], itemTable, onReadNote, player, story);                        end                    end                end            elseif i > 1 and instanceof(clickedItem, "InventoryItem") then -- Unfolded stack.                if clickedItem:getModule() == "MuldraughTales" then                    local modData = clickedItem:getModData();                    local id = modData.id;                    local story = stories[id];                    context:addOption("Read " .. story.tags['<title>'], itemTable, onReadNote, player, story);                end            end        end    endend-- -------------------------------------------------- Game Hooks-- ------------------------------------------------Events.OnPreFillInventoryObjectContextMenu.Add(createMenu);

 

See the line:

 

  if clickedItem:getModule() == "MuldraughTales" then

 

This ignores the name of the item, but instead just checks for the module it belongs to. So if you want to have a certain context menu which appears only for all of the items of your mod, put them in their own module and check for the name of that.


EDIT: ... Can't believe that it's already almost one year since I've last written some code for PZ :D

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...