0

Currently, I have a game setup where if one player clicks on another in range, it is supposed to kill said player. However, I'm not sure how to target specific players on collision. The game is written in p5 as well as node.js to network it.

I have tried to assign a specific id to each client however, when a ton of clients send data at once, the receiving client does not know which id to target.

SERVER CODE...

function newConnection(socket){
  storeid = socket.id;
  idarr.push(storeid);
  io.to(storeid).emit('clientid',storeid);

  usernumber+=1;
  console.log('new connection: ' + storeid);
  console.log(idarr);

  socket.on('mouse', mouseMsg);


  socket.on('killdata', killMsg);


  function mouseMsg(data) {
    socket.broadcast.emit('mouse', data);

  }
  function killMsg(otherid) {
    io.to(otherid).emit('iskilled', killed);
  }

CLIENT CODE TO KILL OBJECT

    if(collided){
        death = true;
        socket.emit('killdata', otherid);
        collided = false;
      }
//otherid is the id of other clients taken from the server when the server sends data from other clients

Right now, the code written kills a player, but it kills a random player because the client can only receive an id one at a time and does not know which id to target.

Tyler Wong
  • 27
  • 4
  • can you share the code which initializes otherid? – user269867 Jun 07 '19 at 18:01
  • socket.on('mouse', // When we receive data function(data) { otherx = data.x; othery = data.y; otherdeath = data.d; othera = data.a; otherid = data.id; – Tyler Wong Jun 08 '19 at 23:18
  • quick note - you are initing it with ```data.id``` instead of ```storeid```. As per ```storeid = socket.id;``` the socket is identified by storeid. right? – user269867 Jun 10 '19 at 18:41
  • Yes it is but I figured it out in the end, I had to send all of the client data in an object so I ended up deleting a large portion of my code and starting again, works now though! Thanks for responding regardless :) – Tyler Wong Jun 12 '19 at 16:41

0 Answers0