Forum BigDB Nested DatabaseObjects

Discussion and help relating to the PlayerIO database solution, BigDB.

Nested DatabaseObjects

Postby azuanagames » October 9th, 2010, 5:35 pm

Hello,

I'm trying to create an achievement record on the PlayerObject. I know that I can use integers and set bits for the various achievements, but since you guys handle packing and what not, I figured I can just use nested objects as follows:

I get the current list of achievements from the database:
Code: Select all
      vAchievements = PlayerObject.GetObject("achievements");
      if (vAchievements == null) {
        vAchievements = new DatabaseObject();
      }


I grant an achievement as follows:
Code: Select all
      if (!vAchievements.Contains(achievement)) {
        vAchievements.Set(achievement, true);
      }


Later I save achievements as follows:
Code: Select all
      PlayerObject.Set("achievements", vAchievements);
      PlayerObject.Save();


But, I'm getting this error message:
Code: Select all
Cannot create circular references inside database objects


I don't understand the error since vAchievement is NOT the PlayerObject, how is this a circular reference?
azuanagames
 
Posts: 157
Joined: April 29th, 2010, 10:59 pm

Re: Nested DatabaseObjects

Postby Oliver » October 11th, 2010, 10:58 am

Hey,

I just tested this and it worked with no issues:

Code: Select all
public override void UserJoined(BasePlayer player) {
   var PlayerObject = player.PlayerObject;
   var vAchievements = PlayerObject.GetObject("achievements");
   if(vAchievements == null) {
      vAchievements = new DatabaseObject();
   }
   var achievement = "hepo";
   if(!vAchievements.Contains(achievement)) {
      vAchievements.Set(achievement, true);
   }
   PlayerObject.Set("achievements", vAchievements);
   PlayerObject.Save();
}


So my guess is that you're somehow sharing the sub object "vAchievements" between multiple PlayerObjects which would cause an error.

Regardless, it would be much easier to simply do this:
Code: Select all
   // whenever you want to give an achivement
   var achivementName="isawesome";
   player.PlayerObject.Set("achievements."+achivementName,true);

   // when you're done
   player.PlayerObject.Save();


That'll create the subobject "achievements" when you need it.

You can use this to check if the achievement is there:

Code: Select all
   // to read an achievement
   player.PlayerObject.GetBool("achievements."+achivementName);


One last note: It will take up less space if you use a single integer and with one bit for each achievement, but it will be much harder to maintain in the long run. I'd go with nested objects if it was me :-)

Best,
Oliver
User avatar
Oliver
.IO
 
Posts: 1159
Joined: January 12th, 2010, 8:29 am

Re: Nested DatabaseObjects

Postby azuanagames » October 12th, 2010, 3:41 am

Looks like it works using the Set("achievements."+achivementName method.

I wasn't sharing the vAchievements DatabaseObject, it was a member variable on the Player object. This works though so no worries!

Thanks again
azuanagames
 
Posts: 157
Joined: April 29th, 2010, 10:59 pm


Return to BigDB



cron