Forum BigDB Creating data then sending it to the player

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

Creating data then sending it to the player

Postby archipdev » February 20th, 2015, 11:42 pm

hello,
I didn't manage to find a good solution on my own so I ask for your advice. I simply want to create dataObject in bigDB and send a message to the player when it's done.

Here is a very simplified code of what I want to do :
Code: Select all
public override void UserJoined(Player player)
        {
            base.UserJoined(player);

            //creating the item
            var obj = new DatabaseObject();
            obj.Set("owner", player.ConnectUserId);
            PlayerIO.BigDB.CreateObject("monsters", null, obj, sendMessage);
         }

Code: Select all
private void sendMessage(DatabaseObject o){
player.Send("info", "objectCreated"); //I can't do this because I have lost the reference to player
}


The problem is that I lose the reference to the player who just connected because of the callback.
archipdev
 
Posts: 28
Joined: September 6th, 2014, 10:35 am

Re: Creating data then sending it to the player

Postby Guillaume » February 21st, 2015, 1:05 am

It's very easy to solve !

Using static method is not mandatory, so you are STRONGLY advised to use Anonymous delegate callback !
This way you can have your callback within your actual method AND have access to the variables of the scope of your parent method, so in your exemple, UserJoined method.

Take a look at this:

Code: Select all
        public override void UserJoined(Player player)
        {
            base.UserJoined(player);

            Callback<DatabaseObject> sendMessage = delegate(DatabaseObject o)
            {
                player.Send("info", "objectCreated"); //You can do this now because we are also in the UserJoined context. player is available in the scope !
            };

            //creating the item
            var obj = new DatabaseObject();
            obj.Set("owner", player.ConnectUserId);
            PlayerIO.BigDB.CreateObject("monsters", null, obj, sendMessage);
        }


Also, if you come from the Web development world, it's just like using a success callback method in Javascript.
So this notation would work too:

Code: Select all
        public override void UserJoined(Player player)
        {
            base.UserJoined(player);
            //creating the item
            var obj = new DatabaseObject();
            obj.Set("owner", player.ConnectUserId);
            PlayerIO.BigDB.CreateObject("monsters", null, obj, delegate(DatabaseObject o){
            player.Send("info", "objectCreated"); //You can do this now because we are also in the UserJoined context. player is available in the scope !
            });
        }


This last notation is better if you need a easyly readable code workflow, you can call a delegate in a delegate and so on...This way you read your code in the good order, even if we know that everything will be executed asynchronous.

But the first notation is better if you have a redondant behavior to implement. Typically, for critical errors, i use the first notation and then use the declared variable in each PlayerIOError callback, so i don't have to rewrite the same code everytime, but i use the second notation for the Game logic workflow.
Guillaume
 
Posts: 277
Joined: March 28th, 2014, 1:59 am
Location: France

Re: Creating data then sending it to the player

Postby archipdev » February 21st, 2015, 9:48 am

Thanks a lot, that's exactly what I looked for :D
I'm glad I asked the question because the solutions I thought of were terrible
archipdev
 
Posts: 28
Joined: September 6th, 2014, 10:35 am

Re: Creating data then sending it to the player

Postby Guillaume » February 21st, 2015, 3:43 pm

You're welcome ;) !
Guillaume
 
Posts: 277
Joined: March 28th, 2014, 1:59 am
Location: France


Return to BigDB



cron