• 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!

[The OTServBR based on TFS 1.3] - Transforming Stacked Items [Action]

Sir Richard

New Member
Joined
Jun 10, 2009
Messages
19
Reaction score
3
Location
Brazil
I've been trying to come up with a solution for the following problem:

A player uses an item which is supposed to be transformed into a random item, but the used item is stackable, and when the action happens, the whole stack is transformed to the reward. e.g.: the player uses a stack of 'concentrated demonic blood', but all of it becomes the same item.

Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    item:getPosition():sendMagicEffect(CONST_ME_DRAWBLOOD)
    item:transform(math.random(7588, 7589))
    return true
end

While I could be simply adding the item to the player (player:addItem), it should be transformed to the current place where the item stands, be it on a secondary backpack, the floor, the inside of a depot, etc...

My question is, how do I use item:transform in a way that it only transforms one item of the stack, into the desired reward (be it 1 ... n items), on the current position (ground, backpack, dp...)?

Alternatively, is there a way to use 'Game.createItem' as a solution? (I've attempted this, but if used inside of a container, no item is created inside of it, since to/fromPosition/item:getPosition()) onnly works outside of a container)

What I've attempted thus far:

Code:
    item:transform(itemId, count)

    &

    Game.createItem(itemId, count, toPosition)

Sorry it it's an easy one, but it's been a while since I worked on ots lol
 
Solution
Hi,
Im at work now, when i get home i will try. I don't know if have a way to do with transform, what i Will try is:
Get item info and item position(if in container, the Index)
Remove original item 1 count
Create the reward as New item
Move the reward to old item position

But if u have the original item with 5 count in the position, the item will remain in the position with 4 count and the reward is created in same position? If the position is a container, can't be created in same Index...

--Edit:
@Sir Richard
Try this:

Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local reward = math.random(7588, 7589)
    if fromPosition.x == CONTAINER_POSITION then --item is inside a container
        local...
Hi,
Im at work now, when i get home i will try. I don't know if have a way to do with transform, what i Will try is:
Get item info and item position(if in container, the Index)
Remove original item 1 count
Create the reward as New item
Move the reward to old item position

But if u have the original item with 5 count in the position, the item will remain in the position with 4 count and the reward is created in same position? If the position is a container, can't be created in same Index...

--Edit:
@Sir Richard
Try this:

Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local reward = math.random(7588, 7589)
    if fromPosition.x == CONTAINER_POSITION then --item is inside a container
        local targetContainer = Container(item:getParent().uid) --get the container that original item was used
        --targetContainer:addItem(reward, 1, fromPosition.z)--add a item to target container in the index of original item (NOT WORKING)
        targetContainer:addItem(reward, 1)
    else--item isn't inside a container
        Game.createItem(reward, 1, fromPosition)
    end
    item:getPosition():sendMagicEffect(CONST_ME_DRAWBLOOD)
    item:remove(1)
    return true
end
The code above is working, only missing create the reward in the index of original item, to fix: you can get all itens in container and just reorder the indexs, to fit your needs. I think isn't needed.

The CONTAINER_POSITION variable is declareted in: data\lib\core\constants.lua and have the value of: 0xFFFF.

This post helped me.

Hope it works.
 
Last edited:
Solution
Hi,
Im at work now, when i get home i will try. I don't know if have a way to do with transform, what i Will try is:
Get item info and item position(if in container, the Index)
Remove original item 1 count
Create the reward as New item
Move the reward to old item position

But if u have the original item with 5 count in the position, the item will remain in the position with 4 count and the reward is created in same position? If the position is a container, can't be created in same Index...

--Edit:
@Sir Richard
Try this:

Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local reward = math.random(7588, 7589)
    if fromPosition.x == CONTAINER_POSITION then --item is inside a container
        local targetContainer = Container(item:getParent().uid) --get the container that original item was used
        --targetContainer:addItem(reward, 1, fromPosition.z)--add a item to target container in the index of original item (NOT WORKING)
        targetContainer:addItem(reward, 1)
    else--item isn't inside a container
        Game.createItem(reward, 1, fromPosition)
    end
    item:getPosition():sendMagicEffect(CONST_ME_DRAWBLOOD)
    item:remove(1)
    return true
end
The code above is working, only missing create the reward in the index of original item, to fix: you can get all itens in container and just reorder the indexs, to fit your needs. I think isn't needed.

The CONTAINER_POSITION variable is declareted in: data\lib\core\constants.lua and have the value of: 0xFFFF.

This post helped me.

Hope it works.

Oh, wow! It's exactly what I was looking for haha.

Tested and working wonders! Thanks a bunch, @zxmatzx :D
 
Back
Top