RakNet is a C++ networking engine to help you write multiplayer games. BitStream is a method for converting data such as variables into bytes to send out across the network.
In many cases you would send data with BitStream from one client to the server (or another client) like this:
and receive it like this:
Effectively when you send variables across BitStream like above, the & symbol tells the BitStream that you are sending a memory address (the literal location of where the data is stored in your PC’s memory) and pointing to the first byte in that address, with sizeof specifying how many bytes make up the data. This ensures it sends and receives the whole piece of data.
Unfortunately, you can’t send standard Strings in this way, so RakNet gave us their method, the RakString. It’s an alternative variable to std::String that you can send across BitStream and later convert back to std::String. It’s also 3x faster than std::String apparently. If you’ve already included the RakPeerInterface.h and BitStream.h, then you should create a new member variable in the header of your class for a RakString.
RakNet::RakString m_spritePath; // file path of the sprite
You can then simply send it like this:
You don’t need to cast it to a char* because RakString is native to RakNet. You can receive it the same way:
Then, before you use it, it is a good idea to convert it back to a normal string.
std::string temp = m_spritePath;
If you enjoyed reading, try my game Puzzledorf.