Worklog for Sanctus

Casual Game Framework

Return to Worklogs

Account Management(Posted 2011-03-11)
After asking on the forum if anyone would buy some of my modules I decided to start working on some more professional ones. the main reason for development is that they will help me and then that I might sell them as a pack.

As a first entry I bring you the Account Manager.
Most casual games support a feature where multiple accounts can be used in a game with different saves.
Last night I've implemented this in a way that it's easy to use and yet flexible.
Here is an example of it:
Type TMyAccount extends TAccount
  Field highScore:int
  Method Save(stream:TStream)
    WriteInt(stream,len(name))
    WriteString(stream,name)
    WriteInt(stream,highscore)
  End Method

  Method Load(stream:TStream)
    name = ReadString(stream,ReadInt(stream))
    highscore = ReadInt(stream)
  End Method
End Type

Function CreateAccount:TAccount()
  Return New MyAccount
End Function

Global accountManager:TAccountManager = CreateAccountManager("MyGame")
accountManager.Load()
accountManager.AddAccount("Player1")
accountManager.AddAccount("Player2")

Global currentAccount:TMyAccount = accountManager.GetCurrentAccount()
currentAccount.highScore = 1000

accountManager.Save()


After any alteration directly on the accountManager like adding accounts,removing them, setting the current one everything is saved so you don't have to.
If however you modify something in the account then you have to call accountManager.Save() or accountManager.SaveAccount(name)

At first glance it may look like a lot of code but that's just so you can customize the account data.
Another ideea would be to use metadata and reflection so that save and load methods don't have to be written but right now I feel like this provides more flexibility.

What it does?
First of all this depends on the operating system. On windows(the only platform I have right now) depending on the version it will save on the application data path \ gamename \
First it creates a file named accounts.acc with data about all the accounts. Next it creates folders for each account there. When writing the save/load methods on the account derivative you may use accountDir and accountFilePath for some more data like save game files.

Tonight I'll try to some more tests to see that everything works as intended and start work on the next module (to be announced)

May the sky never fall on your head