- Home /
how to Stream.serialize(stringValue)
I know Stream.serialize does not sync string value...
actually, in my multiplayer, I want to get some variable from other's player by pointing raycast to instantiated player(remote) , the problem is variable won't syncronized between local player and instantiated remote player on other computer , and raycast getComponent returns null...
so if it impossible, please help me to figure this by RPC,in this situation (raycast selecting)? any help is appreciated.. :D
edit:
OK, Sorry.. my achievement is getting "status" directly from raycast
As selector, I put this script on Camera:
function Update(){
var ray : Ray = camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray.origin, ray.direction, hit, 100)) {
////////////reading status other player///////////////////
if (hit.collider.tag=="player"){
Debug.Log(hit.collider.gameObject.GetComponent("PlayerData").Status);
}
}
}
2.and in player prefab i added PlayerData.js and attach Network view observed to this :
var statusUpdated:boolean;
var Status:String; function
OnSerializeNetworkView(stream :BitStream, info : NetworkMessageInfo){
if (statusUpdated==false){
statusUpdated=true;
stream.Serialize(this.status);
}
}
and the problem still same,serializing string value on the last line.
What's the code you're using? How about posting the relevant parts? Anyway syncing a string value each Network frame and using GetComponent doesn't make much sense.
You didn't tell us anything about what you want to achieve. Think about those things before you post a question. We know only the things you've posted here.
Answer by Bunny83 · Apr 03, 2012 at 12:30 PM
Never use the string version of GetComponent. The compiler can't determine the type of the returned component so it's just a Component. Use
hit.collider.GetComponent(PlayerData).Status
Beside that have you checked if the string is synced in the inspector? Make sure you observe the script component and not the Transform component. You have to drag the script instance onto the observed variable.
edit
RPCs have to be send manually when you want to update a value. Don't send it each frame or something like that. You should send it whenever you change the status. The best way is to use a function to set a new state.
var Status:String;
function SetState(newState : String)
{
Status = newState;
networkView.RPC("OnReceiveState", RPCMode.Others, Status);
}
@RPC
function OnReceiveState(newState : String)
{
Status = newState;
}
Whenever you want to change the Status variable use SetState("my state");
instead of Status = "my state";
okay , actually using GetComponent is also worked nicely.
the problem is only about DATA TYP$$anonymous$$ PlayerData script even failed on comple, because serialize don't recoginize string value. it's only support bool, char, short, int, float, Quaternion, Vector3 and NetworkPlayer NOT STRING.
but when i experiencing to changing "status" to Int, it's successfully compiled and synced.
now, how about using RPC?
What are the string for? Can't you use an enum (cast to int during serialization)?
Yes, i've just checked the docs and there's no support for strings in the BitStream class. I will add an RPC example.
Answer by Kryptos · Apr 03, 2012 at 11:22 AM
Did you read http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnSerializeNetworkView.html ?
OnSerializeNetworkView should be made in two parts (reading and writing) and you cannot directly use a class variable but must use a local variable instead (it is more visible in C# where the ref keyword has to be used).
OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo) {
var _status : String;
if (stream.isWriting) {
_status = this.status;
stream.Serialize(_status);
}
else
{
stream.Serialize(_status);
this.status = _status;
}
}
This doesn't make any difference. It sould work the way he did it. I've build a whole multiplayer hack&slash game with only one NetworkView and did it almost like that.
.Serialize
will either write or read the value depending on whether it's the owner(send) or not(receive).
I don't think String is supported by Serialize(). I think an RPC is the way to go for this.
Your answer
Follow this Question
Related Questions
Did not find target warning when using uNet 3 Answers
SyncVar issue 0 Answers
Is there a way to create a shared variable between all the clients over network? 1 Answer
How to sync a varible managed by the clients using UNET? 0 Answers
Send Variable. 1 Answer