Unity3d Mac/Windows Standalone Unable To Leave MMO Room

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

User avatar
TheHammerSV
Posts: 3
Joined: 03 Nov 2018, 20:40

Unity3d Mac/Windows Standalone Unable To Leave MMO Room

Postby TheHammerSV » 03 Nov 2018, 21:17

Hello! I am relatively new to SmartFoxServer and am currently working on my first project with it, so i've been running into a few problems (most of which I have been able to fix with a bit of research). One I can't seem to figure out though is a issue I am having with allowing a user to leave an MMO room.

I am trying to develop an MMORPG in Unity3d 2018 for Macs and PCs. When completed it will give a player a range of worlds to play on and explore so switching between them is an important mechanic. Through research, I think I have determined that switching users between MMO rooms and then loading that room's corresponding scene is the best way to approach this.

I have been having problems with the LeaveRoomRequests and getting the player to actually leave the room they are currently in. What I am trying to do is have the user click a UI button which will have them leave the room and then join the new one. Unity doesn't give me any errors when the function with the LeaveRoomRequest is run, but when I try to join a new MMO room it won't let me enter it and the server still shows that I am in the old room. The server doesn't seem to be showing any errors or responses to this request either.

To try and learn how to build an MMO like this using smartfox, I started with modifying the MMO demo project by adding the button that would switch the user's room and scene. This was when I first encountered the issue. After being unable to find a solution, I figured that something already in the project might be causing some sort of bug that was stopping the LeaveRoomRequest from working and decided that I should try and create a new project from scratch to solve the issue. Using the tutorial series from GenesisRage on youtube, I have gotten to the point where I can successfully connect, join the zone, and join the first MMO room. The series does not show me how to switch rooms so I used the documentation to recreate the switch room button I had been trying in the demo project (without the scene switch as I have not yet gotten that far into the project yet), but the problem still persisted. Any input someone might have as to how to fix this issue or possibly a better method for switching between worlds would be much appreciated. Thank you!

Screenshots:
https://drive.google.com/file/d/1B1Cy2K ... sp=sharing

Code:

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Requests;

public class ButtonRoomSwitch : MonoBehaviour {

    //Make sure to attach these Buttons in the Inspector
    public Button m_YourFirstButton;

    SmartFox sfs;

    void Start()
    {
        sfs = new SmartFox();
        sfs.ThreadSafeMode = true;

        //Calls the TaskOnClick/TaskWithParameters/ButtonClicked method when you click the Button
        m_YourFirstButton.onClick.AddListener(TaskOnClick);

    }

    void Update () {
        sfs.ProcessEvents();
    }

    void TaskOnClick() {
        sfs.Send(new LeaveRoomRequest());
        Debug.Log("Room Left");
    }
}
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unity3d Mac/Windows Standalone Unable To Leave MMO Room

Postby Lapo » 05 Nov 2018, 09:58

Hi,
the code example is a bit confusing. I am not sure if it's supposed to be pseudo-code or actual, working code.

The problem is you're creating a SmartFox object at the top and then sending a LeaveRoomRequest without having connected, logged in etc... Obviously this won't work, as you need those basic steps before trying to leave any Room.

Maybe you just omitted those steps for brevity? Can you clarify?

Back to your question, there's a main difference between standard Rooms and MMO Rooms: the latter don't generate any USER_ENTER or USER_EXIT events, therefore when you send a LeaveRoomRequest you will not get a confirmation via the USER_EXIT event.

This is normal, as those events are suppressed, to avoid massive updates every time a user enters/leaves the Room. To switch Rooms you can just send a LeaveRoomRequest followed by a new JoinRoomRequest.

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
TheHammerSV
Posts: 3
Joined: 03 Nov 2018, 20:40

Re: Unity3d Mac/Windows Standalone Unable To Leave MMO Room

Postby TheHammerSV » 05 Nov 2018, 13:51

Thanks for your response!
The piece of code I sent you was for the button. It was sensing when the button was clicked and trying to send a LeaveRoomRequest for a room that had already been entered via a separate connect script I had working perfectly.

The steps of the glitch are:
1. The user joins the server, Zone, and First Room
2. The user clicks the button which tries to send a LeaveRoomRequest but nothing seems to happen. The user still shows up in the room on the admin site and there is no recognition of an error in Unity.

I have also since discovered that other requests don't want to work when I am joined into a room either. I had it set up so that when a user joins the first room, a PublicMessageEvent was sent to announce their presence and a debug message would appear in the console if the PUBLIC_MESSAGE event was fired successfully, so I decided to use this request in a test for my button script. I replaced the LeaveRoomRequest in that script with a PublicMessageRequest to see if this problem was only affecting the LeaveRoomRequest or not. I knew that USER_EXIT was not fired in MMO rooms, so I didn't bother trying to send a debug message for that event, but I did set up the script to send a message to the console when the PUBLIC_MESSAGE event was fired, but it appears that this event was never fired at all as the message never showed up in the console. The LeaveRoomRequest is a more pressing problem for me, this was just a test to see where the problem lies, and it appears that none of the requests are working after a player joins a room. I don't know if I am missing something in my scripts when firing events or if the rooms in my server aren't configured properly, but something definitely going wrong.

