local id = creature:getId()
print(id:getName())
not work for me. how to get player object by id? tfs 1.2
local id = creature:getId()
print(id:getName())
it's just variable assignment. I want to use the player's ID to access other data such as name and position. in addevent you can't pass the entire player object and that's why you need to get to it by id
local function doSomething(playerId)
local player = Player(playerId)
if not player then
return
end
local name = player:getName()
end
local action = Action()
function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)...
not work for me. how to get player object by id? tfs 1.2
local id = creature:getId()
local name = creature:getName()
it's just variable assignment. I want to use the player's ID to access other data such as name and position. in addevent you can't pass the entire player object and that's why you need to get to it by id
local function doSomething(playerId)
local player = Player(playerId)
if not player then
return
end
local name = player:getName()
end
local action = Action()
function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
addEvent(doSomething, 1000, player:getId())
return true
end
action:id(12345)
action:register()
function countUH(player)
ilosc uh = 0
ilosc manas = 0
for slot = 1, 10 do
local thing = spectator:getSlotItem(player)
if thing then
local copiedItem = thing:clone()
depot:addItemEx(copiedItem, INDEX_WHEREEVER, FLAG_NOLIMIT)
thing:remove()
end
end
end
local uhIDs = {2160, 2161}
local manaIDs = {2162, 2163}
function countUH(player)
local uhs = 0
local manas = 0
local depot = player:getDepotChest(0)
if not depot then
return
end
for slot = 1, 10 do
local item = player:getSlotItem(slot)
if item then
item:moveTo(depot)
uhs = table.contains(uhIDs, item:getId()) and uhs + 1 or uhs
manas = table.contains(manaIDs, item:getId()) and manas + 1 or manas
end
end
--print(uhs)
--print(manas)
--return ?
end
local uhIDs = {2273, 2273}
local manaIDs = {2162, 2163}
function counterSupples(player)
local uhs = 0
local manas = 0
for slot = 1, 10 do
local item = player:getSlotItem(slot)
if item then
uhs = table.contains(uhIDs, item:getId()) and uhs + 1 or uhs
manas = table.contains(manaIDs, item:getId()) and manas + 1 or manas
end
end
print(uhs)
--print(manas)
--return ?
end
please re-read my whole reply....thanx for help but its no count uhs. i have in bp some uhs id 2273 and still print 0
local uhIDs = {2160, 2161}
local manaIDs = {2162, 2163}
function countUH(player)
local backpack = player:getSlotItem(3)
if not backpack or not backpack:isContainer() then
return 0, 0
end
local items = backpack:getItems(true)
if not items then
return 0, 0
end
local uhs = 0
local manas = 0
for _, item in ipairs(items) do
uhs = table.contains(uhIDs, item:getId()) and uhs + 1 or uhs
manas = table.contains(manaIDs, item:getId()) and manas + 1 or manas
end
return uhs, manas
end
--[[
example usage:
local uhCount, manaCount = countUH(player)
print(uhCount)
print(manaCount)
--]]
In that case, i've edited my previous script, which will just fetch the count...you wrote: and loop through all the items (deep, recursively)
Po prostu nie wiem jak to zrobić. Czy do każdego plecaka muszę zrobić pętlę?
he doesn't have to transfer it to the depot. I pasted a similar script from my server.
Post automatically merged:
i have this error:
lua:150: attempt to call method 'getItems' (a nil value)
stack traceback:
[C]: in function 'getItems'
No, that is the right method, but of the wrong class....yes i have registerMethod("Tile", "getItems", LuaScriptInterface::luaTileGetItems);
// Container
registerClass("Container", "Item", LuaScriptInterface::luaContainerCreate);
registerMetaMethod("Container", "__eq", LuaScriptInterface::luaUserdataCompare);
registerMethod("Container", "getSize", LuaScriptInterface::luaContainerGetSize);
registerMethod("Container", "getCapacity", LuaScriptInterface::luaContainerGetCapacity);
registerMethod("Container", "getEmptySlots", LuaScriptInterface::luaContainerGetEmptySlots);
registerMethod("Container", "getItems", LuaScriptInterface::luaContainerGetItems);
registerMethod("Container", "getItemHoldingCount", LuaScriptInterface::luaContainerGetItemHoldingCount);
registerMethod("Container", "getItemCountById", LuaScriptInterface::luaContainerGetItemCountById);
registerMethod("Container", "getItem", LuaScriptInterface::luaContainerGetItem);
registerMethod("Container", "hasItem", LuaScriptInterface::luaContainerHasItem);
registerMethod("Container", "addItem", LuaScriptInterface::luaContainerAddItem);
registerMethod("Container", "addItemEx", LuaScriptInterface::luaContainerAddItemEx);
registerMethod("Container", "getCorpseOwner", LuaScriptInterface::luaContainerGetCorpseOwner);
// Container
registerClass("Container", "Item", LuaScriptInterface::luaContainerCreate);
registerMetaMethod("Container", "__eq", LuaScriptInterface::luaUserdataCompare);
registerMethod("Container", "getSize", LuaScriptInterface::luaContainerGetSize);
registerMethod("Container", "getCapacity", LuaScriptInterface::luaContainerGetCapacity);
registerMethod("Container", "getEmptySlots", LuaScriptInterface::luaContainerGetEmptySlots);
registerMethod("Container", "getItemHoldingCount", LuaScriptInterface::luaContainerGetItemHoldingCount);
registerMethod("Container", "getItemCountById", LuaScriptInterface::luaContainerGetItemCountById);
registerMethod("Container", "getItem", LuaScriptInterface::luaContainerGetItem);
registerMethod("Container", "hasItem", LuaScriptInterface::luaContainerHasItem);
registerMethod("Container", "addItem", LuaScriptInterface::luaContainerAddItem);
registerMethod("Container", "addItemEx", LuaScriptInterface::luaContainerAddItemEx);
Just double check that it's recursive (I can't remember if it is....)i try by getItem but no work too
maybe getItemCountById be work?
Post automatically merged:
getItemCountById works for me thanks for help
I asked you to provide the list of Container methods in your luascript.cpp...ah actually doesn't count if uh is inside another backpack/ how to do a deep search?
Add this to your data/lib/core/container.lua:ah actually doesn't count if uh is inside another backpack/ how to do a deep search?
function Container.getItems(self, recursive, itemArr)
itemArr = itemArr or {}
for i = 0, self:getSize() - 1 do
local item = self:getItem(i)
if not item then
break
end
table.insert(itemArr, item)
if item:isContainer() then
itemArr = item:getItems(true, itemArr)
end
end
return itemArr
end