SFML community forums
Help => Network => Topic started by: firepro20 on December 01, 2019, 05:32:22 pm
-
Is it possible to ensure shot is sent once to a client so it is not created twice or four times when multiple packets are sent?
if (selector.wait(sf::milliseconds(10.f))) { // not enough time for server to be created with 0.1f
// received something
if (selector.isReady(socket)) {
// Wait for a message
char in[128];
std::size_t received; // am I using this?
sf::IpAddress sender;
sf::Packet playerData; // playerdata1 //playerdata2
sf::Packet bulletData;
float playerXPosition;
float playerYPosition;
float clientXPosition;
float clientYPosition;
float clientBulletX;
float clientBulletY;
bool bulletShot;
// third packet
socket.receive(playerData, sender, port);
// first packet - interpolate .. create scenario as tutorial
//socket.receive(bulletData, sender, port);
if (playerData >> playerXPosition >> playerYPosition >> bulletShot >> clientBulletX >> clientBulletY) { // if you are able to read
clientXPosition = playerXPosition;
clientYPosition = playerYPosition;
player->setPosition(clientXPosition, clientYPosition);
if (bulletShot) {
// - might not need this
// if not already received
//bulletPtr->spawn(true); // once bullet spawned, should be taken care of both sides
//
bulletPtr->isAlive();
bulletPtr->setLocation(clientXPosition - 13.f, clientYPosition - 24.f);
//bulletPtr->getSprite().move(0.f, -20);
//if (!bulletShot)
//{
// std::cout << "Bullet should be dead";
// bulletPtr->kill();
//}
//else
// clientbulletx and clientbullety
// client window handled from both client and server what to do if bullet is alive
}
if (bulletShot && bulletShot!=NULL) {
std::cout << "Satisfying both conditions" << std::endl;
bulletPtr->kill();
}
//else {
// bulletPtr->spawn(false);
//}
}
-
If you need a reliable protocol, then you'd be better of with TCP, otherwise you'll have to build some protocol on top of UDP yourself (or use a library) which will ACK (or similar) received messages etc