Here is my code with the PublicMessage test in it rather than the LeaveRoomRequest:

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Requests;

public class ButtonRoomSwitch : MonoBehaviour {

    //Make sure to attach these Buttons in the Inspector
    public Button m_YourFirstButton;

    SmartFox sfs;

    void Start()
    {
        sfs = new SmartFox();
        sfs.ThreadSafeMode = true;

        //Calls the TaskOnClick/TaskWithParameters/ButtonClicked method when you click the Button
        m_YourFirstButton.onClick.AddListener(TaskOnClick);
        sfs.AddEventListener(SFSEvent.PUBLIC_MESSAGE, OnPublicMessage);

    }

    void Update () {
        sfs.ProcessEvents();
    }

    void TaskOnClick() {
        sfs.Send(new PublicMessageRequest("I Clicked A Button"));
        Debug.Log("Room Left");
    }

    void OnPublicMessage(BaseEvent e)
    {
        Debug.Log(e.Params["message"]);
    }
}


Here is my connect script:

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Requests;
using Sfs2X.Entities;

public class SFS2X_Connect : MonoBehaviour {

    public string ServerIP = "0.tcp.ngrok.io";
    public int ServerPort = 13005;
    public string ZoneName = "BasicExamples";
    public string UserName = "";
    public string RoomName = "World1";

    SmartFox sfs;

   // Use this for initialization
   void Start () {
        sfs = new SmartFox();
        sfs.ThreadSafeMode = true;

        sfs.AddEventListener(SFSEvent.CONNECTION, OnConnection);
        sfs.AddEventListener(SFSEvent.LOGIN, OnLogin);
        sfs.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);
        sfs.AddEventListener(SFSEvent.ROOM_JOIN, OnJoinRoom);
        sfs.AddEventListener(SFSEvent.ROOM_JOIN_ERROR, OnJoinRoomError);
        sfs.AddEventListener(SFSEvent.PUBLIC_MESSAGE, OnPublicMessage);

        sfs.Connect(ServerIP, ServerPort);
   }

    void OnLogin(BaseEvent e) {
        Debug.Log("Logged In: " + e.Params["user"]);
        sfs.Send(new JoinRoomRequest(RoomName));
    }

    void OnJoinRoom(BaseEvent e) {
        Debug.Log("Joined Room: " + e.Params["room"]);
        sfs.Send(new PublicMessageRequest("Hello World!"));
    }

    void OnPublicMessage(BaseEvent e) {
        Room room = (Room)e.Params["room"];
        User sender = (User)e.Params["sender"];
        Debug.Log("[" + room.Name + "] " + sender.Name + ": " + e.Params["message"]);
    }

    void OnJoinRoomError(BaseEvent e) {
        Debug.Log("JoinRoom Error: (" + e.Params["errorCode"] + "): " + e.Params["errorMessage"]);
    }

    void OnLoginError(BaseEvent e){
        Debug.Log("Login Error (" + e.Params["errorCode"] + "): " + e.Params["errorMessage"]);
    }

    void OnConnection (BaseEvent e) {
        if ((bool)e.Params["success"])
        {
            Debug.Log("Successfully Connected");
            sfs.Send(new LoginRequest(UserName, "", ZoneName));
        }
        else
        {
            Debug.Log("Connection Failed");
        }
    }
   
   // Update is called once per frame
   void Update () {
        sfs.ProcessEvents();
   }

    void OnApplicationQuit() {
        if (sfs.IsConnected)
        {
            sfs.Disconnect();
        }
    }
}


I forgot to mention last time that I am using Client API 1.7.9.
Thank you for working with me on this issue. :D
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unity3d Mac/Windows Standalone Unable To Leave MMO Room

Postby Lapo » 05 Nov 2018, 14:56

There seem to be a fundamental misunderstanding with how to use the client side API.

Your code example for the button is not correct.
you must create a single SmartFox instance and reference it from anywhere in your code. You can't create new instances of the SmartFox object for every button you need.

The SmartFox object represents your connection and multiplayer state for the whole gaming session, and therefore it must be created once for every session.

I highly recommend to take a look at our examples and relative tutorials found here:
http://docs2x.smartfoxserver.com/Exampl ... troduction

Especially the first examples, such as "Connector" and "Lobby" are very simple and provide the basis for building any other game/app based on the SFS2X client API.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 11 guests