• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Get list of item by item id, then get item uid of items in list

swashed

Member
Joined
Jul 9, 2023
Messages
75
Reaction score
8
The only function I seem to currently have access to is getPlayerItemCount which returns only a number. I am not sure how I can reference these items individually.

Lua:
getPlayerItemCount(cid, item_id)

Context:

I have an auto-runemaking script that doesn't require the blank rune to be in your hand. So long as you have 1 blank rune in your inventory, it will consume the rune and give you the new rune. The problem is that the new rune is given to the player, which is placed in their main container.

I am trying to reference the blank rune's uid so I can call doPlayerTransformItem, which require the item's uid.
Post automatically merged:

runes1.gif
 
Solution
Untested.. but try this.

It will attempt to find the first item on the player that matches the itemId you chose.

I'm uncertain if you need to return item or item.uid, so play around with it a bit?

Lua:
local blank = findItemOnPlayer(cid, itemId)
if blank == 0 then
    -- unable to find a blank rune?
    return false
end
doTransformItem(blank, newItemId)
Lua:
function findItemOnPlayer(cid, itemId)
    local temp = {}
   
    for slot = 1, 10 do
        local slotItem = getPlayerSlotItem(cid, slot)
        if slotItem.uid > 0 then
            if slotItem.itemid == itemId then
                return slotItem.uid
            end
            if isContainer(slotItem.uid) then
                table.insert(temp, slotItem.uid)...
The only function I seem to currently have access to is getPlayerItemCount which returns only a number. I am not sure how I can reference these items individually.

Lua:
getPlayerItemCount(cid, item_id)

Context:

I have an auto-runemaking script that doesn't require the blank rune to be in your hand. So long as you have 1 blank rune in your inventory, it will consume the rune and give you the new rune. The problem is that the new rune is given to the player, which is placed in their main container.

I am trying to reference the blank rune's uid so I can call doPlayerTransformItem, which require the item's uid.
Post automatically merged:

View attachment 84485

You say that you only have access to the function getPlayerItemCount(), may I ask why this is the case? Or is it possible there are other functions/methods you are unaware of? It would be handy to know which server you are using, and in what function/callback this script will be executed it.

Lua:
local item = player:getItemById(id)
if item then
    item:transform(newId)
end
 
I'm using OTHIRE which is quite old but I'm making it work. Here is a list of all the functions that are in this version.


There is a transform function but it requires referencing the item's uid, which I have no way of grabbing that I know of...

Lua:
int LuaScriptInterface::luaDoTransformItem(lua_State *L)
{
    //doTransformItem(uid, toitemid, <optional> count/subtype)
    int32_t parameters = lua_gettop(L);

    int32_t count = -1;
    if(parameters > 2){
        count = popNumber(L);
    }

doTransformItem(item.uid, newID)

or

doTransformItem(blank rune found in inventory, sd rune)
 
Untested.. but try this.

It will attempt to find the first item on the player that matches the itemId you chose.

I'm uncertain if you need to return item or item.uid, so play around with it a bit?

Lua:
local blank = findItemOnPlayer(cid, itemId)
if blank == 0 then
    -- unable to find a blank rune?
    return false
end
doTransformItem(blank, newItemId)
Lua:
function findItemOnPlayer(cid, itemId)
    local temp = {}
   
    for slot = 1, 10 do
        local slotItem = getPlayerSlotItem(cid, slot)
        if slotItem.uid > 0 then
            if slotItem.itemid == itemId then
                return slotItem.uid
            end
            if isContainer(slotItem.uid) then
                table.insert(temp, slotItem.uid)
            end
        end
    end
   
    while #temp > 0 do
        for i = 0, getContainerSize(temp[1]) - 1 do
            local item = getContainerItem(temp[1], i)
            if item.uid > 0 then
                if item.itemid == itemId then
                    return item.uid
                end
                if isContainer(item.uid) then
                    table.insert(temp, item.uid)
                end
            end
        end
        table.remove(temp, 1)
    end
   
    return 0
end
 
Last edited:
Solution
You are a magician, my friend. A very talented scripter.

90% of it worked but I just had to specify uid in some parts and it worked :)

Lua:
local function findItemOnPlayer(cid, itemId)
    local temp, containers, slots = {}, {}, 0
    
    for slot = 1, 10 do
        local slotItem = getPlayerSlotItem(cid, slot)
        if slotItem.uid > 0 then
            if isContainer(slotItem.uid) then
                print('found container') -- working
                table.insert(temp, slotItem.uid)
            elseif slotItem.itemid == itemId then
                return slotItem.uid -- working
            end
        end
    end
    
    while #temp > 0 do
        for i = 0, getContainerSize(temp[1]) - 1 do
            local item = getContainerItem(temp[1], i)
            if item.uid > 0 and item.itemid == itemId then
                return item.uid
            end
            if isContainer(item.uid) then
                table.insert(temp, item.uid)
                table.insert(containers, item.uid)
            end
        end
        table.remove(temp, 1)
    end
    
    return 0
end

function onSay(cid, words, param)
    local blank = findItemOnPlayer(cid, 2260)
    print(blank)
    if blank == 0 then
        print('unable to find a blank rune')
        -- unable to find a blank rune?
        return false
    end
    print('transform blank pls')
    doTransformItem(blank, 2268)
end

runes2.gif
 
Back
Top Bottom