- Home /
why this serialize function not working
hi guys. im using OnSerializeNetworkView for almost every object in my game and it works perfectly.
But when i try to serialize character animations it does nothing. Here is my code:
#pragma strict
var yuru = false;
function Update(){
if(networkView.isMine){
if(Input.GetKey("w")){
yuru = true;
}else{
yuru = false;
}
if(yuru == true){
animation.Play("run");
}
if(yuru == false){
animation.Play("idle");
}
}
}
function OnSerializeNetworkView(stream : BitStream,
info : NetworkMessageInfo) {
var yuruN : boolean = false;
if (stream.isWriting) {
yuruN = yuru;
stream.Serialize(yuruN);
} else {
stream.Serialize(yuruN);
yuru = yuruN;
}
}
all of my other objects work with this code. (not animations just variables, like burning a fire).
Answer by Benproductions1 · May 17, 2014 at 08:17 AM
If you desk-checked your code, the problem would have shown itself. Lets assume your serialization worked perfectly and were on the "client" side, someone who doesn't own the networkView
. What would happen on Update
?
If you look at your code, the answer is: absolutely nothing. All code in your Update
function is wrapped in an if
statement, checking whether the networkView
belongs to you.
Also note, that instead of doing:
if (foo == true) {
/*code*/
}
if (foo == false) {
/*code*/
}
You can just do:
if (foo) {
/*code*/
} else {
/*code*/
}
Which is much cleaner and easier to maintain. You should also put spaces in your if
statements (as above), simply because it makes it more readable.