3
\$\begingroup\$

I am creating a multiplayer JavaScript game with a PHP central server. Each user will be able to move around in a 3D world, with other users and NPCs. These NPCs will be controlled by the central server in PHP. Should I create one big world simulation PHP file that simulates all the NPCs in turn or a smaller controller that will be ran for each active NPC (on its own thread)? Each NPC has its own row on an SQL table and will be retrieved by the client to determine its position, state, etc.

Note: This is my first time creating a networked game, so if there is anything that I can do better, or a unique way of doing things that could work for this, feel free to comment

\$\endgroup\$
2
\$\begingroup\$

While I don't have much experience creating games with php/js I'd say that there are a few things you should consider.

First thing that popped out to me is that you wanted to save positions in an SQL database - this seems like a terrible idea. It would be incredibly slow, consider how many times a second a game loop would run, times the number of entities you have and you have an insane amount of sql queries and no real hope of performance.

Second, you'll have to consider the overhead of opening a new thread for each entity - likely, it will be faster to have one thread taking care of all the entities. Besides, what would be the upside of parallell updating compared to procedural?

But again, I'm no expert in php.

\$\endgroup\$
1
\$\begingroup\$

my first instinct when seeing this is that you are using all of the wrong technology. that is the big optimisation win to be had.

i am a bit old school so maybe these days you can get away with it, but networking games were never historically made in these ways. even TCP is considered too much overhead (it introduces a lot of lag for dropped packets, a lot of the time, e.g. with positions, you don't care so much because you get another update very quickly).

if you want to progress towards doing this professionally you should use C++ or a similar low level language with access to BSD sockets etc. although you can do the same with php for the server, i don't think you can with javascript from a browser (i am not expert though).

there is some excellent information on these classical approaches here:

http://gafferongames.com/networking-for-game-programmers/ http://beej.us/guide/bgnet/

i doubt you will ever get decent performance for these sorts of things without dropping to this level... and 'being able to send things over udp with sockets' is about what i would consider 'the basics'. so i have no idea what you read which gave you the impression otherwise.

\$\endgroup\$
  • \$\begingroup\$ you can still use sockets with php, and use the server as a go between for setting up p2p connections (i may have done this about a year ago for a project at work...) but it will depend on how your host as configured it as to, e.g. what ports you can use, and which bits of php you have access to. i'd highly recommend that as a path towards doing something more serious. doing the same in something like C++ is not too dissimilar, so it will stand you in good stead still - imo at least. i guess you might be in the same boat with a database? \$\endgroup\$ – jheriko Nov 5 '15 at 1:07
  • \$\begingroup\$ also, something iv;e done when building this sort of thing is to run the server on the same machine i debug the client on, this makes it a lot easier to debug them both together - you can always run the server somewhere else later. ... also... getting a general purpose server with a public ip isn't all that expensive - some providers will do it for $10-20 a month with severe restrictions even. \$\endgroup\$ – jheriko Nov 5 '15 at 1:09
  • \$\begingroup\$ I am still in high school with no reliable source of income yet. Even if this doesn't work out I can still use this to show off my skill in JS and PHP incase I want to get a programming job. And plus I would have to relearn C if I did that. \$\endgroup\$ – Grant Davis Nov 5 '15 at 1:40
  • \$\begingroup\$ And by the way, your information has really helped me, so thank you. \$\endgroup\$ – Grant Davis Nov 5 '15 at 1:41
  • 1
    \$\begingroup\$ a strong demo and knowing the right person was what got me in. you are doing the right things. best of luck. \$\endgroup\$ – jheriko Nov 5 '15 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.