converting text from EditBox

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Watcher
Posts: 40
Joined: Sat Jul 01, 2006 9:44 am
Location: Czech republic

converting text from EditBox

Post 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.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

But of course you can't assign a const pointer a value. What sense would the const make then?
Watcher
Posts: 40
Joined: Sat Jul 01, 2006 9:44 am
Location: Czech republic

Post by Watcher »

Oh thanks. But can you tell me please how to cenver const wchar_t* to wchar_t*?
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post 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.
Watcher
Posts: 40
Joined: Sat Jul 01, 2006 9:44 am
Location: Czech republic

Post 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/
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post 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
Post Reply