Skip to content

item

Reads and changes an item: name, amount, owner, consume, delete, message.

Source: src/Moongate.Server.Ultima/Modules/ItemModule.cs

item.add_loot(container, table, rolls?) -> integer

Rolls a loot table of templates/loots once, or rolls times, into a container, or into the backpack of a mobile, and gives how many items it added; 0 for rolls that give nothing, an unknown table or something that is no container; fewer than the roll gave when the container is full (125 items) or no item serial is at hand.

ParameterTypeDefault
containerinteger
tablestring
rolls (optional)integer1

Returns integer.

item.amount(serial) -> integer?

The item's amount; nil for an unknown item.

ParameterTypeDefault
serialinteger

Returns integer?.

item.can_hold(container, serial) -> boolean

Whether the container, and every container it is in, still holds its limit of stones with the item in it: the template's max_weight, 400 without one and none for 0, a bank box having none. False for an unknown container or item. The count of items a pack may hold is not asked.

ParameterTypeDefault
containerinteger
serialinteger

Returns boolean.

item.combat_stats(serial) -> table?

What the item is in a fight, from its template, as a table: weapon_type (sword, axe, pole_arm, mace, staff, fencing, bow, crossbow, thrown) or nil, two_handed (false when the template does not say), damage_min, damage_max, speed, armor_rating and max_hits, nil for what the template does not say; nil for an item without a template or a serial that is no item.

ParameterTypeDefault
serialinteger

Returns table?.

item.consume(serial, amount?) -> boolean

Takes amount units (default 1) off the item, deleting it at 0, and shows the change to the owner or the players around a ground stack; false for a worn item, an amount below 1, fewer units left or an item a player holds on the cursor.

potion.lua, the script of a potion that is drunk on a double click:

potion = {}
function potion.on_use(serial, user)
item.message(serial, user, "You drink the potion.")
item.consume(serial)
return true
end
ParameterTypeDefault
serialinteger
amount (optional)integer1

Returns boolean.

item.container(serial) -> integer?

The serial of the container the item lies in; nil for an item on the ground, a worn one or an unknown one.

ParameterTypeDefault
serialinteger

Returns integer?.

item.contents(serial) -> table

The serials of the items lying directly in the container, as a list; empty for an empty or unknown container.

The items lying directly in a container:

for _, inside in ipairs(item.contents(bag)) do ... end
ParameterTypeDefault
serialinteger

Returns table.

item.create(template, map, x, y, z, amount?) -> integer?

Makes an item from a template on the ground at x, y, z of the map, seen by the players around, and gives its serial; nil for an unknown template, a spot outside the map, a z outside -128 to 127, an amount the template cannot have, or when no serial is ready, as item.give.

ParameterTypeDefault
templatestring
mapMapType | string
xinteger
yinteger
zinteger
amount (optional)integernil

Returns integer?.

item.delete(serial) -> boolean

Deletes the item; false for a worn item, a held item or a container that still holds items.

ParameterTypeDefault
serialinteger

Returns boolean.

item.durability(serial) -> table?

The hits and the maximum hits of a weapon or a piece of armor, as { hits, max }: its durability and max_durability props, or the template's max_hits for a piece that never wore. Nil for an item that cannot wear (no max_hits) or an unknown one.

Whether a piece is worn down:

local hits = item.durability(serial)
if hits and hits.hits < hits.max then
mobile.message(user, "It needs mending.")
end
ParameterTypeDefault
serialinteger

Returns table?.

item.dyeable(serial) -> boolean

Whether the item's template is dyeable, as clothing is: a dye tub can give it its hue; false for an unknown item.

ParameterTypeDefault
serialinteger

Returns boolean.

item.equip(serial, mobile) -> boolean

Puts the item on the mobile, on its template's layer, seen by everyone around; its script runs on_equip, its can_equip is not asked. False for a worn or held item, a stack, an item without a layer or one the mobile cannot wear, a taken layer, a mobile not in the world, or an item another mobile carries.

Taken from a chest on the ground, those who look into the chest see it go.

ParameterTypeDefault
serialinteger
mobileinteger

Returns boolean.

item.find(holder, template) -> table

The serials of the items of a template inside a container, at any depth, or worn and carried by a mobile (not what lies in its bank), as a list; empty when there is none, or for an unknown container or mobile.

The gold a player carries, pile by pile:

for _, coins in ipairs(item.find(user, "gold")) do ... end
ParameterTypeDefault
holderinteger
templatestring

Returns table.

item.get_prop(serial, key) -> any?

