quest item not triggering OnUse?

I want to change what happens when a quest item is used, but my script isn’t being triggered and I can’t figure out why.

The following was just an attempt to do something simple. When I get this working, I’ll do something more interesting.

I don’t think it’s even triggering OnUse when I use the item. I don’t see anything in the log and when I use the item in the game it starts its cooldown, but it doesn’t do anything else.

What am I doing wrong?

#include “ScriptMgr.h”
#include “Player.h”
#include “Log.h”

class warsong_flare_gun: public ItemScript
{
public:
warsong_flare_gun() : ItemScript(“warsong_flare_gun”) {}

bool OnUse(Player* player, Item* item) OVERRIDE
{
    TC_LOG_INFO(LOG_FILTER_WORLDSERVER, "%lu using Warsong Flare Gun", player->GetGUID());

    if (player->GetQuestStatus(11711) == QUEST_STATUS_INCOMPLETE)
    {
        player->Say("I have the Quest!!", LANG_UNIVERSAL);
    }
    player->Say("Valiance Keep Officer... Come Here!!", LANG_UNIVERSAL);
    return true;
}

};

void AddSC_warsong_flare_gun()
{
new warsong_flare_gun();
}
I’ve been reading tutorials and examples that say I need to add my script name to the item_template.ScriptName field. In my current version of the database, item_template doesn’t have a ScriptName field, but I did find the item_script_names table that looks like it serves the same purpose.

INSERT INTO `item_script_names` (`id`, `ScriptName`) VALUES(34971, 'warsong_flare_gun');
At first, I tried it without adding an entry to item_template at all. That didn’t work. This isn’t a new item, so I found a db2 file editor that allowed me to see the properties of the item in both Item.db2 and Item-sparse.db2. I cross-referenced that with the structs for ItemEntry and ItemSparseEntry in src/server/game/DataStores/DB2Structure.h.

-- Note that the Item-sparse.db2 file shows the spell in the 4th spell id column, but the first one seemed more logical. INSERT INTO `item_template`(`entry`,`class`,`subclass`,`name`,`displayid`, `Quality`,`Flags`,`delay`,`spellid_1`,`spellcategory_1`, `spellcategorycooldown_1`,`bonding` ) VALUES( 34971, -- Warsong Flare Gun 12, -- class : 12 = Quest 0, -- subclass : always 0 for quest items "Warsong Flare Gun", 39435, -- displayid 1, -- quality : 1 = common 64, -- Flags : 64 = "UNK2" (read from Item-sparse.db2) 0, -- delay : 0 (read from Item-sparse.db2) differs from column default (1000) 45958, -- spellid_1 : 45958 = [Signal Alliance] 1167, -- spellcategory_1 : ?? (read from Item-sparse.db2) 100000, -- spellcategorycooldown_1 : ?? (read from Item-sparse.db2) 4 -- "bonding" (binding) : 4 = Quest item (Item-sparse.db2 says -1) );
Please forgive the excessive comments. They help me remember stuff. The fields that I didn’t specify had reasonable defaults that either matched or made more sense than the equivalent fields in the db2 file.
I’ve made the necessary changes to CMakeLists.txt and ScriptLoader.cpp and I can confirm that my code is compiled when I watch the make output.

I’m using commit # 14d80d269dd7f9624983bc5a610e511f31ed33f0 of the 4.3.4 branch on Linux.

My database is TrinityCore_4.3.4_DB_Alpha commit # e07faffe7ade24cab124fc8dd1ad672673d1f2fa .

Thank you.

Wouldn’t the “use” of an item constitute as a spell? If so, might your changes be more appropriate for spell_item.cpp (or at least coded and added to the DB in the same manner)? Just a thought, since I added the support for Flask of Enhancement by creating a patch for that file and adding the association in the spell_script_names table.

–edit–

Afterthought, make sure you are associating the actual “use” spell number to the script. In my Flask of Enhancement patch, the flask is item 58149, but the association is for the spell being cast by the flask (spell 79637).

The item is supposed to cast a spell when it’s used. I think that that is happening the way its supposed to, but I can’t confirm that because it’s just one part of a series of events for a broken quest.

I thought that the next thing I might have to try is a spell script for the spell it’s casting. I’d be reluctant to change existing code until I’m more confident about what I’m doing, but I should be able to get the same result with a custom script and a class that extends SpellScriptLoader.

There is some question about associating the right spell. The .lookup command tells me there are two spells with the same name. If I cast them with .cast, one of them doesn’t do anything at all (as far as I can tell). The one I used is the one that shows in Item-sparse.db2, but I’ll try changing the spell association in item_spell_names and see what happens.

This still doesn’t tell me why my OnUse code isn’t happening. I would expect the OnUse triggers the casting of the spell and that using item_script_names.ScriptName with a class that extends ItemScript overrides whatever is in the core for the item.

I wish I could figure out exactly when, and under what conditions, OnUse is supposed to be called.

The next thing I’ll try is a spell script extending SpellScriptLoader.

Thank you.

Well, you’re basically needing code that ties in exactly like the items in src/server/scripts/Spells/spell_item.cpp

That file covers items that, when “used” (aka OnUse), perform an action (effectively casting a spell). I found this file by searching (via grep) for code that deals with the Flask of the North. I honestly don’t know what the difference is with the item scripts versus spell scripts, but I’m guessing that you need to move your changes to a spell script module. You seem more than capable of making that change to see if it works.