Network Game Dev Diary: Chatroom

Network Game Dev Diary: Chatroom

Apr 6, 2019. | By: Lewis Bond
1 min

Chatroom

When connected to the server the players can send each other messages. This is done using the packet system mentioned in a previous post. Special streamming operaters had to be set up for sending strings across the network compared to other data like ints or chars. A string is a pointer to an array of chars if you tried to stream it in using the normal way of streamming thne it would give you memory location of the pointer and what would of been outputted was a load of nonsense that you couldn’t understand or nothing at all.

// normal packet
    packet_data.insert(packet_data.end(), as_char, as_char + size);
    
// string packet
    packet_data.insert(packet_data.end(), string.data(), string.data() + string_length);

As you can see the string is passed in but we have to do string.data() in order to get the correct data. We can’t do this in the normal packet as it uses a template class so it allows for anything and only the string is used that requires .data() after the variable has been passed in. Since we can’t check what type of variable has been passed in with T, it is much esaier to have a separate functions for sending strings.

The user talking to another player


PREVIOUS BLOG POST NEXT BLOG POST

About

Those who can do and those who can't teach games design.

Our Bunker



United Kingdom.