A value the item keeps across restarts: a string, a number or a bool; nil when it has none.

ParameterTypeDefault
serialinteger
keystring

Returns any?.

item.give(mobile, template, amount?, hue?) -> integer?

Makes an item from a template in the mobile's backpack and gives its serial; what stacks joins the stack of its kind already lying in the backpack (same template, hue and name, no prop of its own, 60000 at most) and the serial is that stack's. The owner sees it at once and its next save keeps it. Nil for an unknown mobile or template, a mobile without a backpack, an amount the template cannot have (more than 1 of what does not stack), a hue outside 0 to 65535, or when no serial is ready: the server keeps 64 in reserve and refills them in the background, so making more in one go gives nil for the rest; try again later. hue, when given, colours the item before it looks for a stack, so it joins only a stack of that colour.

ParameterTypeDefault
mobileinteger
templatestring
amount (optional)integernil
hue (optional)integernil

Returns integer?.

item.has_room(mobile, template, amount?) -> boolean

Whether item.give(mobile, template, amount) would find room: the mobile has a backpack, the template can be made in that amount, and the item either joins a stack of its kind or the backpack takes one more item. Nothing is made. False for an unknown mobile or template, a mobile without a backpack, or an amount the template cannot have. It does not look at the weight, as item.give does not, and it cannot know that the pool of serials is empty.

Ask before taking something to give something back, as the quests do before a turn-in:

if not item.has_room(user, "healpotion", 2) then
mobile.message(user, "Your pack is full.")
return
end
ParameterTypeDefault
mobileinteger
templatestring
amount (optional)integernil

Returns boolean.

item.has_room_for_all(mobile, items) -> boolean

Whether item.give would find room for all the items of the list together, a list of { template, amount } tables (amount 1 when left out): what joins a stack of its kind in the backpack, or another new item of the list, needs no slot, the others take one each. False for an unknown mobile or template, a mobile without a backpack, an entry that is no table or has no template; true for an empty list.

The same for several items together; what joins a stack needs no slot:

local reward = { { template = "gold_coin", amount = 100 }, { template = "healpotion", amount = 2 } }
if item.has_room_for_all(user, reward) then ... end
ParameterTypeDefault
mobileinteger
itemstable

Returns boolean.

item.hue(serial) -> integer?

The item's hue, 0 for the colours of its art; nil for an unknown item.

ParameterTypeDefault
serialinteger

Returns integer?.

item.in_range(serial, mobile, range) -> boolean

Whether the mobile is on the map of a ground item and within range tiles of it, the larger of the two differences; false for an item not on the ground, a mobile not in the world or a negative range.

if item.in_range(serial, user, 2) then ... end
ParameterTypeDefault
serialinteger
mobileinteger
rangeinteger

Returns boolean.

item.is_container(serial) -> boolean

Whether the item's template is a container, as the spells of the lock and of the trap ask; false for an item whose template is not one or an unknown item.

ParameterTypeDefault
serialinteger

Returns boolean.

item.is_held(serial) -> boolean

Whether a player has lifted the item and holds it on the cursor: it still counts where it was taken from until it is dropped, and item.consume and item.set_item_id refuse it. False for an unknown item.

ParameterTypeDefault
serialinteger

Returns boolean.

item.is_movable(serial) -> boolean

Whether the item can be picked up: its own flag, else its template's, else the tiledata's weight (255 cannot be lifted); false for an unknown item.

ParameterTypeDefault
serialinteger

Returns boolean.

item.is_stackable(template) -> boolean

Whether the items of a template stack: the template's stackable, else the tiledata of its graphic. False for an unknown template.

ParameterTypeDefault
templatestring

Returns boolean.

item.item_id(serial) -> integer?

The item's graphic; nil for an unknown item.

ParameterTypeDefault
serialinteger

Returns integer?.

item.layer(serial) -> string?

The layer a worn item is on, as a lowercase name such as backpack, bank or one_handed; nil for an item that is not worn, lies in a container or on the ground, and for an unknown one.

ParameterTypeDefault
serialinteger

Returns string?.

item.location(serial) -> table?

Where a ground item lies, as a table { x, y, z, map }; nil for an item not on the ground.

ParameterTypeDefault
serialinteger

Returns table?.

item.look(target, player) -> boolean

Shows the player what a single click on the item or the mobile shows: the first line of its tooltip, its name (with title, amount and the name its template gives), as a label over it seen by that player only. False for an unknown serial, a player not in the world, or something the player cannot see.

ParameterTypeDefault
targetinteger
playerinteger

Returns boolean.

item.loot_type(serial) -> string?

