Forum BigDB Am I using your API correctly? (Newbie question)

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

Am I using your API correctly? (Newbie question)

Postby zapleaf » October 21st, 2014, 5:30 am

I am very amateur at this, and my last MMO (though made and runs) is a giant mess, and I am trying a new approach at this. I am just curious if I am loading / calling / setting the Player DBObject correctly or not.

This is what I am doing:

When a user joins the game, this is called for that user.
Code: Select all
public void loadUser(Player player) {
            PlayerIO.BigDB.LoadOrCreate("PlayerObjects", player.ConnectUserId,
                delegate(DatabaseObject result)
                {
                    if ( !result.Contains("username") ) { result.Set("username", "SimpleUser"); }
                    if ( !result.Contains("level") )    { result.Set("level", 1); }
                    if ( !result.Contains("exp") )      { result.Set("exp", 0); }
                    result.Save();

                    player.Send( "loadFinished", result.GetString("username"), result.GetInt("level"), result.GetInt("exp") );

                    if (result.Contains("username")) { Broadcast("UserJoined", result.GetString("username")); }
                    else { Broadcast("UserJoined", "Unknown"); }
                }
            );
        }


Next when a user wishes to change his name he calls this function:
Code: Select all
public override void GotMessage(Player player, Message message) {
         switch(message.Type) {
            case "NameChange":
                    PlayerIO.BigDB.Load("PlayerObjects", player.ConnectUserId, delegate(DatabaseObject user) { user.Set("username", message.GetString(0)); user.Save(); });
               break;
         }
      }


I'm still in the early stages of development, and I would like to know if I am using Player DB correctly or not, so I can get into the swing of things right away. I've tried following the documentation best I can, but I find it very hard to understand so I find myself testing and failing a lot of ways of going about this.

EDIT: One big concern of mine would be running all these delegate in a fast paced environment. I am curious if they would be reliable? I've found calling a delegate and then doing something else does not run the program in order, instead there is a slight (Very noticeable) delay when a delegate function/method is called.
zapleaf
 
Posts: 16
Joined: August 13th, 2013, 12:31 pm

Re: Am I using your API correctly? (Newbie question)

Postby zapleaf » October 22nd, 2014, 1:14 am

If anyone else is as confused about this as I am and has been searching for an answer for ages here ya go.
At the start of the game place this code to ensure the player.PlayerObject is preloaded for each user.
Code: Select all
public override void GameStarted() { PreloadPlayerObjects = true; }


Now when ever you wish to make changes try doing this as the player object is now directly tied to your users Player Class. Using save() is perfectly safe as the people behind this SDK have built in mechanics in place to optimize performance and only save the what has been changed in the player Object.
Code: Select all
player.PlayerObject.Set("username", message.GetString(0));
player.PlayerObject.Save();


Stupid simple, took me ages to figure out. Some of my older posts show how horrible confused I was, and some of the crazy methods I cam up with to fix this. I have finally read though so many forums and documentation on this site, I think this is how they intend for the playerobject to be used. If I am off, and you know what it is I am doing wrong, please let me know so I can make changes!
zapleaf
 
Posts: 16
Joined: August 13th, 2013, 12:31 pm

Re: Am I using your API correctly? (Newbie question)

Postby robertflesch » October 28th, 2014, 4:23 pm

Zapleaf sounds like you are on the right track now.

My code to load the player looks something like this (simplified for brevity)

Two big things are
1) using the loadMyPlayerObject call
2 ) And using the databaseObject's save function to save the data.

As for all of the delegates, that is no big deal.
The player loading code is called VERY rarely. Don't optimize things that get called rarely.

I just have AS3 Code, but its not that different.
Code: Select all
public function createPlayer():void   {
   Network.client.bigDB.loadMyPlayerObject( onPlayerLoadedAction, onPlayerLoadError );
}

private function onPlayerLoadedAction( $dbo:DatabaseObject ):void {
   // The dbo is null if the object does not exist.
   if ( $dbo ) {
      Globals.player.fromPersistance( $dbo );
      Globals.g_app.dispatchEvent( new PlayerPersistanceEvent( PlayerPersistanceEvent.PLAYER_MODEL_LOAD_SUCCESS ) );
   }
   else {
      // this creates a new player object, if the record is not found.
      Globals.player.toPersistance();
      Globals.g_app.dispatchEvent( new PlayerPersistanceEvent( PlayerPersistanceEvent.PLAYER_MODEL_LOAD_FAILURE ) );
   }
}

private function onPlayerLoadError(error:PlayerIOError):void {
   Globals.g_app.dispatchEvent( new PlayerPersistanceEvent( PlayerPersistanceEvent.PLAYER_MODEL_LOAD_FAILURE ) );
}

// Now if the user wants to save his name change.
// I would call
private function save():void {
   // this saves the data from the member variables into the dbo object.
   toPersistance();
   _dbo.save( false, false, successHandler, failureHandler );
}

// An example of toPersistance
public function toPersistance():void {
   _dbo.name       = _name;
   _dbo.description   = _description;
   _dbo.owner      = _owner;
}



I can relate to how frustrating it is to figure this stuff out with minimal documentation, but it does seem to work well.

bob
robertflesch
Paid Member
 
Posts: 136
Joined: April 22nd, 2013, 9:18 pm


Return to BigDB