VRChat UdonSharp: Basics Of Networking

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so I got a question in my intro to udon Sharp video from Clayton HD how could I make it that the mirror toggles for everyone in the world when one player toggles it to make the mirror toggle for all players at the same time we're gonna need to use a network event networking in general can be tricky to understand at first so I'm going to explain some Basics when you and a friend join a world in VR chat your computers download one copy each of that world to use you and your friend are not in the same world but are in identical copies of that world and the other person's Avatar is transposed into your copy of that world on your computer without networking if you do anything in that world like open a door or pick up an object your friend will not see that change in the world you're making if you want people to see and experience things at the same time you need to network that object or event in this case toggling a mirror on and off is an event that needs to be networked I'm going to add a network event to the mirror so all players can see when it's turned on and off using the basic mirror toggle script that I set up in the intro to food on Sharp video in the interact method on a new line I can add send custom Network event and when I add an open parenthesis I get a few options VRC udon com interface Network event Target and that followed by period all and period owner varieties the all and owner types are the ones you can actually use all means that everyone in this world will receive the event or command owner means that only the object's owner is going to receive the event or command the object owner is almost always a player has been in the world the longest or is the last player to pick up that object since the goal is to have all players see the mirror toggle at the same time we're going to use all after all add a comma with two double quotation marks in between this quotation marks we're going to add the name of the method we would like to call and have every player do at the same time right now we only have the interact method so we need to make a new method to place all of the original mirror toggle code into and take it out of the interact method this is so when the sphere is interacted with it will send a custom Network event out to everyone instead of just toggling the mirror for that one person who clicked it under the interact method type public void this sets up your method and now we just need to name it you can name it whatever you want in this case I'm calling it mirror stuff just like naming variables there can't be any spaces I'm using Pascal case for naming my methods Pascal case capitalizes the first letter of every word it's not necessary to use Pascal case but getting used to it will help you read your own code and other people's code after the name of the method add an open and close parenthesis and an open and close curly brackets cut and paste your if-all statement from the interact method into the new method between the curly brackets now on your custom Network event add the name of your new method in my case it's mirror stuff save the script and test it out because I'm testing a networking event I need to build two test clients I can do this by changing the number of clients to 2 and the local testing panel beware the more clients you launch the more demanding on your computer it'll be and there we go it works but does it really if someone joined late to the world after the mirror was already turned on there would be out of sync with everyone else the mirror would be on for players that have been in the world for a while but it'd be off for the new player and every time the mirror toggles it would toggle for everyone and be the opposite for the late joiners to fix this we're going to use a udon synced variable and the on deserialization method when a player joins a world and they're not the only person in that world the on deserialization method gets triggered we can add the Andy serialization method to our code and have the mirror active State change to match the players who have already been in the world longer to add the method on a new line type public override void on deserialization open and close parentheses and open and close curly brackets in this method we can copy the mirror stuff methods code mode and paste it into the Andy serialization method however when the late jointer joins it will check if our mirror is on bull is true or false then change the mirror state to the opposite of that we want the mirror to be set to the same state as the mirror is on bull so we need to change the if condition from if mirror is on equals false to if mirror is on equals true also since the point of this method is set the mirror to the same state as others already in the world we should take out the lines that set the mirrors on bull to true and false lastly we need to make the bull mirror is on into a udon synced variable up at the beginning of the script where we stated the mirror is on variable we will need to add udon sync to enclosed in brackets at the beginning of the variable now every time the script is synced the mirror is on variable will be synced across all users to match the owner of the object the script is on in this case it'll be the person who has been in the world the longest don't forget to save your script back in unity under the toggle food on Sharp script you'll find a synchronization method option with three options none continuous and manual none will ignore any network events or synced variables we don't want that continuous will sync variables and run on deserialization at regular intervals I've seen it run a little faster than once a second we could use this but it will continue to run on deserialization when we don't need it to and lastly manual will run on desolization when someone joins the world but not again unless you specifically ask for things to be synced in your script by running the request serialization method since in this case I only need the mirror to be synced once and every time the mirror is toggled a network event will tell each player to change the mirror's active state I'm going to choose manual and that's it now you have a synced mirror that everyone in the world can see when it's toggled on and off this can be applied to any object that you need to sync whether it's active or inactive thanks for watching
Info
Channel: Zorgy Baby
Views: 3,518
Rating: undefined out of 5
Keywords:
Id: MOw8a5RmMjU
Channel Id: undefined
Length: 6min 1sec (361 seconds)
Published: Fri Oct 07 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.