Learn About RPC's and PhotonView for use in Unity

Published: 18 August 2019
on channel: Zenva
19,760
405

ACCESS the FULL COURSE here: https://academy.zenva.com/product/mul...

TRANSCRIPT

Hey everyone, in this lesson we are going to go over two important concepts before we continue on with our project. And these two different concepts are going to be very important in actually being able to relay information over from one computer to another inside of Photon. The first is Remote Procedure Calls, or RPCs. RPCs allow you to call functions on other player's computers. So just like on your computer, when typing code you would just go FunctionName, and then brackets. That is how you will call a function, for example. But let's just say you wanted to call a function, for example, you have an enemy and you wanna damage them and you wanna call the take damage function on them. Well, normally you would just call their take damage function, but in order for that player to actually have that function be called on their computer, you need to do something. You need to call an RPC, and here is how we can do it. There's two different ways to do an RPC. The first one is you go PhotonView, we'll get to that soon. Dot RPC, and then in brackets, you have the name of the function as a string, and then comma, and then you enter in the player you wanna do, you want to send it to. And this is a Photon player. Remember where you had the player class, which is the Photon Player? And that is the person we want to send this to. And then you can have some parameters there if you do have parameters. The other way, though, is sending it to an RPC target, and this is not extending it to an individual person, but sending it to a group of people or a specific group of people in a certain way. Here are all the different RPC targets. We have All Via Server, Others, Master Client. All Buffered, All Buffered Via Server, and Others Buffered. All executes the function instantly on your computer and sends that function out to everybody else inside your room to call that function on their computer. All Via Server sends that function call up the server, and then from the server it tells everybody, including you, to call that function. Others sends that function to everybody else but you. Master Client sends that function to the Master Client. And then we go into the buffered. Now, what buffered means is that it will actually cache that function call, so what it means is that, let's just say you call the change color function, for example, just a hypothetical function, and it changes your color to blue. You could call that with All, for example, but what if somebody was to then join the game after you call that? You join the game and, for them, for you everyone's blue, yet for them nobody's blue. The way to have a function call for them that has already been called is to set it in All Buffered.` And what this does is cache that function for when when new people join the room, it will call that function on their computer. So you've got All Buffered, and then you've got All Buffered Via Server, and then you've got Others Buffered, okay? So that's pretty much the gist of RPCs. We'll get into those in more detail when we start actually using them. But for now, this is just a brief overview on what they do and how we are going to used them in the project. And we are going to use them quite a lot in the project. Okay, so let's move on to our next concept, which is the PhotonView. Now a PhotonView is a component which identifies an object across the network. This, if you want to call an RPC to an object, that object needs a PhotonView. Because in Unity, at the moment, how does Photon know which objects are the same across the network? Each game object does have its own instance ID for the game object, but that is randomly generated and it is actually different, of example, of you had two instances of the game launched, and you had to spawn in an object, that object would have a different instance ID from the other. So at the moment, there is no real way for Photon to know which objects are the exact same across different players' computers.


Watch video Learn About RPC's and PhotonView for use in Unity online without registration, duration hours minute second in high quality. This video was added by user Zenva 18 August 2019, don't forget to share it with your friends and acquaintances, it has been viewed on our site 19,760 once and liked it 405 people.