0

I am currently coding my offline game into an online game by use of node.js and socket.io

In my game, I use vectors from a library called p5.js to store position of player, collision related movement, etc.

However, the server side (a txt file called "server.js") does not have p5.js like the client, so I can't send information about the player's with vectors.

Here is my question: How could I make the server.js file have access to my p5.js library?

Note: Simply sending x and y values, and then using them to make a vector would be a difficult solution, as I would no longer be able to send a single array holding all the information of all players. Also, enemies, food, trail positions, and much more also depend on vectors. Coverting all of these would be difficult to code.

Canatron
  • 85
  • 11

1 Answers1

1

What you want to do is simple, but it won't feel simple until you fully understand all the parts involved, and for that I'm afraid it's going to take you at least a few months given your current level.

How could I make the server.js file have access to my p5.js library?

Is your p5.js a browser-only library, or you can import it as a module in your server? If it's the second option, all you have to do for your server to access it is:

const p5 = require('p5.js');

Keep in mind that:

  • Server should handle the position, movement, and actions of players so they can't cheat.
  • The client should be just a visual display of the information coming from the server, plus sending the player key inputs to the server.

So unless you want to make client side prediction and entity interpolation, which I doubt because you are starting out, keep it simple. No need to share libraries between client and server yet.

sidewinder
  • 682
  • 4
  • 9
  • No, p5 is just a client side library it seems, I want to create a vector inside the server.js file to give to the clients, do you think there would be any way of doing that? Or would I have to create the vector from the client – Canatron Jun 10 '17 at 19:55
  • Try this one: https://www.npmjs.com/package/vectors . Do "npm install vectors" from the command line and then require it as explained in the website. – sidewinder Jun 10 '17 at 19:59
  • Thanks for searching that up for me, however, these vectors are obviously different than the ones in my library, so in order to incorporate these into my game, I would have to replace my current system with these new vectors, which would also be just as much work. It seems I will just send the x and y instead, as it seems like the way of least work. – Canatron Jun 10 '17 at 20:04
  • Well, if you just want to make a small game and make it fast over anything else, it's an option. You can do the calculations in the client, send them to the server, and make the server stream the position to the other player. **I would no longer be able to send a single array holding all the information of all players.** Why not? – sidewinder Jun 10 '17 at 20:08
  • whenever a player joins the game, I put all their stats and info in an object, and all of those objects (players) into an array. These player objects include a "pos" value that stores their position in a vector, however, because I cant use a p5.vector, I have to store them in two different variables, both x and y. – Canatron Jun 10 '17 at 23:37
  • If i do the calculations in the client, then if the player leaves the tab, and the other players move around, it wouldn't account for them, and the players would be de-synchronized. – Canatron Jun 10 '17 at 23:46
  • Not really. Even if you switch tabs, Javascript does not stop running. The only thing that may not run properly is requestAnimationFrame/intervals. Read this: https://stackoverflow.com/questions/5927284/how-can-i-make-setinterval-also-work-when-a-tab-is-inactive-in-chrome – sidewinder Jun 11 '17 at 17:22
  • p5.js has a function called "draw" which loops 60 times a second, and is dependant on "request Animation Frame", The game loops also exist within that looping function, so therefore, the game DOES depend on window focus. (player will not regenerate health if player is off screen) – Canatron Jun 11 '17 at 17:26
  • The game logic shouldn't ever depend on window focus. You update health bar graphics inside requestAnimationFrame, but the health variable (ie player.HP = 100) is set on socket message, so as soon as you switch tabs back the health bar should be refreshed also because requestAnimationFrame gets called again. – sidewinder Jun 11 '17 at 17:32