I’m trying to conceive of a way that a game state can be saved and restored for a Unity browser game. It’s conceivable that there may be kilobytes worth of data to save and load.
Internet cookies don’t seem to be an ideal solution for large chunks of data. They seem best for saving and loading a few small snippets as text. I imagine there may be limits to the size of a cookie entry, but I’m not sure. I suspect that such data would need to make a round-trip to/from the server anyway.
We’re not opposed to storing data on a web server for each player, in a particular folder, based on player user name. However the WWW class seems good for downloading, but I don’t see that there is some sort of FTP support for uploading (anonymous or password).
Developing a web server program to interact with the web player game for purposes of saving and loading gamestates is a bit daunting and out of scope. Simple hosting plans probably can’t accomodate.
Anyway I’d appreciate any suggestions on getting over the major obstacles of implementing saving/loading large chunks of data in a web player game. Thanks.
Cookies
These are workable but not the greatest as they’re volatile and you won’t know for sure whether the user (a) allows them, or (b) lets them persist between visits. Note that cookies require no client-server communication, they’re just chunks of text stored locally that the browser loads/reads/saves.
Unity’s PlayerPrefs Class
This works great as it lets you store persistent data on the local machine that won’t be cleared like browser cookies. The downside is that they’re local only, if the user logs in to your content from another machine they won’t have their saved data.
Server Side Database Storage
This is likely the most ideal solution as it lets you store persistent user data on the server which is available no matter where the user logs in from. Nothing FTP is required here and most hosting providers have support for MySQL, from there you just need them to enable something like PHP/ColdFusion/etc.
Really, those are your choices so pick one that suits you and go from there. And honestly, setting up a simple PHP/MySQL type configuration isn’t really all that daunting as again, most hosting providers have this in some sort of cookie-cutter form (and there’s a high-scores script available off the wiki!).
Does it work in the browser? I did a quick scan and see that it relies (apparently, correct me if I’m wrong) on System.IO and StreamWriter which shouldn’t work in the browser.
Well Im not 100% sure, but I dont think that its System.IO that is “banned” e.g. Im using MemoryStream and StreamReader in a webplayer without any problems (but they dont do any real IO either =) ).
Thanks for all the good advice. Setting up a server-side solution has some appeal, and would force me to learn some valuable skills.
Let’s assume the hosting provider offers a database, and PHP (as Lunarpages does). I wonder what sort of efforts/skillsets (in general) are involved in setting up the server side.
What would be involved in the Unity side, to interact with the server? I suppose I can learn much from Unity docs, but what specifics are involved that may not be covered in Unity docs?
Pointers to get me started would be much appreciated. The biggest gap in my skills is coding for the web.
Thanks for pointing me to that wiki! It’s a very good intro.
Would that sort of approach be suitable if there are a few megabytes of information involved?
For example, the database stores account information by user name (password-protected). It’s sufficient to store a single saved game per account (could be one or two megs).
It seems that a megabyte worth of various information in the saved game may be a bit much for this approach, or maybe not. I’m such a newbie at web-related development, but I’m willing to learn. Are there other approaches for bulk upload/download to/from a MySQL database, via the WWW class or other?
A few megs is an awful lot for a game state. You might want to rethink what you’re storing and in what way. Say you wanted to store the positions of a thousand objects, that should only amount to 40k worth of data if you do it correctly.
Still, a meg for a database record is entirely doable. Probably not for a single field unless the database supports BLOB’s. Also depending on the user’s connection speed, you might end up running into browser client timeouts if say they’re on a dialup connection.
I’m also in the process of looking into which web service to use and how to scale databases… a few thoughts after doing a bit of research.
AMF seems to be a great binary solution for sending quite a bit of data across server → client and has been optimized especially for games like flash.
Google Protocol Buffers, is another solution similar to xml rpc but about 10x faster, this is what Google uses in its on network so im sure its quite optimized, this requires mono 2.4 or higher which will be for 3.0 I think.
Soap/Rest/Json/XML - I just have a feeling of course no data yet, that this will be a bottle neck for people playing unity games over several thousand to millions of people requesting data.
Before developing the system I would like to hear from people who are doing this or working on this system before going down the wrong road… the AMF system seems like the most compact…
I would like to know also how to go about doing user-generated content in a game. Let’s say the user can use editors in the game to create new objects, combinations of objects, game levels etc… these then need to be put somewhere central that can be accessed by everyone, ie upload it to some server, make it downloadable by other people. It could be running in a web browser or it could be running as a desktop app.
The question then is how to upload this data and how/where to store it, how to also store/cache it locally on the user’s machine (with presumably unlimited storage), and how to then download other people’s creations in a kind of in-game store? At it’s simplest it’s just a matter of save local content, upload local content, download remote content and save it… but I’m not sure how Unity wants you to do that??? I don’t know if it really needs an sql database versus just a bunch of files with some kind of index? Or like upload to some ftp folder and be able to download anything from that folder, or via just a regular url download? Of course, doing things like user accounts and security and purchases/virtual currency is harder.
It is possible to do this, say with PHP’s Filesystem functions, as long as your host gives you the right permissions to do that. However, it suffers from concurrency problems (ie, when two or more users write at once) and other issues, which is why the database approach is more common.
Hmm ok, thanks. Just wondering what the best way to do shared user-generated content would be - ie oldschool heres a webpage to download a bunch of files or something more advanced.
“Best” is highly subjective and based on what you may or may not know already. For me the “best” would easily be PHP because I know PHP quite well (not a pro but well enough) and thus I could implement it successfully. Now, the more seasoned developers that do such things might run in here and offer arguments about Ruby on Rails, PHP or whatever, and that may sway the “best” decision for them or others.
So is PHP capable of doing what you’re after? Yes. Will you need to resort to database usage? Quite likely, but perhaps you can do it using the stuff Andy mentioned above (although I think that involving a database won’t be too tough and is likely to provide more flexibility/better results long term).
Hmm ok, thanks for the insight sir. I think you are probably right about php and the database, these are areas that I need to learn new things so we’ll see how it goes.