What happens to the item when its owner dies, as its own loot type or its template's: regular, newbied, blessed or cursed; nil for an unknown item.

ParameterTypeDefault
serialinteger

Returns string?.

item.message(serial, player, text) -> boolean

A label over the item seen only by that player (cut to 128 characters); false for blank text, an unknown item or a player not in the world.

ParameterTypeDefault
serialinteger
playerinteger
textstring

Returns boolean.

item.message_cliloc(serial, player, cliloc, args?) -> boolean

A label over the item seen only by that player, with a text of the client by its number, in the client's language; args fill its ~1_NAME~ places, split by tabs. False when the player or the item is not in the world.

ParameterTypeDefault
serialinteger
playerinteger
clilocinteger
args (optional)stringnil

Returns boolean.

item.move_into(serial, container, transfer?) -> boolean

Moves the item into a container, or into a mobile's backpack; those who saw it lose it and the new owner sees it. No weight or item limit is checked. False for a worn or held item, a target that is not a container, a container put into itself or into what it holds, or an item one mobile carries moved to another mobile (a trade, not supported yet) unless transfer is true, as a theft does: the item is then saved by its new owner's leave or the world save, and until then a crash gives it back to the old one. A part cut off a pile with item.split is a new item with its own row: a crash between the owners' saves can lose it, or leave it in both packs.

Players who have a container on the ground open do not see it change until they open it again.

ParameterTypeDefault
serialinteger
containerinteger
transfer (optional)booleannil

Returns boolean.

item.move_to(serial, x, y, z) -> boolean

Moves a ground item to x, y, z on its map: the players around the old spot lose it, those around the new one see it, and a decaying item's decay starts again; false for an item not on the ground, a spot outside the map or a z outside -128 to 127.

ParameterTypeDefault
serialinteger
xinteger
yinteger
zinteger

Returns boolean.

item.name(serial) -> string?

The item's name, or its template id when it has none; nil for an unknown item.

ParameterTypeDefault
serialinteger

Returns string?.

item.owner(serial) -> integer?

The serial of the mobile carrying or wearing the item; nil on the ground or unknown.

A cursed ring: once worn, it stays on. A worn item has an owner and lies in no container:

ring = {}
function ring.can_pick_up(serial, picker)
-- worn: it has an owner and lies in no container
if item.owner(serial) == picker and item.container(serial) == nil then
mobile.message(picker, "The ring will not come off.")
return false
end
end
ParameterTypeDefault
serialinteger

Returns integer?.

item.play_sound(serial, sound) -> boolean

Plays a sound id (0 to 65535) where the item lies, or where the mobile carrying it stands, for the players within 15 cells; false for an unknown item, a sound out of range or an item inside a container on the ground.

ParameterTypeDefault
serialinteger
soundinteger

Returns boolean.

item.script(serial) -> string?

The script_id of the item's template, such as 'door' or 'dye_tub'; nil for an item whose template has no script, or an unknown item.

ParameterTypeDefault
serialinteger

Returns string?.

item.search(text, limit?) -> table

Finds item templates whose id or name holds the text, without regard to case, and those of the graphic when the text is a number (3903 or 0xF3F), sorted by id, as a list of { id, name, graphic } with the template's id as the name when it has none. limit is the most to give, 40 by default and between 1 and 100; empty for a blank text.

ParameterTypeDefault
textstring
limit (optional)integer40

Returns table.

item.set_amount(serial, amount) -> boolean

Sets the stack's amount (1 to 60000), shown to the players who see it; false for a worn or held item, an amount out of range, or an amount above 1 on an item that does not stack, as its template or its graphic says.

ParameterTypeDefault
serialinteger
amountinteger

Returns boolean.

item.set_durability(serial, hits, max) -> boolean

Sets the hits and the maximum hits of a weapon or a piece of armor, keeping them on the item across restarts; the hits are kept from 0 to the maximum, and its tooltip shows them again. False for an item that cannot wear (no max_hits), one that is worn, one on a player's cursor, a maximum under 1 or an unknown item.

A reward that mends the piece a player carries:

local hits = item.durability(serial)
if hits then
item.set_durability(serial, hits.max, hits.max)
end
ParameterTypeDefault
serialinteger
hitsinteger
maxinteger

Returns boolean.

item.set_hue(serial, hue) -> boolean

Changes the item's hue (0 to 65535), shown to the players who see it; false for a worn or held item.

ParameterTypeDefault
serialinteger
hueinteger

Returns boolean.

item.set_item_id(serial, graphic) -> boolean

