• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by The Paper Pilot · Jan 22, 2011 at 01:00 AM · lobbymmorpgsmartfoxsmartfoxserversfs

Smart Fox Server Lobby

This doesn't seem to have been asked, but I'm not 100% sure. I'm trying to make a lobby where someone can log in, then chat and choose a room to play in, which they can then go into via a "play now" button. But there are several problems: 1. Custom login I can't figure out how to make a custom login. I looked at examples but they were in css. Do I need to know that, because I'm good at Javascript? 2. Room select I made a list using GUI, but I don't know how to make the list clickable and move people into those rooms. Again, this is a beginner's kind of problem, but I couldn't figure this out through help 3. Play now After I added the play now button, I tried to see if it worked, and now I can't connect. At the admin tool it says the server is up,but I can't connect. I tried to edit the example's css, but it seems I messed up somewhere. can you show me where?

Thanks if you can help, I'm really new to Unity, and this is the first time I've used SFS to. I am familiar with Flash, and other scripting codes like Java, Ruby (1 and 2), HTML (duh), etc.

Heres the code I was trying to edit in the example lobby that (as you know) failed epically(lol did I spell this right?):

using UnityEngine;

using System; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Security.Permissions; using System.Text; using Sfs2X; using Sfs2X.Core; using Sfs2X.Entities; using Sfs2X.Requests; using Sfs2X.Logging;

public class LobbyGUI : MonoBehaviour { public string serverName = "127.0.0.1"; public int serverPort = 9933;

private SmartFox smartFox; private string zone = "Unnamed MMO"; private string username = ""; private string password = ""; private string loginErrorMessage = ""; private bool isLoggedIn; private bool isJoining = false;

private string newMessage = ""; private ArrayList messages = new ArrayList(); // Locker to use for messages collection to ensure its cross-thread safety private System.Object messagesLocker = new System.Object();

private Vector2 chatScrollPosition, userScrollPosition;

private int roomSelection = 0; private string [] roomStrings;

public GUISkin gSkin;

private Room currentActiveRoom;

void Start() { bool debug = true; if (SmartFoxConnection.IsInitialized) { smartFox = SmartFoxConnection.Connection; } else { smartFox = new SmartFox(debug); }

 // Register callback delegate
 smartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection);
 smartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
 smartFox.AddEventListener(SFSEvent.LOGIN, OnLogin);
 smartFox.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);
 smartFox.AddEventListener(SFSEvent.LOGOUT, OnLogout);
 smartFox.AddEventListener(SFSEvent.ROOM_JOIN, OnJoinRoom);
 smartFox.AddEventListener(SFSEvent.PUBLIC_MESSAGE, OnPublicMessage);

 smartFox.AddLogListener(LogLevel.DEBUG, OnDebugMessage);

 smartFox.Connect(serverName, serverPort);

}

void FixedUpdate() { smartFox.ProcessEvents(); }

