I have my web based rpg game already created. Inventory management, equipment system, character stats, skill tree, and etc. It's almost complete.

For the game world, I am creating a top-down rpg character movement and combat. With point and click. You click on a part of the map (in this case, it's a image, and your character .png image transitions to that spot). It's with Javascript and PHP. I'm using a PHP Websocket server. I have it where players can only move up to 200 pixels per each click. Each click is essentially a packet being sent to my gameserver. Then the gameserver validates if it's a valid input (1235x1235) format, and spits it out to other players in that specific game. (So it's a consistent world/map). This works perfect.

My question is: If I want to incorporate a monster that has AI and starts to attack the character... I can do that with javascript easily with setInterval or setTimeout functions (to make the mob move and then attack the character). But how do I do that serverside? Because a user could just press F12 and edit the javascript to stop the mob from attacking. But, I need the mob to attack the player every xxx milliseconds serverside as well. How is this done with PHP? I feel like I need some type of heartbeat going on where it's a constant check between client and server every xxx seconds? I might be going off on the deep end and I'll stop talking lol. But I hope you get the jist of it. Any ideas are greatly appreciated.

Another thought: Think of Diablo 2 or Path of Exile, and using a hack that just stops mobs from moving towards your character and attacking it. Is that just an illusion? And the "auto attacking" is done serverside regardless if you've stopped that mob on the client, right? If so, how to accomplisgh this with a PHP Websocket server, and some JS? I'm not looking for code, just an application theory or design process would help! :)

P.S. It's either I go this route, or the game becomes a simple real time strategy pokemon style combat. Which I really, really don't want...

  • 1
    \$\begingroup\$ You don't. At least not with PHP. If its a JavaScript game, google for JavaScript sockets and write a Server in NodeJS or some other language you like. \$\endgroup\$
    – tkausl
    Commented Feb 26, 2015 at 4:33
  • \$\begingroup\$ Well, it only uses javascript to communicate with the php websocket server with packet senders. And ofcourse javascript to update and move the character around the map. So I guess in essence, it's, a JavaScript game. Hmm.. How would it be done with nodejs though? Anything that is written in javascript can be done in PHP though, right? I don't know if it's worth it to re-write my gameserver into nodejs. I mean, it works and I am more comfortable with PHP for sure. Then again, nodeJS has the setTimeout and setInterval functions that would be very useful for monster AI. I guess? Hmm..I'm lost. \$\endgroup\$ Commented Feb 26, 2015 at 4:39
  • \$\begingroup\$ Well, if you're using a socket-server already, thats okay. But PHP isn't the best language for this (horrible to work with threads). I would do it in Java or C(++) but thats just my preference, it should also work in PHP if you are comfortable with it, you just need to implement a tick-loop, which updates everything on the world. As far as i understand you, the client does all the work and the server just keeps track of the state, thats the wrong way. The server should do all the work, mob movements, attacks and so on and the client just has to show what the server tells it to show. \$\endgroup\$
    – tkausl
    Commented Feb 26, 2015 at 5:42
  • \$\begingroup\$ Oh Yeah tkausl, the game client is really there just for the client side display shenanigans. I'm definitely still using the gameserver for everything. That is my problem now though, I'm trying to use the game server for when the mobs are attacking, but just don't know how. You said a tick-loop. Is this possible with a php socket server? I think we're getting close. Cause I would need to tick the attacks that the mob did to my character ON THE game server, then display it to the client.. Right? (Until the character is dead ofc). That's what I'm thinking anyways. Hmm... We're getting closer. \$\endgroup\$ Commented Feb 26, 2015 at 5:48
  • \$\begingroup\$ Yes, exactly, thats what your server has to do. I don't know if it is easy in php, but it should be possible somehow. This is essentially how it works: Your server starts to tick all entities, this means, run their logic, which involves walking around or attacking if there is someone in range, removing dead entities, spawn new entities, maybe regenerate some health of all online players, and then (or even before the update) handle input, which means, if the player attacks, the server gets the attack-request, it checks if its even possible, \$\endgroup\$
    – tkausl
    Commented Feb 26, 2015 at 5:56

1 Answer 1


check out http://browserquest.mozilla.org/ as a reference model. it uses node for the backend.

They use setTimeout on server side classes e.g character, mob, player. Messages are sent client-server (move, attack etc) and server-client (move,spawn,damage etc). There are client and server side versions of classes for major entities in the game world. Numeric constants are used to keep messages light.

Source: https://github.com/mozilla/BrowserQuest


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .