Usage flow tutorial | Unity Realtime

In this tutorial, we will go through a step-by-step process on how to login a user in Gamedonia Realtime, add him to a Room, and send information to other users in the same room.

Your first step should be to connect to the Gamedonia servers. If you haven't done it yet, check out our Get Started guide first.

Login

Once there exists an active connection, you can log a user in. This is done by sending a Login operation to the server.

	GamedoniaRT gdRt = GamedoniaRT.SharedInstance ();
	LoginOperation lop = new LoginOperation ();
	lop.name = this.nickname;

	gdRt.SendOp (lop);

And we can register for Login return events like this:

	gdRt.AddEventListener (GamedoniaEvents.ON_LOGIN_SUCCESS, OnLoginSuccess);
	gdRt.AddEventListener (GamedoniaEvents.ON_LOGIN_ERROR, OnLoginError);

Check if a room exists

If everything went fine, the user is now logged in but still outside of any room.

Before the user can join a room, we need to check if it exists (in this example, we will search for DefaultRoom). If it does, we will join it. If it doesn't, we will create it and join it afterwards. Let's see how we can search for a room:

	GamedoniaRT gdRt = GamedoniaRT.SharedInstance ();
	Room room = gdRt.roomsManager.GetRoomByName ("DefaultRoom");

	if (room == null) {
	   //Create room
	} else {
	   //Join room       
	}

Gamedonia RT SDK stores a local representation of the visible Rooms and User information of the game. You can access all room information through the rooms manager exposeed in the GamedoniaRT singleton.

Create a Room

If the room we want to join didn't exist yet, we need to create it. This operation is a bit more complex because it requires the specification of the Settings of the room we want to create.

The CreateRoom operation is invoked like this:

	GamedoniaRT gdRt = GamedoniaRT.SharedInstance ();
	RoomSettings rs = new RoomSettings ();
	CreateRoomOperation crop = null;

	rs.name = "DefaultRoom";
	rs.isGame = false;
	rs.maxUsers = 100;
	rs.removePolicy = Room.REMOVE_POLICY_NEVER;                
	crop = new CreateRoomOperation (rs);
	gdRt.SendOp (crop);

And we can register for the CreateRoom return events like this:

	GamedoniaRT gdRt = GamedoniaRT.SharedInstance ();
	gdRt.AddEventListener (GamedoniaEvents.ON_CREATE_ROOM_SUCCESS, OnCreateRoomSuccess);
	gdRt.AddEventListener (GamedoniaEvents.ON_CREATE_ROOM_ERROR, OnCreateRoomError);

To get more information about the room settings and the CreateRoom operation, check the Operations guide.

Join Room

Once we are sure a room exists, users can start joining it. If there is still place for another user, he will be able to become part of the room. A user can be a member multiple rooms.

To make a user join a room, send the following operation:

	GamedoniaRT gdRt = GamedoniaRT.SharedInstance ();
	JoinRoomOperation jrop = new JoinRoomOperation ("DefaultRoom");
	gdRt.SendOp (jrop);

We can register for JoinRoom return events like this:

	GamedoniaRT gdRt = GamedoniaRT.SharedInstance ();
	gdRt.AddEventListener (GamedoniaEvents.ON_JOIN_ROOM_SUCCESS, OnJoinRoomSuccess);
	gdRt.AddEventListener (GamedoniaEvents.ON_JOIN_ROOM_ERROR, OnJoinRoomError);

Set variables

Now that we have joined at least one room, we start exchanging information with other users in the room.

Gamedonia Realtime uses the exchange of variables to share information between users in a room.

There are 2 types of variables:

  • Room variables: They are stored in the room. All changes to this variables will be broadcasted to the users connected to this room.
  • User variables: They are stored in the user. Changes to these variables are sent to any user linked in any way to the user. For instance, all concurrent users in a room will see updates to the other user variables (if the variable is declared as shared).

The choice between user and room variables will depend on the game we are developing. If by design a game allows a player to be connected to a single room, then it is better user vars to exchange information between users, as they are a bit easier to handle.

This is an example on how to send a user variable to the server:

	List<Gamedonia.Rt.Entities.UserVariable> userVariables = new List<Gamedonia.Rt.Entities.UserVariable>();
	userVariables.Add(new Gamedonia.Rt.Entities.UserVariable("x", 1.1));
	userVariables.Add(new Gamedonia.Rt.Entities.UserVariable("y", 1.653));
	userVariables.Add(new Gamedonia.Rt.Entities.UserVariable("z", 1.2332));
	userVariables.Add(new Gamedonia.Rt.Entities.UserVariable("rot", 0.68));
	SetUserVariablesOperation suvop = new SetUserVariablesOperation(userVariables,gdRt.me);

And this is how you should register to the corresponding events:

	GamedoniaRT gdRt = GamedoniaRT.SharedInstance ();
	gdRt.eventsDispatcher.AddEventListener (GamedoniaEvents.ON_SET_USER_VARIABLES_SUCCESS, OnSetUserVariablesSuccess);
	gdRt.eventsDispatcher.AddEventListener (GamedoniaEvents.ON_SET_USER_VARIABLES_ERROR, OnSetUserVariablesSuccess);