Page 1 of 1

converting text from EditBox

Posted: Fri Nov 10, 2006 4:24 pm
by Watcher
Hi I have a problem with converting text from EditBox to const wchar_t* . It seems to me, that const wchar_t* text = editbox->getText() doesn´t work correctly.

I´m trying to make a chat for my multiplayer game (using RakNet). I want to send text from EditBox to ListBox.
If I set text like text = L"hello" and send it it works fine but if I use text = EditBox->getText and send it text come absolutly jammed. But if I save text from edit box to const wchar_t* text and add line to listbox with value of text on same computer, it shows correct. Im really confused.

Posted: Fri Nov 10, 2006 10:17 pm
by hybrid
But of course you can't assign a const pointer a value. What sense would the const make then?

Posted: Sat Nov 11, 2006 8:42 am
by Watcher
Oh thanks. But can you tell me please how to cenver const wchar_t* to wchar_t*?

Posted: Sat Nov 11, 2006 2:12 pm
by Midnight
why does it need converted? for raknet?

try the irrlicht wiki it has information about conversions like this and so does google although a bit obscure like anything not so popular that somebody will link or email you the answers.

step one in game design is not to beg for answers on a small community forum but to learn programming usually in a school I know from personal experience even the brightest of people will have a hard time with self education especially when they are lazy like me lol.

Posted: Sat Nov 11, 2006 3:23 pm
by Watcher
Well I think than my main problem is in sending wchar_t to server and back. Please, have a look at my code.

client side

if you press enter
wchar_t temptext[256];
wcscpy(temptext, editbox->getText());
listbox->addItem(temptext);
myConnection.SendZprava(temptext);

sending text to server
void SendZprava(wchar_t text[256])
{
RakNet::BitStream dataStream;

dataStream.Write(PACKET_ID_MESSAGE);


int delka = wcslen(text);
printf("%d\n", delka);
dataStream.Write(text[256]);
client->Send(&dataStream, HIGH_PRIORITY, RELIABLE_ORDERED, 0);
}
read wchar_t
case PACKET_ID_MESSAGE:
wchar_t text[256];
dataStream.Read(text[256]);
listbox->addItem(text);
break;
server side

case PACKET_ID_MESSAGE:
wchar_t text[256];
dataStream.Read(text);
sendzprava(server, p->playerId, text);

break;
////////////////////////////////////////////
void sendzprava(RakServerInterface * server, PlayerID clientToExclude, wchar_t text[256])
{
RakNet::BitStream dataStream;

dataStream.Write(PACKET_ID_MESSAGE);


dataStream.Write(text);
server->Send(&dataStream, HIGH_PRIORITY, RELIABLE_ORDERED, 0, clientToExclude, true);

I´ve got inspiration in this tutorial http://www.daveandrews.org/articles/irrlicht_raknet/

Posted: Sat Nov 11, 2006 6:30 pm
by vitek
Dude, this is a C++ problem, not an Irrlicht or RakNet problem. Your code is wrong. I don't know RakNet, but I can tell you what you are doing won't work...

First off, you are only writing the 256th character to the buffer, which is actually one past the end of the buffer. Second, you should probably not take a 256 character buffer, you should take an arbitrary length buffer. If you insist on taking 256 characters, you should not use wcscpy; use wcsncpy instead.

Code: Select all

  // these are wrong
  dataStream.Read(text[256]); 
  dataStream.Read(text); 

  dataStream.Write(text[256]);
  dataStream.Write(text); 

  // you should be doing this...
  dataStream.Write(text, 256 * sizeof(wchar_t));
  dataStream.Read(text, 256 * sizeof(wchar_t));
It is going to be inefficient to send 256 characters all of the time, especially if your messages are relatively short. As I mentioned above, you should send just the number of characters that the user typed, and maybe cap that at 256 characters in the edit box. It will avoid sending large amounts unused data. The other thing to consider is using the string compressor.

Have a look at the section of the RakNet documentation titled Writing Strings. It might help you.

Travis