- Home /
OnSerializeNetworkView NOT sending a bool value when button is pressed???
Is this supposed to be normal?
I just made a simple test and it seems OnSerializeNetworkView is not sending a bool value when a button is pressed. I've checked all variables and everything is ok because I switched back to RPC and it sends the bool value just fine.
Could someone please check this with me?
using UnityEngine; using System.Collections;
public class Character_Network_Script : MonoBehaviour { public bool this_is_client = false; public bool test; public bool received_test; public bool test_result;
void OnNetworkInstantiate(NetworkMessageInfo info)
{
if (networkView.isMine)
{
this_is_client = true;
}
}
void Update ()
{
test = false;
received_test = false;
test_result = false;
if (this_is_client)
{
if (Input.GetKey (KeyCode.Alpha5))
{
test = true;
}
}
}
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
if (stream.isWriting)
{
stream.Serialize(ref test);
}
else
{
stream.Serialize(ref received_test);
test_result = received_test;
}
}
}
Answer by Bunny83 · Apr 27, 2011 at 10:47 PM
I think your problem is your Update method. Every instance is setting test
to false, but only the owner can set it to true. If the others receive a network update test will turn true, but in Update it get set back to false. I would use such an Update function:
void Update ()
{
if (this_is_client)
{
test = Input.GetKey (KeyCode.Alpha5);
}
}
I don't even get what's test_result is good for? And received_test doesn't need to be a member variable. That's how i would sync the test variable:
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
bool tmpTest = test;
stream.Serialize(ref tmpTest);
test = tmpTest;
}
Bunny, thank you for your answer... Sorry if I'm way off here, I'm no experienced programmer. The idea here is: Only Player owner of this script/object can set test to true, if not, update keeps this value false. On the other hand, if not owner, that is, it's an instance of this object running in another client, then receive value for test via serializenetworkview. I've made these variables public members so I can watch their values in the inspector running Unity.
in your code, if player doesn't push key, or pushes it and releases it, how will value return to false?
In my script on the owners PC in Update test get set to the state of the key. Get$$anonymous$$ey returns a bool that is true when the key is down and false if not. The owner is the only one who can send data. Thats why you don't need to check for isWriting/isReading in my case. On the owner OnSerializeNetworkView gets called with isWriting so the real value of test gets copied into tmpTest and get serialized. The last line is irrelevant for the owner since Serialize don't change the value. On the other players Serialize will set tmpTest to the received value and the last line updates the actual variable.
Thanks again, what's happening is that server can see this variable value change watching other clients, but clients can't see this change watching other clients. I still don't get it.
Answer by Caps · Apr 28, 2011 at 09:29 PM
Just did some final tweaks and it works now.
Thank you very much for your help! :D
Your answer
Follow this Question
Related Questions
Space Invaders style game bug. 1 Answer
BUG? Camera rotation on x set to 90! 0 Answers
Unity physics bug? 3 Answers
Bug. Player can't move. 3 Answers
Two Objects Touching??? 1 Answer