简体   繁体   English

Unity3d何时使用Single_Instance类

[英]Unity3d When to use Single_Instance class

I have a project that is multiplayer and uses SmartFoxServer. 我有一个多人游戏项目,并且使用SmartFoxServer。 I am writing a server extension but not sure what the best practice is. 我正在编写服务器扩展,但不确定最佳实践是什么。 I am seeing some odd behavior and I am not sure if this is due to how I have my classes set up. 我看到一些奇怪的行为,并且不确定这是否是由于我如何设置班级而导致的。 In my extension, I have a main class like this: 在我的扩展程序中,我有一个像这样的主类:

public class MyExtension extends SFSExtension
{
 private int numberOfPlayersReady  = 0;

    @Override
    public void init()
    {            
            addRequestHandler("dealingHand",MoveHandler.class ); 

    }
     //...some code
   public int getNumberOfPlayersReady(){
        return numberOfPlayersReady;
    }
    void setNumberOfPlayersReady(){
        numberOfPlayersReady++;
    }

}

MoveHandler.java: MoveHandler.java:

@Instantiation(InstantiationMode.SINGLE_INSTANCE)
public class MoveHandler extends BaseClientRequestHandler{    

    @Override
    public void handleClientRequest(User user, ISFSObject params)
    {

            MyExtension gameExt = (MyExtension) getParentExtension();   
            int currentNumber  = gameExt.getNumberOfPlayersReady();                
            trace("another player is ready " + currentNumber);

            if(currentNumber  == 3){
               //do something 
            }else{
                int newCurrentNumber = (int)currentNumber + 1;
                gameExt.setNumberOfPlayersReady();
            }
     }

  }

Then my server extension code is called in my Unity C# code like this: 然后在我的Unity C#代码中调用我的服务器扩展代码,如下所示:

SFSObject obj = new SFSObject();
obj.PutInt("playerCheckingInID", sfs.MySelf.Id);
sfs.Send(new ExtensionRequest("dealingHand", obj, sfs.LastJoinedRoom));

But, when I look at my SmartFoxServer standalone command line, to view the trace, I see: 但是,当我查看SmartFoxServer独立命令行时,要查看跟踪,我会看到:

"another player is ready 0"
"another player is ready 0"
"another player is ready 0"
"another player is ready 3"

I would imagine I would see: 我想我会看到:

"another player is ready 0"
"another player is ready 1"
"another player is ready 2"
"another player is ready 3"

Can anyone let me know whats going on here? 谁能让我知道这是怎么回事? Does this have to do with using a Single_Instance class? 这与使用Single_Instance类有关吗?

Edit: Or, does this have to do with synchronizing threads? 编辑:或者,这与同步线程有关吗? If so, what is the best way to manage that? 如果是这样,最好的管理方式是什么? This is multiplayer, turn based game. 这是多人,回合制游戏。 There will always be four players. 总会有四个玩家。 Requests sent to the Server Extension should be managed in the order they are received. 发送到服务器扩展的请求应按照接收顺序进行管理。

If you are going to make a multiplayer game , join each player that connect to sfs to a room , and then check total player that joined in room , when number of them reaches 4 , then leave them from that room , and create another room and put them in that room. 如果您要制作多人游戏,请将每个连接到sfs的玩家加入一个房间,然后检查加入该房间的玩家总数(当人数达到4个时),然后离开该房间,并创建另一个房间并把它们放在那个房间里。 Crete a static room for match making then create dynamic rooms for each match. 制作一个静态房间进行比赛,然后为每个比赛创建动态房间。 for example , create a room name MatchFindingRoom , then join each player that connects. 例如,创建一个房间名称MatchFindingRoom,然后加入每个连接的玩家。

        Room matchFindRoom= getParentExtension().getParentZone()
                .getRoomByName("MatchFindingRoom");
        getApi().joinRoom(user, getParentExtension().getParentZone().getRoomByName("MatchFindingRoom"));
        List<User> waiterUsers = matchFindRoom.getUserList();
        if(waiterUsers.size() >= 4)
        {
            CreateRoomSettings crs = new CreateRoomSettings();
            crs.setName("testRoom");
            crs.setMaxUsers(4);
            crs.setMaxVariablesAllowed(50);
            crs.setAutoRemoveMode(SFSRoomRemoveMode.WHEN_EMPTY);
            getApi().createRoom(getParentExtension().getParentZone(), crs,
                    null, false, null, false, false);
            Room matchRoom = getParentExtension().getParentZone().getRoomByName("testRoom");
            getApi().joinRoom(waiterUsers.get(0), matchRoom);
            getApi().joinRoom(waiterUsers.get(1), matchRoom);
            getApi().joinRoom(waiterUsers.get(2), matchRoom);
            getApi().joinRoom(waiterUsers.get(3), matchRoom);
            // do other stuff
        }

when you use room for users , then u don't need treads for tracking them , just get room and get the player inside it , and then do what ever you want. 当您为用户使用空间时,您不需要踩踏就可以跟踪它们,只需要腾出空间并在其中放置播放器,然后执行您想要的任何操作即可。 Highly recommended that don't use treads on server , because if every user create one of it , your server cpu can't reach them properly. 强烈建议不要在服务器上使用踏步,因为如果每个用户都创建一个踏步,则服务器cpu将无法正常访问它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM