Server Help

Bot Questions - String help

Anonymous - Fri Jul 16, 2004 9:44 pm
Post subject: String help
Ok for mervbot, I made a command that will say something when the play enters, and what is says is customizeable (ex. !message [message]).
But all it did was PM the player with a blank message. Here is my code>

In command.cpp:

if (c->check("message"))
{
String themessage = c->final;

if (c->checkParam(themessage))
{
message_enabled = true;

if (themessage.IsEmpty())
{
message_enabled = false;
}
}
}

And in spawn.ccp:

case EVENT_PlayerEntering:
{
Player *p = (Player*)event.p[0];
if(p->ship!=icon_cool.gif
{gotEvent(makePlayerShip(p,p->ship,icon_cool.gif);}

if(message_enabled)
{
sendPrivate(p, themessage);
}
}

And in spawn.h, I defined:

bool message_enabled;
String themessage;[/code]
Anonymous - Fri Jul 16, 2004 9:45 pm
Post subject:
Oops that got messed up a bit.
50% Packetloss - Fri Jul 16, 2004 10:00 pm
Post subject:
Code: Show/Hide

if (c->check("message"))
{
     if(*c->final != NULL)
     {
           if(c->checkParam("off"))
           {
                 enabled=false;
           }
           else
           {
                 enabled=true;
                 strncpy(message,c->final,256);
           }
     }
}

And in spawn.ccp:

case EVENT_PlayerEntering:
{
      Player *p = (Player*)event.p[0];

     if(enabled)
     {
          sendPrivate(p, message);
     }

}

And in spawn.h, I defined:
#include <string.h>
bool enabled;
char message[256];

D1st0rt - Sat Jul 17, 2004 8:42 am
Post subject:
Quote:
strncpy(c->final,256);

I must be missing something
Bak - Sat Jul 17, 2004 9:24 am
Post subject:
try changing, in commands.cpp:

if (c->check("message"))
{
String themessage = c->final;


to:

if (c->check("message"))
{
themessage = c->final;


It will always use the local variable whever it can, so if you redefine it locally it won't affect the global one, save you doing this.themessage
50% Packetloss - Sat Jul 17, 2004 1:36 pm
Post subject:
haters, lol, i corrected it. There is no need for a class, just use a regular char
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group