Synced Toggle 2022 | VRChat Udon Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] all right so let's set up a sync to toggle for some object in our scene i set up one of these previously but there have been a lot of updates since then that make this a lot more efficient so let's get started i have a button that i'm using right here as my button and it just has a box collider on it for interacting so let's set up a graph for it we'll do add component boot on behavior and on here we can press new program but i'll put it in a different folder than you probably want it so i'm going to put it in my object toggle folder right click here create vr chat udon udon graph program asset now i'm just going to call this synced toggle now if we go back to our object in our scene we can just drag this into the slot and press open udon graph we have two things that are going to be changing or that will be variables for this one is a saved state of this object being on and off and two would be the game object that we're actually toggling on and off so let's go ahead and make those variables right now we'll make a bool and i'll just call this state and if we hit the drop down here we'll just press public and synced and public will just let it show up in the inspector after we hit compile and synced means that it will sync to other players next we need our game object so i'll make this a little bit bigger and type game object and i'll just name this target if we hit the drop down here we can also hit public and hit compile and we can see here that it appears in our inspector so we'll just drag orb into here just the object i'm going to be toggling and then we have a reference there so now we need to actually set up the logic for this event since this is going to be when we interact with something being the button we need the interact event so space to search and type interact and there we go now since state is a sync to value on this script only the owner of that information can be changing it so we need to set ourselves to be the owner whenever we interact with this similarly to the last video we'll just do networking set owner plug the flow in right there and we need the local player to be doing this so drag out from player and type local and the only one that shows up is local player just select that if we leave object empty it will be referencing whatever object this script is on which is perfectly fine but i like to populate that reference just so it's easier to read so i'll just drag out from here and type this and that way it references this object which just has a node for it instead of being empty all right now that we're the owner we're going to switch the state of the state and since it's a bool we just need to set it to be true if it's false or false if it's true ctrl drag out from our bool in our variables box to get a set to variable node we'll connect in the float here and we need to get the opposite value here drag out from state normally to get a reference to it and to get the flipped value we just type unary negation bit of a long word but should work for us connect that into value so it flips it and then sets it to the inverted value and now we have a bool with a different value so we need to actually set this to do something like we have our sync to state but we're not doing anything with that state yet to do that we drag out from target we'll drag out from there to get set active and here we need to actually give it our state bool let me just plug that in here however our flow isn't connected to anything so nothing will actually call this so we could put it here but the way that we sync this with other players is we need to be able to basically run this twice and in the event we want to change this instead of having the same set of nodes in two different locations it would be easier just have it in one location and call it for both instances that might seem a bit complicated but i'll walk you through it here specifically for this implementation we're going to be changing to manual sync now if we look at our behavior over here in our component the first thing at the beginning is synchronization and since it's set to continuous right now it sends out all sync to data four times a second which is way more than we need and way less efficient than we want to be having we're going to set this to be manual so that it only sends out information when we want it to which means it's happening much less often than it need listening has to but also happens almost instantly instead of waiting for that next period with that change we can go back into our graph and add the request serialization node now request serialization basically translates to send out information so whenever this node gets run it takes any synced information that we have say our synced bull over here and distributes that to all other players we'll connect that in here and now when other players are receiving information that is in the on deserialization event again request serialization is sending out the information and on deserialization is when you're receiving the information on deserialization and with this node we want to be calling this section over here but since we want to be calling it from up here as well we're going to have this in its own custom event so we'll do event custom and i'll call this apply toggle connect the flow up there so that this actually will run with that set up we want a send custom event node so we'll do send custom event and this will give us a list of all of the different events that are on our script if we hit compile we'll make sure that this is all fully loaded and the drop down here will let us select apply toggle now this is basically saying when someone receives this information run this event but since we're the ones who are distributing this information we're never receiving that information so we also need to run send custom event up here so basically instead of having to duplicate this entire set of nodes for whatever changes we may have in the future we're only duplicating a single node that is calling this set of nodes here all right shift space to leave full screen and we can actually hit play to test it out right now i'm using the creator companion which is set up with client sim and i'm going to walk forward and go press this button and it should enable our orb here and there you go you see it's spinning there and that should be synced as well of course that doesn't show up in client sim since it's only one player so let's actually go ahead and check this out in vrchat in the sdk builder tab we can actually go and set our number of clients to be two so that we can launch multiple local users instead of just one hit build and test and now we wait all right so with vrchat launched up now i'm gonna put one to the left one to the right and we'll start out with the one on the left but this implementation is actually immediately compatible for late joiners so i'm on the left going to run up and press the button and then the one on the right will join the world and should see exactly the same state and then be able to change the values on their own so if i come up here hold tab i can press the button and there you go you see it's spinning around i'll leave that on swap over to this one and as well i load in and can see the object on as well i can also come up and press the button and you'll see that it toggles on and off for both players perfect so that's the synced toggle and graph now let's go back and set it up in u-sharp as well i'll just open up the graph so we can reference it while making our u-sharp script and down in our project window i'll just right-click create u-sharp script call this synced toggle sharp so that i don't have naming conflicts and we can just open that up as soon as it's done compiling now for some initial setup we don't need our start event we'll just clear that and i'll put this in a namespace as always so that if i have this in a project that someone else already has a script named sync toggle sharp it won't conflict so i'll just have namespace and open close brackets around the whole script now remember how we selected manual sync in the inspector with u-sharp we can actually declare which type of syncing that this will be using regardless of what we said in the inspector we can just enforce it and that's with a attribute an attribute is just something before something else you put square brackets around so inside of here we're going to do udon behavior sync mode and in parentheses we'll select manual close the parentheses and this just enforces that this script will always use manual syncing now if you take a look at our variables here let's go ahead and replicate them first one is a public bool state so we'll just type public bool state and the second one is a public game object so public game object target now to set state to be synced similarly we use an attribute we put our square brackets in front of it and type udon saint and there you go this just declares that this value will be synced the same as pressing this check mark over here now let's start with our interact event now interact is a vrchat event so we'll have to override it which is just with public override then we can type interact and it should autofill we don't need the base dot interact line so we can go ahead and clear that and the first thing we do over here as well is set ownership so we'll do networking dot set owner and in here we'll pass it the local player so networking.local player and then we'll pass it this game object as well just type game object with lower g and that references the game object that this script is on now that we've set up ownership we can just flip the value of state so we'll set state to equal the opposite of state and that exclamation point just declares opposite following that we just do request serialization which should autofill and has parentheses and a semicolon and the line of course now we need to actually set up our apply toggle event here so that we can call it so since this doesn't need to be called from any other scripts we can do a private void fy toggle so this one will just be setting how target is so we'll do target dot set active parentheses state now we can call that immediately after request serialization apply toggle and proceed to semicolon but now we also need a setup for on deserialization as well so since this is a vrchat event as well we'll need to override it so public override on deserialization again we don't need the baseline and all we need to do is call apply toggle once again and there we go that is the entire u-sharp script for this as well and let's go ahead and implement it and test it in vr chat as well a quick swap out our udon behavior here we can actually do add component and type synced toggle sharp because it's using a c-sharp script as a back-end we can actually just search for it right here now let's grab our orb put it in there and we can actually just immediately go to the vrchat sdk build and build and test with two new clients all right and just like before i'll run up with one client and press the button so it toggles the state and then we can have the other player come in and observe that is toggled as well and there you go it is toggled on both players as well i can come up and toggle it as well and then the other player can toggle it back and this syncing works perfectly fine on both ends and there you go that's it for this one and i hope you enjoy we'll see you next time [Music] you
Info
Channel: Vowgan VR
Views: 5,483
Rating: undefined out of 5
Keywords: VRChat, Tutorial, Udon, UdonSharp, U#, Unity, Unity3D
Id: K3kAGQvrH9A
Channel Id: undefined
Length: 12min 28sec (748 seconds)
Published: Wed Sep 14 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.