For the turn-based browser Flash RPG I'm working on, I'm using url-encoded query stings to pass data from PHP to Flash. Basically, Flash posts a request to PHP, then PHP pulls some data from a MySQL database, does some calculations and echos something like "health=100&mana=50&strength=10" which Flash reads. It seems to work fairly well, but I'm wondering if there is a better way to do it? As I go along, the number of character stats is increasing, and after inventory slots, it will probably be around 100 variables. All that only needs to be pulled from the MySQL database through PHP once at the beginning though, with much smaller queries during gameplay.
-
1\$\begingroup\$ You know flash is basically dead? It will be discontinued in 2020 \$\endgroup\$– BálintCommented Mar 14, 2018 at 11:51
-
\$\begingroup\$ Yes, but it's the only thing I'm really familiar with. \$\endgroup\$– BladePointCommented Mar 14, 2018 at 17:28
-
3\$\begingroup\$ Great reason, to learn something new \$\endgroup\$– BálintCommented Mar 14, 2018 at 17:35
1 Answer
What you have here is basically a RESTful webservice API. The client communicates with the backend using HTTP requests with the request parameters in the query string. The backend then answers the request with the requested data in the body of the response. Using the query-string syntax for the response is unusual. Most people would use something like JSON or XML, but it's not a completely unreasonable solution.
Using RESful webservices is a very common architecture choice for client-server application. The main advantage is that it is not technology-specific. You can consume it with client applications implemented in any technology which can do HTTP requests. This is especially important in a case like yours where you are working with an obsolete technology like Flash. Down the line you will be forced to abandon it due to lack of browser support. But as long as you are using web services to communicate with your backend, you will very likely be able to keep using them with your new game client.
In the context of game development, there are however two drawbacks:
It usually sends text data. You might be able to save a lot of bandwidth when you use some binary encoding.
health=100&mana=50&strength=10
uses 30 bytes of text to communicate 3 integers. You could do that with 12 bytes if you just send 3 32-bit binary numbers. Even less if 16 bit or 8 bit are sufficient in your case (8 bit would be enough in this particular example, but I don't want to assume you won't ever have more than 256 in any of these attributes). And this doesn't even account for the HTTP header which will be sent in both directions with every message and can be several hundred bytes.Note that webservices can return binary data. It's just a very uncommon thing to do. But don't let that stop you.
HTTP uses the TCP protocol. This is fine for slower-paced games. But it isn't suitable for action-oriented games where every millisecond of latency matters. When your game is one of these, it would be better to use UDP if possible.
Flash supports UDP, but only on the Adobe Air profile, not for the web player. But the usually recommended replacement for Flash web applications (HTML5 + Javascript) does not support UDP either. So when you want to use UDP, you will have to transit from a web application to a native game client.
-
\$\begingroup\$ If I could get my data from PHP in an array, that would save me some of bandwidth, but Flash's URLVariables wants it in name/value pairs. I can set the URLLoader.dataformat to receive the data in binary though. But I'm not sure how to get PHP to output my data in binary and then turn it back into something I can use in Flash. \$\endgroup\$ Commented Mar 14, 2018 at 20:39
-
\$\begingroup\$ @BladePoint That sounds more like a general programming question than a game development question. So it would be something more appropriate for Stackoverflow. But considering that you got a solution which works for now and that your new solution would be something which doesn't make much sense to port to your new game client I wouldn't really bother. \$\endgroup\$– PhilippCommented Mar 15, 2018 at 0:23