Wednesday, 21 December 2011

SharedObject file location

One thing that comes up from time to time, that I learned while making Bug Tunnel Defense, is how to control where Flash saves the settings for the game (assuming the game has settings). This is important as the default location depends on the path to the file on the server and the name of the file on the sever. Although this doesn't usually change it can when the file is moved as it's re-uploaded or for security reasons.

To code I used looks like this:

try {
    so = SharedObject.getLocal("/", "BTD2/002");
catch(error:Error) {
    trace ("unable to access shared object " + strSOName + 
            ", error " + + ", saving disabled");
    bFile = bOK = false;

The key is the parameter "BTD2/002". This specifies both the path, "BTD2", and the filename "002", separated by a slash. If the path is unique (i.e. if you choose something non-obvious) only files for your game will be in that folder, so you can use whatever file names you like. I tend to use numeric ones so I can increment the number if I break and need to recreate the file format.

As the save location is independent of the game name and path to it this also opens up another possibility: more than one game could access the same file. One use might be for a game sequel to use settings from a previous game, such as a custom character or game progress.

The rest of the code is a try/catch block, about the only one in Bug Tunnel Defense, so I could set a flag to disable saving if the call failed. This might happen if the game was played on a machine where the user has no access to the file system, or if the disk were full.

No comments:

Post a Comment