Impact server script question

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
ShawnSwander
Posts: 53
Joined: Thu May 17, 2012 12:31 am

Impact server script question

Post by ShawnSwander »

So I followed Hallsofvalhalla for some of my code on my game using impact

However, if I just spawn entities to my map in WM or using a client side script it will spawn them different for each client. I tried allowing only a single client to 'host' the mobs by assigning a booleon hosting varriable that in my update() if no one is hosting that player attempts to host and if there is more than 1 host to stop hosting and this worked okay though I had some bugs to work out of it. I liked that it takes some load off the server and I could always just have an admin account always running that hosts all the time. though this seems clunky...

It seems like I need to do all my npc entities control server side. My server script doesn't include impact can I just add that in at the top and make an update function? A little guidance would be appreciated Here is my server.js hallsofvallhalla called it app.js

Code: Select all

var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs')
 
app.listen(8080);

var playerlocation = 0;
var playerlist = [];

function handler (req, res) {
   fs.readFile(__dirname + '/index.html',
   function (err, data) {
      if(err){
         res.writeHead(500);
         return res.end('Error loading index.html');
      }
      res.writeHead(200);
      res.end(data);
   });
}

io.sockets.on('connection', function (socket) {

   socket.on('recievedata', function (positionx,positiony,currentanimation,gamename) {  
      socket.broadcast.emit('playermove', positionx,positiony,currentanimation,gamename);//trigger playmore to everyone function on index.html
   });

   socket.on('initializeplayer', function (newplayername,posx,posy) {
      socket.clientname = newplayername;
      playerlist.push(newplayername);
      io.sockets.emit('addplayer',playerlist,newplayername,posx,posy);
   });

   socket.on('disconnect', function(){
      delete playerlist[socket.clientname];
      for(var i in playerlist){
         if(playerlist[i] == socket.clientname){
            playerlist.splice(i, 1);
         }
      }
      if(playerlist[i] == socket.clientname){
         playerlist.splice(i, 1);
      }
      socket.broadcast.emit('message',socket.clientname);
      socket.broadcast.emit('netreplayer',playerlist);
   });
});
Post Reply

Return to “Beginner Help and Support”