Sign in to follow this  
cseibert

Phaser as Multiplayer Server

Recommended Posts

I'm trying to create a multiplayer game using phaser3 for both the client and server.  When I attempt to run phaser3 as a node process, it crashes because "window is not defined".  Is there an easy way to run the phaser3 engine as a "server" mode where it doesn't try to render anything?

I don't care to rewrite my own tilemap and physics engine just to have an authoritative server be able to check if players are out of bounds, etc.

Share this post


Link to post
Share on other sites

This is not actually how you would create a multiplayer game. You would not run Phaser as-is on the server, since it is doing all kinds of things that you don't want done on the server, such as managing sprite images, drawing the screen, etc. It is crashing with a "window is not defined" error because Phaser is looking for the window object to do all of those things. You will also need to work with websockets, which is not something Phaser will do out-of-the-box.

Rather, the general idea is that you use Phaser on the client-side to display the game and share as much of the code as possible through re-use in your server, but the server will be a distinct application that is different from the client. There will be times when the server needs to do things in somewhat of a different way than your client logic because the server has a much wider view of the game and it has to be more efficient. For starters, I would highly recommend reading Gabriel Gambetta's excellent series that outlines the basic concepts of making an authoritative server. It doesn't go over any code, but instead goes through the strategies to handle lag and other obstacles in multiplayer development. I would also recommend the Build New Games real-time multiplayer tutorial. That actually walks through some code for the user to see two different players moving around. As far as using Phaser 3 goes, you could check out the newer tutorial from Zenva. However, I would just consider that a starting point because there are some serious holes and missing parts from the code (such as client-side prediction, interpolation, etc.). I believe it would fall apart when real world latency enters the picture. But it does demonstrate pretty well how to get everything set up with Phaser 3 and could be a good starting point. Then there is also a good Phaser 2 tutorial and github found here.

Hope this helps!

Share this post


Link to post
Share on other sites

Indeed, your back end should be dealing in game abstractions rather than phaser data types etc. It would also be very bad for efficiency having those data types passed back and forth between the client, when simple numbers and strings could do. It's a lot like creating save game files and serializing data in that sense. When you save a game, you don't save all the objects and state and literally everything  - you save simple data types that act as signposts for rebuilding your types/objects/state etc. 

Share this post


Link to post
Share on other sites

It would be nice to have arcade physics in an external module, to be able to use it on a node server. I tried with matter-js but it doesn't match my need. If someone did a multiplayer game using Phaser, I would like to know how did he managed server-side physics.

Share this post


Link to post
Share on other sites

If I was making a multiplayer game with physics, I would either used standalone one (for something complex like full rigid body), or custom written simple one. Using something integrated like Arcade would bring pointless overhead.

That said, if you are so inclined, it probably wouldn't be too much work to decouple Arcade code from Phaser and use that standalone. That's one of the main benefits of using open source libraries.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.