private void UnregisterSFSSceneCallbacks() { // This should be called when switching scenes, so callbacks from the backend do not trigger code in this scene smartFox.RemoveAllEventListeners(); }

public void OnConnection(BaseEvent evt) { bool success = (bool)evt.Params["success"]; string error = (string)evt.Params["error"];

 Debug.Log("On Connection callback got: " + success + " (error : <" + error + ">)");

 if (success) {
     SmartFoxConnection.Connection = smartFox;
 }

}

public void OnConnectionLost(BaseEvent evt) { Debug.Log("OnConnectionLost"); isLoggedIn = false; isJoining = false; currentActiveRoom = null; UnregisterSFSSceneCallbacks(); }

// Various SFS callbacks public void OnLogin(BaseEvent evt) { try { bool success = true; if (evt.Params.ContainsKey("success") && !(bool)evt.Params["success"]) { loginErrorMessage = (string)evt.Params["errorMessage"]; Debug.Log("Login error: "+loginErrorMessage); } else { isLoggedIn = true; Debug.Log("Logged in successfully"); ReadRoomListAndJoin(); } } catch (Exception ex) { Debug.Log("Exception handling login request: "+ex.Message+" "+ex.StackTrace); } }

public void OnLoginError(BaseEvent evt) { Debug.Log("Login error: "+(string)evt.Params["errorMessage"]); }

void OnLogout(BaseEvent evt) { Debug.Log("OnLogout"); isLoggedIn = false; isJoining = false; currentActiveRoom = null; }

public void OnDebugMessage(BaseEvent evt) { string message = (string)evt.Params["message"]; Debug.Log("[SFS DEBUG] " + message); }

private void ReadRoomListAndJoin() { Debug.Log("Room list: ");

 List<Room> roomList = smartFox.RoomManager.GetRoomList();
 List<string> roomNames = new List<string>();
 foreach (Room room in roomList) {
     if (room.IsHidden || room.IsPasswordProtected) {
         continue;
     }   

     roomNames.Add(room.Name);
     Debug.Log("Room id: " + room.Id + " has name: " + room.Name);

 }

 roomStrings = roomNames.ToArray();

 if (smartFox.LastJoinedRoom==null) {
     JoinRoom("Main Room");
 }

}

void JoinRoom(string roomName) { if (isJoining) return;

 isJoining = true;
 currentActiveRoom = null;
 Debug.Log("Joining room: "+roomName);

 // Need to leave current room, if we are joined one
 if (smartFox.LastJoinedRoom==null)
     smartFox.Send(new JoinRoomRequest(roomName));
 else    
     smartFox.Send(new JoinRoomRequest(roomName, "", smartFox.LastJoinedRoom.Id));

}

void OnJoinRoom(BaseEvent evt) { Room room = (Room)evt.Params["room"]; Debug.Log("Room " + room.Name + " joined successfully");

 lock (messagesLocker) {
     messages.Clear();
 }

 currentActiveRoom = room;
 isJoining = false;

}

void OnPublicMessage(BaseEvent evt) { try { string message = (string)evt.Params["message"]; User sender = (User)evt.Params["sender"];

     // We use lock here to ensure cross-thread safety on the messages collection 
     lock (messagesLocker) {
         messages.Add(sender.Name + " said " + message);
     }

     chatScrollPosition.y = Mathf.Infinity;
     Debug.Log("User " + sender.Name + " said: " + message);
 }
 catch (Exception ex) {
     Debug.Log("Exception handling public message: "+ex.Message+ex.StackTrace);
 }

}

// Finally draw all the lobby GUI void OnGUI() { if (smartFox == null) return; GUI.skin = gSkin;

 if (!smartFox.IsConnected) {
     GUI.Label(new Rect(10, 90, 100, 100), "Connecting...");
 }

 else if (!isLoggedIn) {
     GUI.Label(new Rect(10, 90, 100, 100), "Zone: ");
     zone = GUI.TextField(new Rect(100, 90, 200, 20), zone, 25);

     GUI.Label(new Rect(10, 116, 100, 100), "Username: ");
     username = GUI.TextField(new Rect(100, 116, 200, 20), username, 25);

     GUI.Label(new Rect(10, 142, 100, 100), "Password: ");
     password = GUI.TextField(new Rect(100, 142, 200, 20), password, 4);

     GUI.Label(new Rect(10, 218, 100, 100), loginErrorMessage);

     if (GUI.Button(new Rect(100, 166, 100, 24), "Login")  || (Event.current.type == EventType.keyDown && Event.current.character == '\n'))
     {
         Debug.Log("Sending login request");
         smartFox.Send(new LoginRequest(username, password, zone));
     }
 }
 else if (isJoining)
 {
     // Standard view
     if (GUI.Button(new Rect(580, 478, 90, 24), "Logout"))
     {
         smartFox.Send( new LogoutRequest() );
     }

     GUI.Label(new Rect(498, 248, 180, 40), "Joining room");
 }
 else if (currentActiveRoom!=null)
 {
     if (GUI.Button(new Rect(580, 478, 90, 24), "Logout"))
     {
         smartFox.Send( new LogoutRequest() );
     }

     GUI.Label(new Rect(498, 248, 180, 40), "Current room: " + currentActiveRoom.Name);

     // Room list
     GUI.Box(new Rect(490, 80, 180, 170), "Room List");                  

     GUILayout.BeginArea (new Rect(500, 110, 150, 130));                                     
     roomSelection = GUILayout.SelectionGrid (roomSelection, roomStrings, 1, "RoomListButton");

     if (GUI.Button(new Rect(170, 180, 80, 490), "Play Game"))
     {
         Application.LoadLevel(1);  //is this valid css? I put this here myself.
     }

     if (roomStrings[roomSelection] != currentActiveRoom.Name)
     {
         JoinRoom(roomStrings[roomSelection]); 
         GUILayout.EndArea();
         return;
     }

     GUILayout.EndArea();

     // User list
     GUI.Box(new Rect(490, 270, 180, 200), "Users");

     GUILayout.BeginArea (new Rect(500, 300, 150, 160));
         userScrollPosition = GUILayout.BeginScrollView (userScrollPosition, GUILayout.Width (150), GUILayout.Height (160));
             GUILayout.BeginVertical();

                 foreach (User user in currentActiveRoom.UserList)
                 {
                     GUILayout.Label(user.Name);
                 }

             GUILayout.EndVertical();
         GUILayout.EndScrollView ();
     GUILayout.EndArea();



     // Chat history
     GUI.Box(new Rect(10, 80, 470, 390), "Chat");

     GUILayout.BeginArea (new Rect(20, 110, 450, 350));
         chatScrollPosition = GUILayout.BeginScrollView (chatScrollPosition, GUILayout.Width (450), GUILayout.Height (350));
             GUILayout.BeginVertical();

             // We use lock here to ensure cross-thread safety on the messages collection 
             lock (messagesLocker) {
                 foreach (string message in messages)
                 {
                     GUILayout.Label(message);
                 }
             }

             GUILayout.EndVertical();
         GUILayout.EndScrollView ();
     GUILayout.EndArea();

     // Send message
     newMessage = GUI.TextField(new Rect(10, 480, 370, 20), newMessage, 50);
     if (GUI.Button(new Rect(390, 478, 90, 24), "Send")  || (Event.current.type == EventType.keyDown && Event.current.character == '\n'))
     {
         smartFox.Send( new PublicMessageRequest(newMessage) );
         newMessage = "";
     }

 }

}

}

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Steffen Franz · Jan 22, 2011 at 01:10 AM

