Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Steaming to a client, ensure shot fired is sent once using UDP  (Read 89 times)

0 Members and 1 Guest are viewing this topic.

firepro20

  • Newbie
  • *
  • Posts: 22
    • View Profile
Steaming to a client, ensure shot fired is sent once using UDP
« 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?

Quote
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);
            //}
            
         }

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 9388
    • View Profile
    • development blog
    • Email
Re: Steaming to a client, ensure shot fired is sent once using UDP
« Reply #1 on: December 02, 2019, 07:37:51 am »
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
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

 

anything