The question is answered, right answer was accepted
[Photon Networking] Passing My Player scripts 'Target' object to the Uncontrolled version of My Player
Helllooooo!
I am quite further a long in my project now, just trying to setup some multiplayer. I have pretty much everything working, that I need so far... Except... well, the targeting!
If I play on my computer, and my mobile at the same time, it works awesome. My players are both there, and they are in real time, and they are moving how they should.
The problem is with the NPC's in the game.
If I for instance, select an NPC in my game, and then choose to attack it - all from my mobile.
My player on both screens, will go to the target, however only one will start attacking the NPC.
The movement is being tracked, but not the scripts variables - which the targets are GameObjects in this case.
Now I did some testing with other variables, and things like Strings, Booleans and Integers... Even Vectors work. But GameObjects seem not to. So at the moment, I have specific GUI and everything popping up exactly how it should and health bars and levels remain the same across all clients. But NOT FOR TARGETING TARGETS! D:
I have tried a few methods, one which seems to sometimes work, so I don't think it's down the correct avenue of code.
Is there any way to send GameObjects through a network AS a variable, so that the uncontrolled viewer of my player on the client of another person, can then be fed the correct -updated - information.
Any help or links are appreciated, I've been searching for 2 days and have only got so far.
Cheers,
Justin!
$$anonymous$$ay have figured out an answer to my own question, but help is still appreciated. Will keep you updated!
Nope. That wasn't it. Anyone have any bright ideas? Come on.
Hi,
$$anonymous$$y player on both screens, will go to the target, however only one will start attacking the NPC.
This sounds like you forgot a photonView.is$$anonymous$$ine
condition somewhere.
Perhaps, perhaps. But that seems a little too obvious.
This is my current code.
void Update () {
if (photonView.is$$anonymous$$ine) {
//do nothing...
} else {
if (target_object != null) {
GetComponent<Player> ().target_object = target_object;
}
GetComponent<Player> ().username = realUsername;
GetComponent<Player> ().current_health = currentHealth;
GetComponent<Player> ().isDead = isDead;
GetComponent<Player> ().is_player_being_attacked = is_player_being_attacked;
transform.position = Vector3.Lerp (transform.position, realPosition, 0.1f);
transform.rotation = Quaternion.Lerp (transform.rotation, realRotation, 0.1f);
}
}
void OnPhotonSerializeView(PhotonStream stream, Photon$$anonymous$$essageInfo info) {
if (stream.isWriting) {
//this is YOU.
//Debug.Log(gameObject.GetPhotonView().viewID);
//stream.SendNext(GetComponent<PhotonView>().viewID); //sending my viewer id. I am the sender.
if (GetComponent<Player> ().target_object != null) {
if (GetComponent<Player> ().target_object.transform.tag == "tree") {
stream.SendNext (GetComponent<Player> ().target_object.GetComponent<TreeAI> ().game_object_id);
}
}
stream.SendNext(GetComponent<Player>().username);
stream.SendNext (GetComponent<Player> ().current_health);
stream.SendNext (GetComponent<Player> ().isDead);
stream.SendNext(GetComponent<Player>().is_player_being_attacked);
stream.SendNext(transform.position);
stream.SendNext (transform.rotation);
} else {
//this is someone else...
//int viewer_id_of_sender = (int)stream.ReceiveNext (); //getting the viewer id from the sender.
//GameObject senders_character = PhotonView.Find (viewer_id_of_sender).gameObject;
//Debug.Log ("Viewer is: " + PhotonView.Find (viewerId).gameObject);
//Debug.Log ("Target object is: " + PhotonView.Find (viewerId).gameObject.GetComponent<Player>().target_object);
//target_object = senders_character.GetComponent<Player>().target_object;
if (GetComponent<Player>().target_object == null) {
int temporary_game_object_id = (int)stream.ReceiveNext ();
GameObject gameHandler = GameObject.Find ("GameHandler").gameObject;
target_object = gameHandler.GetComponent<GameObjectHandler> ().gameobjects [temporary_game_object_id].gameObject;
Debug.Log ("The receiver is now Targetting " + target_object);
}
realUsername = (string)stream.ReceiveNext ();
currentHealth = (int)stream.ReceiveNext ();
isDead = (bool)stream.ReceiveNext();
is_player_being_attacked = (bool)stream.ReceiveNext();
realPosition = (Vector3)stream.ReceiveNext ();
realRotation = (Quaternion)stream.ReceiveNext ();
}
}
well, that's this part of it anyway. It semi works. I need a better way to pass game objects through the script to the same script on another client.
Do you actually need to send the game object across the network? Which additional information are required except of the few shown in the code snippet, because using OnPhotonSerializeView(...) should be fine in this case. Besides I think you don't need the Update() function to set the received values, you can set them directly when those are being received (except position and rotation when using Lerp function). Another way to send variables across the network is to use RPCs and also possible in this case.
If you still want to send a game object, you can try registering a custom type in Photon. You can find an example here.
Answer by OfficialCoatsee · May 07, 2017 at 01:58 AM
So, I figured it out. The code I used is as follows.
Call Method:
if (target_object.GetComponent<PhotonView> () != null) {
int target_id = target_object.GetComponent<PhotonView> ().viewID;
photonView.RPC ("networkSetTargetObject", PhotonTargets.All, target_id);
}
The above method triggers the RPC displayed below. It sends the viewID of the GameObject in question.
[PunRPC]
void networkSetTargetObject(int view_id) {
if (view_id != null) {
if (photonView != null) {
GameObject testSubject = PhotonView.Find (view_id).gameObject;
target_object = testSubject;
}
}
}
You don't need all the if statements in the networkSetTargetObject() function, I don't think. But they have prevented a lot of errors from coming up during runtime.
What we are doing - is getting the gameObject that the passed over the network viewID is assigned to.
And that's it, this works, all done.
Follow this Question
Related Questions
Photon Networking - How to move all players from game scene to current room scene? 0 Answers
How to choose different teams one by one 1 Answer
How to sync RPC function Photon Network? 1 Answer
ClientRpc/Commands between two seperate projects? 0 Answers
uNet. Spawning objects over the network. 0 Answers