Issues with RPC...

Options
Okay, so I'm trying to build a basic chat window with Unity 4.6.. It was a little easier doing network chat back with the old Unity since players held all of the code for the GUI in their own scripts, or at least, attached to their respective gameobjects.. But now, since the GUI are their own gameobjects, it makes things a bit more difficult..

I've gotten the ChatWindow made, and it looks great! But now i'm trying to get it to work and it seems to not be doing so lol..

Here's the code I've gotten for it so far:

In the NetworkCharacter script, I keep a local List<string> of all ChatMessages.
public List&lt;string&gt; chatList;

void Update()
{
//Handles the opening and closing of the chat window itself.
if(photonView.isMine)
		{
			if(!inputField.isFocused)
			if (Input.GetKeyDown (KeyCode.T)) 
			{
				chatWindow.SetActive(!chatWindow.GetActive());

				chatOpen = chatWindow.GetActive();

				if(chatWindow.GetActive() == true)
				{
					chatWindow.transform.FindChild ("Scrollview").FindChild ("Panel").GetComponent&lt;ScrollableList&gt; ().interactingPlayer = this;
					inputField.Select();
					inputField.onEndEdit.AddListener((value) =&gt; AddToChat(value));
					this.GetComponent&lt;PlayerMovement&gt;().canMove = false;
					this.GetComponent&lt;PlayerCombat&gt;().canShoot = false;
					chatWindow.transform.FindChild ("Scrollview").FindChild ("Panel").GetComponent&lt;ScrollableList&gt; ().RefreshChat();
				}
				else
				{
					chatWindow.transform.FindChild ("Scrollview").FindChild ("Panel").GetComponent&lt;ScrollableList&gt; ().interactingPlayer = null;
					inputField.onEndEdit.RemoveAllListeners();
					this.GetComponent&lt;PlayerMovement&gt;().canMove = true;
					this.GetComponent&lt;PlayerCombat&gt;().canShoot = true;
				}
			}
		}
}

//Methods for adding a local chat message, and then RPCing the chat message to other players
void AddToChat(string text)
	{
		//chatList.Add (text);
		string&#91;&#93; s = { text };
		this.photonView.RPC ("PhotonAddChat", PhotonTargets.AllViaServer, s);
		chatWindow.transform.FindChild ("Scrollview").FindChild ("Panel").GetComponent&lt;ScrollableList&gt; ().RefreshChat();
	}

	&#91;RPC&#93;
	public void PhotonAddChat(string text)
	{
		string theText = text;

		if (theText == string.Empty)
						return;

		Debug.Log (theText);
		chatList.Add (theText);  //This SHOULD add the chat message to the other players chatList... right?

		if (chatOpen)
		chatWindow.transform.FindChild ("Scrollview").FindChild ("Panel").GetComponent&lt;ScrollableList&gt; ().RefreshChat();
		RPCReceived = true; //Just a bool I made to see if my other players were actually receiving the RPC... It is not turning to true.
	}


Here's the code for my chat window:
public void RefreshChat()
	{
		if (!interactingPlayer)
						return;

		ClearList ();
		
		RectTransform rowRectTransform = itemPrefab.GetComponent&lt;RectTransform&gt;();
		RectTransform containerRectTransform = gameObject.GetComponent&lt;RectTransform&gt;();
		
		//calculate the width and height of each child item.
		float width = containerRectTransform.rect.width / columnCount;
		float ratio = width / rowRectTransform.rect.width;
		float height = rowRectTransform.rect.height * ratio;
		int rowCount = itemCount / columnCount;
		if (itemCount % rowCount &gt; 0)
			rowCount++;
		
		//adjust the height of the container so that it will just barely fit all its children
		float scrollHeight = height * rowCount;
		containerRectTransform.offsetMin = new Vector2(containerRectTransform.offsetMin.x, -scrollHeight / 2);
		containerRectTransform.offsetMax = new Vector2(containerRectTransform.offsetMax.x, scrollHeight / 2);
		
		int j = 0;
		for (int i = interactingPlayer.chatList.Count - 1; i &gt; 0; i--)
		{
			string theText = interactingPlayer.chatList&#91;i&#93;;
			
			//this is used instead of a double for loop because itemCount may not fit perfectly into the rows/columns
			if (i % columnCount == 0)
				j++;
			
			//create a new item, name it, and set the parent
			GameObject newItem = Instantiate(chatPrefab) as GameObject;
			newItem.name = gameObject.name + " item at (" + i + "," + j + ")";
			newItem.transform.SetParent(gameObject.transform, false);
			newItem.transform.FindChild("Label").GetComponent&lt;Text&gt;().text = theText;
			//newItem.transform.FindChild("NameText").GetComponent&lt;Text&gt;().text = currentRoom.name;
			//newItem.transform.FindChild("PlayerNumText").GetComponent&lt;Text&gt;().text = currentRoom.playerCount + "/4";
			
			//move and size the new item
			RectTransform rectTransform = newItem.GetComponent&lt;RectTransform&gt;();
			
			float x = -containerRectTransform.rect.width / 2 + width * (i % columnCount);
			float y = -containerRectTransform.rect.height / 2 + height * j;
			rectTransform.offsetMin = new Vector2(x, y);
			
			x = rectTransform.offsetMin.x + width;
			y = rectTransform.offsetMin.y + height;
			rectTransform.offsetMax = new Vector2(x, y);
			
		}
		
		StartCoroutine (ResetSlider ());
	}


Essentially, all the ChatWindow should have to do is refresh the list of chat messages that have come through. I'm trying to effectively use "separation of concerns" here, where my Graphics layer only has to worry about the graphical side of things, and all of the actual raw data is held on the coding layer.

Comments

  • I just thought about this, but would it be best to just keep a static List<string> maybe? Idk exactly what the issue is lol....
  • vadim
    Options
    So what's are the issues with RPC? It's not clear from the post. Does PhotonAddChat get called properly on every client?

    Also consider using Photon Chat SDK: http://doc.exitgames.com/en/chat/curren ... dk-unity3d
  • Tobias
    Options
    epictickle: I don't think the question is really Photon-related. It's more about how to keep messages and how to show them in the new GUI, right?
    For those questions, this is the wrong forum. You will get more help in the Unity forum for UI, I hope. We have to focus only on Photon questions.