Forum C# Executing code on server when just logged in?

Executing code on server when just logged in?

Postby HAnz » March 2nd, 2011, 4:51 pm

Right now I can only run code on the server when a user creates or joins a game, in here:
public class GameCode : Game<Player> {
public override void GameStarted() {

So I'm wondering how I should do to be able to run code on the server as soon as the player have logged in. Right now I want to read and write some stuff from/to the PlayerObject.
I make games and stuff.
User avatar
HAnz
 
Posts: 46
Joined: August 7th, 2010, 2:59 pm

Re: Executing code on server when just logged in?

Postby FulaFisken » March 2nd, 2011, 8:10 pm

I don't think you can, but I am not sure. However, I solved by having the user waiting for a callback event if the game is not ready yet. Something like below..

Code: Select all
public override void UserJoined(player) {
    if (!ready && firstUser) {
        load....(delegate{
            load what ever.....
            UserJoined(player);
        });
    } else {
        //Game ready and give user info     
    }
}
Fula Fisken
website blog twitter
Play Game
Astroflux
User avatar
FulaFisken
Paid Member
 
Posts: 139
Joined: March 2nd, 2011, 10:15 am

Re: Executing code on server when just logged in?

Postby Henrik » March 3rd, 2011, 9:41 am

What do you mean by "just logged in"? Just after doing a Connect or QuickConnect?

You can only execute serverside code for players that are in a room, but your game can consist of multiple roomtypes. If you want to do something right after connect, make another roomtype that contains the code you want to run in the UserJoined method, and have that method end with a call to player.Disconnect(). Then, when a user logs in, just join a room of that other roomtype, and your code will execute once, for each player.

Set the room name to $service-room$ and players will be automatically load-balanced across multiple rooms if you get a huge amount of players logging in at the same time.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Executing code on server when just logged in?

Postby Henrik » March 3rd, 2011, 9:46 am

Code: Select all
public class InitPlayer : BasePlayer { }

[RoomType("InitRoom")]
public class InitRoom : Game<InitPlayer> {
   public override void GameStarted() {
      PreloadPlayerObjects = true;
   }

   public override void UserJoined(InitPlayer player) {
      //Do your PlayerObject changes:

      //And disconnect the user
      player.Disconnect();
   }
}
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Executing code on server when just logged in?

Postby HAnz » March 3rd, 2011, 1:18 pm

Great stuff, it was just what I needed. One little error in your code though: "UserJoined(Player player)" is supposed to be "UserJoined(InitPlayer player)"

If anyone needs to do this in the future, this is my client side code:

Code: Select all
client.multiplayer.createJoinRoom(
      "$service-room$",
      "InitRoom",
      false,
      {},
      {},
      handleJoin,
      handleError
)
I make games and stuff.
User avatar
HAnz
 
Posts: 46
Joined: August 7th, 2010, 2:59 pm


Return to C#



cron