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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

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
Try this one: npmjs.com/package/vectors . Do "npm install vectors" from the command line and then require it as explained in the website.
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.
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?
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.