the Smartfox Server documentation has all the info you need for this, SFS and Unity code wise. I would check over on their side. http://www.smartfoxserver.com/docs/ Even more so as you are familiar with Flash most of the documentation examples are in AS which you can easily translate into C#

Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image The Paper Pilot · Jan 22, 2011 at 01:17 AM 0
Share

Its still hard to translate anything into a script you don't know... Thank you for the help, I will try there, but if you do figure out how to do it directly, that would be great :)

avatar image The Paper Pilot · Jan 22, 2011 at 01:18 AM 0
Share

o (sry for double posting) does this work for SFS 2X? Because that what I have currently

avatar image

Answer by WhiskeyJack · May 16, 2011 at 07:32 PM

Better late than never. For a custom login you will need to code some stuff on the server side in java (not javascript). For an excellent tutorial on the basics of creating a serverside extension watch this video: http://www.youtube.com/watch?v=nKGxhwJ0Ccc

Also, like Steffen mentioned, take another close look at http://www.smartfoxserver.com/docs Chapter 8.3 shows how to create a simple database extension (not dealing with any javascript which you do not need at all by the way).

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by CarlLydon · Mar 02, 2014 at 04:48 AM

If you prefer javascript extension for your custom login you could use an older version of smartFox, which allows js.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

i need SFS help 1 Answer

How to put two different characters on SFS ISLAND DEMO 0 Answers

Smart Fox error 2 Answers

Error building Player: SystemException: 'System.Net.Sockets' are supported only with Unity Android Pro. Referenced from assembly 'SmartFox2'. 1 Answer

not able to connect to sfs(smartfox) server? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges