Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by tkhowell95 · Feb 06, 2018 at 04:13 AM · networkingserverrpcclient-servercommand

How to change turns on button click using HLAPI?

So I've been stuck on this issue for a few days now and haven't been able to make this work. What I'm trying to do is make a turn-based game using the Unity HLAPI, and I'm trying to wrap my mind around the concepts of Commands, ClientRpc's, and all that stuff.

In order to help myself understand how simple client-server calls work, I have two objects in my scene: a Text box, and a Button. When one user presses the button, it should send a call to the server to tell it to change turns, and the Text box should reflect whose turn it is on each client. The caveat is that I don't want the ChangeTurn function to be called by anyone but the person whose turn it is, and that's where I'm hitting some roadbumps. I've tried all sorts of checks to make sure the presser of the button is the client/player whose turn it is by using isLocalPlayer, but it only seems to register the local player as whichever client is also hosting the server.

I'm using a singleton class to keep track of the state of the game, and I wonder if the error comes because there's only a single instance of the class, and so there can only be one localPlayer variable inside it...? ( localPlayer is a self-defined variable) Here's the relevant code from my Player and GameManager classes.

Player:

 public override void OnStartLocalPlayer() {
     GameManager.instance.localPlayer = this;
 }

 public int getID() { return playerID; }
 
     public void setID(int id) { playerID = id; }

GameManager:

 public static GameManager instance;
 public List<Player> players = new List<Player>();
 public Player localPlayer;
 public Deck deck;
 public GameTurnState currentState;
 public int numPlayers = 0;
 public Text messageBox;
 public Button testButton;

 [SyncVar(hook = "DisplayWhoTurn")]
 public int currentPlayerTurn = 0;

 // Initialize Singleton
 void Awake () {
     if(!instance) {
         instance = this;
         DontDestroyOnLoad(gameObject);
     } 
     else Destroy(gameObject);
 }
 
 public override void OnStartClient() {
     // Debug.Log("**** OnStartClient GameManager");
     testButton.onClick.AddListener(RequestChangeTurn);
 }

 public void RequestChangeTurn() {
     if (localPlayer.getID() != GameManager.instance.currentPlayerTurn) {
         GameManager.instance.SetMessage("It's not your turn! It's " +
              GameManager.instance.players[GameManager.instance.currentPlayerTurn].Name +
             "'s turn!");
     }
     else {
         CmdChangeTurn();
     }
 }

 [Command]
 public void CmdChangeTurn() {
     ChangeTurn();
 }

 [Server]
 public void ChangeTurn() {
     int turn = currentPlayerTurn;
     currentPlayerTurn = (turn + 1) % numPlayers;
 }

 public void DisplayWhoTurn(int currentPlayerTurn) {
     // messageBox.text = "It's " + currentPlayerTurn + "'s turn!";
 }

 public void AddPlayer(Player player) {
     players.Add(player);
     numPlayers++;
 }

 public void RemovePlayer(Player player) {
     players.Remove(player);
     numPlayers--;
 }

 [Client]
 public void SetMessage(string msg) {
     messageBox.text = msg;
 }

Then I also have a NetworkManager class that boots everything up on starting.

 public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) {
     for (int slot = 0; slot < MAX_PLAYERS; ++slot) {
         if (players[slot] == null) {
             GameObject playerObj = GameObject.Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
             Player player = playerObj.GetComponent<Player>();

             player.Name = slot.ToString() + "-a";
             player.setID(slot);
             players[slot] = player;
             GameManager.instance.AddPlayer(player);

             Debug.Log("**** Adding new player with id: " + slot);
             NetworkServer.AddPlayerForConnection(conn, playerObj, playerControllerId);
             return;
         }
     }

     conn.Disconnect();
 }

If anybody can see what I'm doing wrong or assuming incorrectly, I'd be super appreciative to hear your words of wisdom! Thanks in advance.

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

160 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ClientRPC not called from command on client instance during Start() 0 Answers

Client can't answer to Server, Command methods don't work, ClientRPC do 0 Answers

How to command dedicated server 1 Answer

Network is spawning more objects then desired 1 Answer

Instantiating over the Network, good way? 1 Answer


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