What's new

[3.3.5] Gossip NPC Tutorial

dokuro

Veteran Member
Verified Member
31
2020
6
Location
Paris
In this tutorial we will see how to simply create a GOSSIP NPC in C ++.

The library

#include "ScriptMgr.h"
#include "ScriptedGossip.h"

We must not forget the ScriptedGossip.h library, available in the sources, src \ server \ game \ AI \ ScriptedAI \.
There are various useful functions:

AddGossipItemFor

SendGossipMenuFor

CloseGossipMenuFor

Creation

We start by creating the structure of our script (class, type, name, etc.).

class GossipTutroial : public CreatureScript
{
public:
GossipTutroial() : CreatureScript("GossipTutroial") { }


struct MyAI : public ScriptedAI
{
MyAI(Creature* m_creature) : ScriptedAI(m_creature) { }

bool GossipHello(Player* player) override
{
return OnGossipHello(player, me);
}

bool GossipSelect(Player* player, uint32 menuId, uint32 gossipListId) override
{
uint32 action = player->PlayerTalkClass->GetGossipOptionAction(gossipListId);
return OnGossipSelect(player, me, action);
}
};

CreatureAI* GetAI(Creature* creature) const override
{
return new MyAI(creature);
}
};


We then create our first function, the one we will see when talking to the PNJ.
We are going to use the following two functions, which we can find in the ScriptedGossip.h file:

AddGossipItemFor(Player* player, uint32 icon, std::string const& text, uint32 sender, uint32 action)
SendGossipMenuFor(Player* player, uint32 npcTextID, ObjectGuid const& guid)
static bool OnGossipHello(Player * player, Creature * creature)
{
AddGossipItemFor(player,GOSSIP_ICON_CHAT, "Hello World", GOSSIP_SENDER_MAIN, 1);
AddGossipItemFor(player,GOSSIP_ICON_BATTLE, "Good Bye", GOSSIP_SENDER_MAIN, 2);
SendGossipMenuFor(player, player->GetGossipTextId(creature), creature->GetGUID());
return true;
}


Calling the AddGossipItemFor function will allow us to display a text that will execute an action when we click on it. Remember to change the action number each time, so that the execution is different here 1 and 2.
Do not forget to use the SendGossipMenuFor function, otherwise the PNJ will not display anything.
In short, here is the result on the PNJ:

static bool OnGossipSelect(Player* player, Creature* creature , uint32 Action)
{
switch (Action)
{
case 1:
creature->TextEmote("Bienvenue sur Open-WOW", player);
CloseGossipMenuFor(player);
break;
case 2:
CloseGossipMenuFor(player);
break;
}
return true;
}

If we choose action 2, the gossip closes.
Calling the CloseGossipMenuFor function closes the gossip.

We are just missing the last line to call the script.

void AddSC_GossipTutroial()
{
new GossipTutroial();
}

We are missing more than the NPC to create:

SET
@Entry = 50000,
@Name = "GossipTutroial",
@ScriptName = "GossipTutroial";

INSERT INTO `creature_template` (`entry`, `modelid1`, `modelid2`, `name`, `subname`, `IconName`, `gossip_menu_id`, `minlevel`, `maxlevel`, `exp`, `faction`, `npcflag`, `scale`, `rank`, `dmgschool`, `baseattacktime`, `rangeattacktime`, `unit_class`, `unit_flags`, `type`, `type_flags`, `lootid`, `pickpocketloot`, `skinloot`, `AIName`, `MovementType`, `HoverHeight`, `RacialLeader`, `movementId`, `RegenHealth`, `mechanic_immune_mask`, `flags_extra`, `ScriptName`) VALUES
(@Entry, 21665, 0, @Name, '', NULL, 0, 80, 80, 2, 35, 1, 1, 0, 0, 2000, 0, 1, 0, 7, 138936390, 0, 0, 0, '', 0, 1, 0, 0, 1, 0, 0, @ScriptName);

If all goes well the final script looks like this:


#include "ScriptMgr.h"
#include "ScriptedGossip.h"

class GossipTutroial : public CreatureScript
{
public:
GossipTutroial() : CreatureScript("GossipTutroial") { }
static bool OnGossipHello(Player * player, Creature * creature)
{
AddGossipItemFor(player,GOSSIP_ICON_CHAT, "Hello World", GOSSIP_SENDER_MAIN, 1);
AddGossipItemFor(player,GOSSIP_ICON_BATTLE, "Good Bye", GOSSIP_SENDER_MAIN, 2);
SendGossipMenuFor(player, player->GetGossipTextId(creature), creature->GetGUID());
return true;
}

static bool OnGossipSelect(Player* player, Creature* creature , uint32 Action)
{
switch (Action)
{
case 1:
creature->TextEmote("Bienvenue sur Open-WOW", player);
CloseGossipMenuFor(player);
break;
case 2:
CloseGossipMenuFor(player);
break;
}
return true;
}

struct MyAI : public ScriptedAI
{
MyAI(Creature* m_creature) : ScriptedAI(m_creature) { }
bool GossipHello(Player* player) override
{
return OnGossipHello(player, me);
}
bool GossipSelect(Player* player, uint32 menuId, uint32 gossipListId) override
{
uint32 action = player->PlayerTalkClass->GetGossipOptionAction(gossipListId);
return OnGossipSelect(player, me, action);
}
};
CreatureAI* GetAI(Creature* creature) const override
{
return new MyAI(creature);
}
};

void AddSC_GossipTutroial()
{
new GossipTutroial();
}

ENjoy!! =)

Credit to : Killit5 from Open-Wow
 
Last edited by a moderator:
  • Like
Reactions: ExO
Top