Jump to content

Announcer Script


Guest xeross

Recommended Posts

I am working on an announcer script and i have the following.

void World::SendBroadcast()
{
   std::string msg;
   int nextid;

   if(nextid != 0)
   {
       QueryResult *result = WorldDatabase.PQuery("SELECT `text`, `next` FROM `autobroadcast` WHERE `id` = %u", nextid);
   }
   else
   {
       QueryResult *result = WorldDatabase.PQuery("SELECT `text`, `next` FROM `autobroadcast` ORDER BY RAND() LIMIT 1");
   }

   if(!result)
       return;
   nextid = result->Fetch()[1].GetInt();
   msg = result->Fetch()[0].GetString();
   delete result;

   static uint32 abcenter = 0;
   abcenter = sConfig.GetIntDefault("AutoBroadcast.Center", 0);
   if(abcenter == 0)
   {
       sWorld.SendWorldText(LANG_AUTO_BROADCAST, msg.c_str());

       sLog.outString("AutoBroadcast: '%s'",msg.c_str());
   }
   if(abcenter == 1)
   {
       WorldPacket data(SMSG_NOTIFICATION, (msg.size()+1));
       data << msg;
       sWorld.SendGlobalMessage(&data);

       sLog.outString("AutoBroadcast: '%s'",msg.c_str());
   }
   if(abcenter == 2)
   {
       sWorld.SendWorldText(LANG_AUTO_BROADCAST, msg.c_str());

       WorldPacket data(SMSG_NOTIFICATION, (msg.size()+1));
       data << msg;
       sWorld.SendGlobalMessage(&data);

       sLog.outString("AutoBroadcast: '%s'",msg.c_str());
   }
}

All calls timers etc are configured properly, this is based on an announcer i found.

However i get the following errors

2>..\\..\\src\\game\\World.cpp(2629) : error C2065: 'result' : undeclared identifier
2>..\\..\\src\\game\\World.cpp(2631) : error C2065: 'result' : undeclared identifier
2>..\\..\\src\\game\\World.cpp(2631) : error C2227: left of '->Fetch' must point to class/struct/union/generic type
2>        type is ''unknown-type''
2>..\\..\\src\\game\\World.cpp(2631) : error C2228: left of '.GetInt' must have class/struct/union
2>..\\..\\src\\game\\World.cpp(2632) : error C2065: 'result' : undeclared identifier
2>..\\..\\src\\game\\World.cpp(2632) : error C2227: left of '->Fetch' must point to class/struct/union/generic type
2>        type is ''unknown-type''
2>..\\..\\src\\game\\World.cpp(2632) : error C2228: left of '.GetString' must have class/struct/union
2>..\\..\\src\\game\\World.cpp(2633) : error C2065: 'result' : undeclared identifier
2>..\\..\\src\\game\\World.cpp(2633) : error C2541: 'delete' : cannot delete objects that are not pointers

I think i did something wrong with nextit, it needs to be saved for the next function call so it gets the correct message from mysql, how would i accomplish this ?

Thanks for your time, Xeross

Link to comment
Share on other sites

The problem is here:

if(nextid != 0)
{
   QueryResult *result = WorldDatabase.PQuery("SELECT `text`, `next` FROM `autobroadcast` WHERE `id` = %u", nextid);
}
else
{
   QueryResult *result = WorldDatabase.PQuery("SELECT `text`, `next` FROM `autobroadcast` ORDER BY RAND() LIMIT 1");
}

When you declare the variable result in your code, its scope is limited to that block. That is, it only exists in that block.

You can solve your problem with a code like this:

QueryResult *result;
if(nextid != 0)
{
   result = WorldDatabase.PQuery("SELECT `text`, `next` FROM `autobroadcast` WHERE `id` = %u", nextid);
}
else
{
   result = WorldDatabase.PQuery("SELECT `text`, `next` FROM `autobroadcast` ORDER BY RAND() LIMIT 1");
}

Link to comment
Share on other sites

i now have

void World::SendBroadcast()
{
   std::string msg;
   int nextid;

   QueryResult *result;
   if(nextid != 0)
   {
       QueryResult *result = WorldDatabase.PQuery("SELECT `text`, `next` FROM `autobroadcast` WHERE `id` = %u", nextid);
   }
   else
   {
       QueryResult *result = WorldDatabase.PQuery("SELECT `text`, `next` FROM `autobroadcast` ORDER BY RAND() LIMIT 1");
   }

   if(!result)
       return;

   nextid = result->Fetch()[1].GetUInt8();
   msg = result->Fetch()[0].GetString();
   delete result;

   static uint32 abcenter = 0;
   abcenter = sConfig.GetIntDefault("AutoBroadcast.Center", 0);
   if(abcenter == 0)
   {
       sWorld.SendWorldText(LANG_AUTO_BROADCAST, msg.c_str());

       sLog.outString("AutoBroadcast: '%s'",msg.c_str());
   }
   if(abcenter == 1)
   {
       WorldPacket data(SMSG_NOTIFICATION, (msg.size()+1));
       data << msg;
       sWorld.SendGlobalMessage(&data);

       sLog.outString("AutoBroadcast: '%s'",msg.c_str());
   }
   if(abcenter == 2)
   {
       sWorld.SendWorldText(LANG_AUTO_BROADCAST, msg.c_str());

       WorldPacket data(SMSG_NOTIFICATION, (msg.size()+1));
       data << msg;
       sWorld.SendGlobalMessage(&data);

       sLog.outString("AutoBroadcast: '%s'",msg.c_str());
   }
}

But i wonder if nextid will be saved till the next function call so that the correct message will be selected

Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy Terms of Use