Treasure Chest

Post Reply
dragonarmor
Posts: 36
Joined: Sat Feb 21, 2015 10:08 am

Treasure Chest

Post by dragonarmor » Wed May 17, 2017 9:20 am

So I've searched the forums and i'm not really coming up with anything on this. What i'd like to do is have certain mobs spawn a treasure chest or pyrix upon death and have custom loot inside. If someone could at least point me in the right direction it would be much appreciated. Thank you

dragonarmor
Posts: 36
Joined: Sat Feb 21, 2015 10:08 am

Re: Treasure Chest

Post by dragonarmor » Thu May 18, 2017 12:14 pm

Basically I'm just looking for an onMobDeath function I can use to spawn chests. I can figure out the rest. Any help is appreciated.

User avatar
TeoTwawki
Developer
Posts: 523
Joined: Mon Jul 15, 2013 9:50 pm

Re: Treasure Chest

Post by TeoTwawki » Thu May 18, 2017 3:39 pm

Updated

While retail has multiple chests in a single zone being shared by lots of mobs, if you just have a few specific ones this is rather easy.

Make sure status.lua is required (in both scripts):

Code: Select all

require("scripts/globals/status");
In the mob script:

Code: Select all

function onMobDeath(mob, player, isKiller)
    local npcid = put_ID_here
    local timestamp = os.time()

    if (isKiller) then -- this check ensures its only running once per
        GetNPCById(npcid):setLocalVar(npcid, timestamp) -- Timestamp the NPC
        GetNPCById(npcid):setPos(mob:getXpos(), mob:getYPos(), mob:getYPos()); -- Set chest position to where mob died.
        GetNPCById(npcid):setStatus(STATUS_NORMAL) -- Spawn it!
    end

    -- This part hits every member of all parties in the alliance
    player:setLocalVar(npcid, timestamp) -- Timestamp the players
end

Code: Select all

function onMobDeath(mob, player, isKiller)
    if (isKiller) then -- this check ensures its only running once per
        local npcid = put_ID_here
        local timestamp = os.time()
        GetNPCById(npcid):setLocalVar(npcid, timestamp)
        for member, player in player:getAlliance() do
            member:setLocalVar(npcid, timestamp)
        end
        -- Spawn it!
        GetNPCById(npcid):setStatus(STATUS_NORMAL)
    end
end
[/strike]

in the npc script for your chest:

Code: Select all

function onTrigger(player,npc)
    -- comparing these variables prevents wrong people opening the box
    if (player:getLocalVar(npc:getId()) == npc:getLocalVar(npc:getId()) then
        player:startEvent( whatever the event ID is goes here )
    end
You'll also need onEventFinish code to handle your event. This will only work in zones that actually have retail treasure box events, like abyssea. You will need

Code: Select all

GetNPCById(npcid):setStatus(STATUS_DISAPPEAR)
at the end of the event to despawn the box.

If you are looking for a dynamic system to do what retails does, I am workin on it (slowly, lots of real life/offline distractions these days) and will have it in the project when its usable.
Last edited by TeoTwawki on Thu Aug 03, 2017 11:14 pm, edited 3 times in total.
Hi, I run The Demiurge server.

Code: Select all

PLS USE [code]CODE TAGS[/code] WHEN POSTING CODE
Image
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. - Martin Golding
DO NOT PRIVATE MESSAGE ME ABOUT BUGS

dragonarmor
Posts: 36
Joined: Sat Feb 21, 2015 10:08 am

Re: Treasure Chest

Post by dragonarmor » Thu May 18, 2017 3:51 pm

That is exactly the info I needed. Thanks a lot! All the work you guys do on this project is amazing keep up the good work!

dragonarmor
Posts: 36
Joined: Sat Feb 21, 2015 10:08 am

Re: Treasure Chest

Post by dragonarmor » Thu Aug 03, 2017 10:11 am

I cannot seem to get the chest to spawn. i'm getting this error:
lua:22: attempt to call a table value

function onMobDeath(mob, player, isKiller)
if (isKiller) then -- this check ensures its only running once per
local npcId = 17216140
local timestamp = os.time()
GetNPCByID(npcId):setLocalVar(npcId, timestamp)
for member, player in player:getAlliance() do <<<-- THIS line 22 error
member:setLocalVar(npcId, timestamp)
end
-- Spawn it!
GetNPCByID(npcId):setStatus(STATUS_NORMAL)
end
end

User avatar
TeoTwawki
Developer
Posts: 523
Joined: Mon Jul 15, 2013 9:50 pm

Re: Treasure Chest

Post by TeoTwawki » Thu Aug 03, 2017 7:36 pm

My bad.
We changed some onMobDeath things several times and it used to be called killer before "isKiller" came about, and the come now runs once per alliance member natively and I didn't update from my old version proper.. So lets try:

Code: Select all

function onMobDeath(mob, player, isKiller)
    local npcid = put_ID_here
    local timestamp = os.time()

    if (isKiller) then -- this check ensures its only running once per
        GetNPCById(npcid):setLocalVar(npcid, timestamp) -- Timestamp the NPC
        GetNPCById(npcid):setStatus(STATUS_NORMAL) -- Spawn it!
    end

    -- This part hits every member of all parties in the alliance
    player:setLocalVar(npcid, timestamp) -- Timestamp the players
end
I think the error you got was because I used "player" when player was already your baseentity. But this version get rid of the need for using getAlliance inside a for loop anyway.
Hi, I run The Demiurge server.

Code: Select all

PLS USE [code]CODE TAGS[/code] WHEN POSTING CODE
Image
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. - Martin Golding
DO NOT PRIVATE MESSAGE ME ABOUT BUGS

dragonarmor
Posts: 36
Joined: Sat Feb 21, 2015 10:08 am

Re: Treasure Chest

Post by dragonarmor » Thu Aug 03, 2017 8:13 pm

Well I don't get any errors this time but no chest pops. here is my code

Code: Select all

-----------------------------------
-- Area: South Gustaberg
--  MOB: Land Crab
-----------------------------------

require("scripts/globals/fieldsofvalor");
require("scripts/globals/status");


-----------------------------------
-- onMobDeath
-----------------------------------

function onMobDeath(mob, player, isKiller)
    local npcid = 17216156
    local timestamp = os.time()

    if (isKiller) then -- this check ensures its only running once per
        GetNPCByID(npcid):setLocalVar(npcid, timestamp) -- Timestamp the NPC
        GetNPCByID(npcid):setStatus(STATUS_NORMAL) -- Spawn it!
    end

    -- This part hits every member of all parties in the alliance
    player:setLocalVar(npcid, timestamp) -- Timestamp the players
end

User avatar
TeoTwawki
Developer
Posts: 523
Joined: Mon Jul 15, 2013 9:50 pm

Re: Treasure Chest

Post by TeoTwawki » Thu Aug 03, 2017 11:02 pm

whats the position of the NPC? if its all zero for pos data in the database it will be unable to be spawned. I always forget people new to this won't know these things :x Show me your sql line for this npc, just to be sure.

If you want a dynamic location (wherever mob died) that can be done with an addition script command but the database still has to be non zero.
Hi, I run The Demiurge server.

Code: Select all

PLS USE [code]CODE TAGS[/code] WHEN POSTING CODE
Image
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. - Martin Golding
DO NOT PRIVATE MESSAGE ME ABOUT BUGS

dragonarmor
Posts: 36
Joined: Sat Feb 21, 2015 10:08 am

Re: Treasure Chest

Post by dragonarmor » Fri Aug 04, 2017 9:31 am

Ahh yes they were all zeros. Working now thanks for your assistance on this one.

Post Reply