- Joined
- Mar 16, 2017
- Messages
- 1,408
- Solutions
- 154
- Reaction score
- 1,958
- Location
- London
- GitHub
- MillhioreBT
- Twitch
- millhiorebt
This is a short script that I wrote quickly for those who still have the habit of creating small chest rewards in the map editor.
data/scripts/rewardContainerSystem.lua
This is the base storage of the system:
This means that if in your editor you configure the container with a uniqueID of
This storage is where the status of the reward will be saved, in this way we can know if a character has already obtained said reward and in the future we will not give it to them anymore, this is where we say that the container is empty:
This is where we will create a new bag, this bag will contain the reward, so what we do is clone the items inside our container and these cloned items will go directly to the new bag:
Next we will try to give the bag to the player, if it is not possible to give it to him it is because he does not have the capacity to carry it, then we will send a message to the player, the message will be related to the reason why the player cannot carry it
OFC we will eliminate the bag, since it is virtual and no one will use it at any time. (in case the player cannot carry it)
If everything is correct, the player will receive his reward and we will send him a message with the content
We will also update the storage so that in the future this player will not receive this reward again
That's it, here I will leave a quick video demonstration of how I did it
Note: in the video I forgot to add
data/scripts/rewardContainerSystem.lua
Lua:
local action = Action()
function action.onUse(player, item, fromPos, target, toPos, isHotkey)
if not item:getType():isContainer() then
return true
end
local storageId = 5000 + item:getUniqueId()
if player:getStorageValue(storageId) == 1 then
player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
return true
end
local bag = Game.createItem("bag", 1)
for index, item in pairs(item:getItems()) do
bag:addItemEx(item:clone())
end
local returnValue = player:addItemEx(bag)
if returnValue ~= RETURNVALUE_NOERROR then
bag:remove()
player:sendCancelMessage(returnValue)
return true
end
player:sendTextMessage(MESSAGE_LOOT, string.format("You found: %s", bag:getContentDescription()))
player:setStorageValue(storageId, 1)
return true
end
action:aid(8010)
action:register()
This is the base storage of the system:
5000
This means that if in your editor you configure the container with a uniqueID of
1000
, then the result will be 6000
This storage is where the status of the reward will be saved, in this way we can know if a character has already obtained said reward and in the future we will not give it to them anymore, this is where we say that the container is empty:
This is where we will create a new bag, this bag will contain the reward, so what we do is clone the items inside our container and these cloned items will go directly to the new bag:
Next we will try to give the bag to the player, if it is not possible to give it to him it is because he does not have the capacity to carry it, then we will send a message to the player, the message will be related to the reason why the player cannot carry it
OFC we will eliminate the bag, since it is virtual and no one will use it at any time. (in case the player cannot carry it)
If everything is correct, the player will receive his reward and we will send him a message with the content
We will also update the storage so that in the future this player will not receive this reward again
That's it, here I will leave a quick video demonstration of how I did it
Note: in the video I forgot to add
bag:remove()
ajajja sorry