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.