Changes the item's graphic (0 to 65535), as a door opening, shown to the players around a ground item or the owner of a carried one; an item inside a container on the ground changes without being shown again. False for an unknown, worn or held item or a graphic out of range.

ParameterTypeDefault
serialinteger
graphicinteger

Returns boolean.

item.set_light(serial, type?) -> boolean

Sets the item's light shape by LightType name, such as circle150, circle300 or west_big, nil clears it; the players who see the item are shown it again, and the client draws the light only for a lit graphic. False for an unknown shape or a worn or held item.

ParameterTypeDefault
serialinteger
type (optional)stringnil

Returns boolean.

item.set_name(serial, name?) -> boolean

Sets the item's own name (cut to 128 characters), nil gives it back its template's, shown to the players who see it; false for a worn or held item.

ParameterTypeDefault
serialinteger
name (optional)stringnil

Returns boolean.

item.set_prop(serial, key, value?) -> boolean

Keeps a string, a number or a bool on the item across restarts, saved by the world save or its owner's save; nil removes it. False for a table, a function, a blank key or a timer.<name> key, which the item's timers use.

ParameterTypeDefault
serialinteger
keystring
value (optional)anynil

Returns boolean.

item.set_rarity(serial, rarity) -> boolean

Sets the item's rarity: 'common', 'uncommon', 'rare', 'epic' or 'legendary'; its tooltip shows a rarity above common, in its colour, and an item of another rarity does not join its stack. False for an unknown item or rarity.

A reward chest that makes one of its prizes rare:

local prize = item.give(user, "0x13ff")
if prize then
item.set_rarity(prize, "rare")
end
ParameterTypeDefault
serialinteger
raritystring

Returns boolean.

item.show_contents(container, player) -> boolean

Opens the container on the player's client, with the items directly inside it, as a double click would, with no check of whether the player may: the script has decided. False for a serial that is not an item container, or a player not in the world.

ParameterTypeDefault
containerinteger
playerinteger

Returns boolean.

item.split(serial, amount) -> integer?

Cuts amount units off a stack: the item keeps amount of them and a new item holds the rest where the stack lay, with its props and hue; gives the serial of the rest. Nil for an item that is no stack, held or worn, an amount under 1 or not under the stack's, an unknown item, or when no serial is left.

ParameterTypeDefault
serialinteger
amountinteger

Returns integer?.

item.start_timer(serial, name, seconds) -> boolean

Starts, or starts again from now, a timer of the item that runs on_timer(serial, name) of its script after that many seconds, also after a restart; one whose script has no on_timer, or fails, is dropped with a warning in the log. False for an unknown item, a blank name or one over 32 characters, or seconds not above 0 or over a year.

The timers are kept in the props timer.<name>, which item.set_prop refuses; splitting a stack leaves them with the part that is lifted.

ParameterTypeDefault
serialinteger
namestring
secondsnumber

Returns boolean.

item.stop_timer(serial, name) -> boolean

Stops a timer of the item; false for an unknown item or when it has no timer of that name.

ParameterTypeDefault
serialinteger
namestring

Returns boolean.

item.template(serial) -> string?

The id of the item's template; nil for an unknown item.

ParameterTypeDefault
serialinteger

Returns string?.

item.timer(serial, name) -> number?

The seconds a timer of the item still has to run, 0 when it is due; nil for an unknown item or when it has no timer of that name.

ParameterTypeDefault
serialinteger
namestring

Returns number?.

item.unequip(serial) -> boolean

Takes the worn item off its wearer and puts it into the wearer's backpack, as a disarm does: everyone around sees it go and the wearer sees it in the pack. No weight or item limit is checked. False for an item nobody wears, one held on the cursor, one that cannot be moved, the backpack, the bank box, the mount, the hair and the face, a wearer not in the world or with no backpack.

ParameterTypeDefault
serialinteger

Returns boolean.

item.weight(serial) -> integer?

The stones the item weighs, with everything inside it: each pile is its unit weight times its amount, rounded up, as its tooltip says; nil for an unknown item.

ParameterTypeDefault
serialinteger

Returns integer?.

item.worn_by(serial) -> integer?

The serial of the mobile that wears the item or holds it in its hands (a backpack is worn too); nil for an item inside a backpack or another container, on the ground, lifted onto a player's cursor or unknown.

An axe chops only in the hands of who uses it, not from the backpack:

if item.worn_by(serial) ~= user then
mobile.message(user, "Take the axe in your hands first.")
end
ParameterTypeDefault
serialinteger

Returns integer?.

Made with ❤️ by Tom

Buy me a coffee