To begin you will want to open up your character select scene and adding an empty game object. I called mine CharacterSelectGUI. I then created a new CharacterSelectGUI script which I attach to the game object. We are then going to add our disconnect functions to the script:
public void OnConnectionLost(BaseEvent evt)
{
// Display popup, when user hits ok, return to lobby.
//loginErrorMessage = "Connection lost / no connection to server";
Application.LoadLevel("Lobby");
}
void OnLogout(BaseEvent evt)
{
Application.LoadLevel("Lobby");
}
public void OnDebugMessage(BaseEvent evt)
{
string message = (string)evt.Params["message"];
if(debugMessages)
{
Debug.Log("**** DEBUG ****" + message);
}
}
Next we will add the Awake function and its components.
bool debugMessages = false;
CharacterSelectHandler CharacterSelect;
bool debugMessages = false;
CharacterSelectHandler CharacterSelect;
void Awake()
{
base.Awake();
if(smartFox.IsConnected)
{
CharacterSelect = new CharacterSelectHandler();
// Register callback delegate
smartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
smartFox.AddEventListener(SFSEvent.LOGOUT, OnLogout);
smartFox.AddLogListener(LogLevel.DEBUG, OnDebugMessage);
// We are ready to get the character list
ISFSObject data = new SFSObject();
ExtensionRequest request = new ExtensionRequest("getCharacters", data);
smartFox.Send(request);
}
else
{
Application.LoadLevel("Lobby");
}
}
This will set us up so that we send the new message the server is looking for. The server should then respond with our list of characters.
In our common code section I will be creating an abstract class that will set up 2 delegates so we can handle things before and after a message is received as well as what to do when the message is actually received. Create a new class called IMessageHandler.cs in common/scripts and it will contain the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sfs2X.Entities.Data;
public abstract class IMessageHandler
{
public delegate void BeforeMessageRecieved();
public BeforeMessageRecieved beforeMessageRecieved;
public delegate void AfterMessageRecieved();
public AfterMessageRecieved afterMessageRecieved;
public abstract void HandleMessage(ISFSObject data);
}
As you can see it is fairly simple. Next we will create a CharacterSelectHandler that will listen for our event back from the server containing a list of characters:
using System;
using Sfs2X.Entities.Data;
using System.Collections.Generic;
using UnityEngine;
public class CharacterSelectHandler : IMessageHandler
{
public List characterList;
public int maxCharacters;
public CharacterSelectHandler ()
{
characterList = new List();
maxCharacters = 0;
}
public override void HandleMessage(ISFSObject data)
{
maxCharacters = data.GetInt("maxCharacters");
ISFSObject characters = data.GetSFSObject("characters");
foreach (string key in characters.GetKeys())
{
Debug.Log("Adding character: " + key);
characterList.Add(Character.LoadFromSFSObject(characters.GetSFSObject(key)));
}
Debug.Log("Max: " + maxCharacters);
Debug.Log("Characters: " + characterList.Count);
}
}
This class uses a class we haven't created yet called Character. It is what our client will use to show all of our character's data. Later we will create a RemoteCharacter class that will be for other players and NPCs as we won't be able to see all the data about those characters. Much like you can't see all the data in any other MMO.
using System;
using Sfs2X.Entities.Data;
using UnityEngine;
public class Character
{
string name;
int level;
public Character ()
{
}
public static Character LoadFromSFSObject(ISFSObject data)
{
Character newCharacter = new Character();
try
{
newCharacter.name = data.GetUtfString("name");
newCharacter.level = data.GetInt("level");
}
catch(Exception e)
{
Debug.Log(e);
}
return newCharacter;
}
}
For now I have limited it to name and level, later we will expand it to include the other important data contained in the entity [GameName]Character class. For now this will do. The last thing we need to do is to add the handler to the CharacterSelectGUI's awake function. Just after the Log Debug handler add:
// Personal message handlers
handlers.Add("characterlist", CharacterSelect.HandleMessage);
This concludes part 3. By now you should be able to log in to the server and get back a list of characters. Tomorrow we will delve into the logic to see if the user can create a character and moving to a character select screen where they can chose some simple data and create a character. Stay Tuned!
No comments:
Post a Comment