Unity 2018 : Multiplayer LLAPI Tutorial - Updating your followers 9/9

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey boys we are on last episode of this tutorial we made it that far night's times actually closed things up in terms of code then we have to do the hosting of course but right now we are going to make sure that as soon as one of our personally fellow goes online or offline we are aware of that information so we're gonna send the update to all the people who follow us simple stuff today actually not so simple but it's all things we've done in the past so it's it's simple in that terms and we're gonna start by creating a new message we're gonna have a message that will contain the user were following and all his new updated information so under my net message I'm jumping straight into it like yeah friend update or follow update or update follow maybe mmm really up to you this point we'll be opening up this message of course when we create a new message we'll need a new net operation code and yeah in here we're gonna have the simple account like this so it's gonna be look very very similar to this code over here I'm just gonna grab this one put it here and of course follow update here here here and we remove the success so when somebody would follow gets updated we're gonna take all this information again so username discriminator status most important one and that's it let's go under the net operation code at this new one that's the final message aren't you hyped about that we're almost done boys and girls mostly boys according to analytics yeah okay we got our message now it's time to send it now who's sending that message not the client because the client if he goes offline then he can't send any message he's just he's disconnected the server will be the one that sends this message and it will send it on its own like we don't need any feedback from the client we don't need any input from the client on well we need one right when he logs in we need to send that over to the server and then he's going to propagate that information but when he logs out that's something that is already being treated on the server itself so we got a new message we just need to make sure we send it to the right people and we send it once we log in and also once we log out or disconnect in this case okay so actually here's the thing we have to do in our disconnection event over here in the service es we're gonna have to add the code to propagate information about OSS connecting we're not going to be doing it here in the connection we also have to do it when we connect but that's not the connection we're thinking about that's one the person any user connects the master server well they're not authenticated just yet so it's like just anybody who opens a new tab with your game in it that's the connection event he doesn't have any identity in your game as long as he's not connected so we have to again something here so here we have two again something on the login request if it's successful so over here if it's successful we have to do it here as well and what else we have to do we have one more thing to do that I had in mind a second ago oh yeah we have to omit the status of somebody who disconnects we haven't done that yet so it's again over here now it's going to be a big chunk of code so I'll be creating a function here that will redirect course a little bit further down so let me just call that this this connect event and what shall we send receiving host ID since we're treating with both standalone and also web GL connection ID we're gonna need what else there's no message being sent here because it's a disconnection event all you really need is these two thing I believe I might be wrong let me have a look yeah it's the only two thing I've been using so let me grab that we're going to create that function a little bit below it's gonna be on data in the on data section but at the top because it's basically just like a message we would send ourself however this one's being sent directly from the system and then connection ID okay cool right light it up with my end here okay never mind that connection ID here we're calling that function also grab this one I'll put it here so we free up some space in the Big Data function cool so our connection ready is called CN n IG over here right we're gonna start here we're gonna have a look if the user is connected right now if he is connected let's make sure we remove his status his token and his connection ID now once we're done with that well if he was connected we were also going to send everybody who follows him a message saying that yo he's offline now all right now as I mentioned the connection Evan this connection event by themself doesn't mean that person is connected to your game it only means that they're connected to the master server which means that we're gonna get some disconnection event that will be called even though the person has not input any login information and has not successfully authenticated so we have to make sure that there is actually someone connected there so get a reference to the connected account that could work I guess and we're going to do a find account by nothing we don't have that it's a fine account by connection ID there we go seeing an ID of course we don't have that so we're gonna have to create a new overload under database again we're gonna have a lot of overload for this simple thing but they all work and they all they're all unique by the way let's have a look here public model account fine yeah thanks god I put it in my clipboard all time int connection ID what do we do in here what do the exact same thing as we've been doing with all the other functions a one-liner that simply looks for the active connection in this case and D connection ID there we go good we got what we need well let's head back to server dot CS proceed with this now at this point we have a muddled account account but it might be no which mean nobody was connected with this ik with this connection ID so if account is not equal to null then we're going to proceed else well return right I don't think we need a elf statement here which is gonna put return by itself now this account might be no in case the person was not connected as we've mentioned so if a cat is equal to null let's just return we don't go any further if somebody disconnects there's nothing to change in the database in case he wasn't authenticated so we're just making sure just making sure he was indeed authenticated I'm getting better English every day that's great I'm gonna make more videos and become an English major ok so at this point the person have to be authenticated so their status has to be at least one or higher if using that as a bite a bite flag then they also have a token and a connection ID let's make sure we get rid of all of that we don't want to have any of those sitting in database once the person is offline so database update account after this connection this connection what do we have in terms of information at this point we have a lot of information we have a muddled account so if we go in their account you'll see there's a token which I don't think I'm gonna be using active connection we could be using this one email you just choose really you choose whichever you want in this case I'm not going to use active connection because I'm scared of one thing which doesn't make sense but just assume that at the same time we disconnect somebody logs in on a different tread and we're doing multi-threading he might fake that active connection ID that he might steal that in fact what I'm saying doesn't make any sense but we're gonna try something else and actually find him by email well we're not finding anything with my email we're just turning a couple of fields so I've called the function update account after disconnection which is I think quite explicit nobody should ask me what this what this function does if you look at my code I'm gonna put that in the update function here it is we said we're gonna be taking in the email because email is unique so might as well do that all right now it's time to actually update okay now it's time to actually update the account and the only other place I believe we didn't update is down here once we log in basically so account update hmm we're gonna need first equerry to find it we know how to do it with query here I mean we're able to do a query quite easily if we just still say the fetch query so we can say var query is equal to and we'll do query on mobile account where a email is equal to the parameter we sent at this point we have a query I don't want to do a find one on this because we need the query to update as we've seen in the login account yeah down here right um let's see we also need to have a reference to that account okay so we are going to do a find one I lied once more account it's gonna be equal two accounts the collection find one with the query good so now we have the query the account we need both of them once we do our update let me grab this line of code I'm going to copy it down here I'm liking this I don't even have to look at my code on the left side I just remember stuff now I've been doing it so much three times three time in fact cool so we're going to take our cap I'll bait it and yeah right I count the token let's put that on no I don't want to see it token in there like not at all oh by the way when I'm doing this it's very important you're doing no one you're not doing this if you're doing this then anybody who tries to like do something on behalf of a user with nothing in it will be able to do it that's an actual string so make sure you put it on no don't make the same mistake as I've done in the past right we want to kill the if connection as well back to zero if we put it on zero the game is going to like nobody is ever zero when you start the first user that connects is actually one it's not zero then let's see there was one more thing that I had to kill what was it the status of course put that on zero as well right I believe we got all we need for this connection now and before I go in I code the part where we sent information about our user we're going to test this out really really quickly I'm going to open up my bin the other tab and I've also started my server over here so let's have a look at what is going on I will be logging and using my favorite user one-one-one-one window and let's have a look so I'm under mem M lab and here I just logged in using the account 1 1 1 1 here it is the status is 1 getting tired of saying that um and I'm going to refresh after killing the application ok so active connection is still equal to 1 stead of still equal to 1 definitely something that didn't work there let's have a look at what happened all right so I've just tested it again without changing anything else and now it's back to 0 token is no services to 0 I guess it just didn't save properly the only thing I've changed is I've added a debug dot log just to see it was being called so um it works which mean that if I connect on the account to which I've just did right now and I disconnect if I log in again on account 1 which is the account that follows account 2 then I will not be able to see it online well I just I'm going to see a great thing so yeah here is we managed to turn off the active connection that I had over here on this one if I reset you're going to see active connection 0 token no status 0 amazing good so we've done the first part second part is to send information to our user third part is you send information to the people who are following us we're going to need a a new call instead of the MongoDB because we're never really getting that information out what I mean by that is that the people that follow us we never have a list of that we have fine all followed by user but we got to fine fine I'll follow from does that is that going to you know what this is going to be quite confusing if you're following this tutorial from earlier stage but fine I'll follow from and I'll just put a note here used to be fine all follow by my mistake okay so it's now fine I'll follow from and if I actually compile my code I'm going to see where it crashes because of course this isn't what we've called in in the past here it is double-click on that boom change I should be using an add-on to change that but if I'm actually recording stuff I want to do with there manually so you don't you don't see me do secret key binds and then the code doesn't work somewhere else let's see good now that it's a little bit clearer in my headed place I am going to go create a fine all follow from publicly Civic count it's exactly the same function actually but you're just swapping two thing around should we find by token we could be finding by email again that would work just know that we're never leaving the server so all the information were sending across like that is never gonna be anything sensitive so we could be playing with that account the password all that kind of stuff though I would not recommend but you know the email is never leaving so let me copy this whole thing over here and what we'll be doing is we're gonna be having a look at this and swapping it around so fine account by Tolkien well that's now fine account by email that's my email we're grabbing a reference to ourself so we're getting self that's the person who is going to disconnect or connect we're gonna find him by email and then instead of the query we're not looking for the sender this time we're looking for the follow or the target yeah that's how we called it yeah so everybody who's targeting myself so when I disconnect those people will be the one I want in my query and then finally in our 4-h we are not going to do well this is fine this is fine but we're not going to be finding the target that ID we need to find a sender now we have to find people who sent those requests initially and just like that we swap our function around to find the other person so followed by the real bite is done okay let's grab this and we're gonna be putting it at our two places which let's see wherever we wrote here twice so one was in this connect and also the other one was in login request okay so let's start writing down this function up here in this connect first so here we're going to prepare and send our update message so prepare and sin our update message now something I've just realized that was doing a little bit earlier so if you guys have a look at the code I had earlier this was it right here don't copy that I've recruited a new message I have gotten the account but that's the old account over here and then put his status on zero the reason I was doing that is because the reference to account we have here at the top is old because we're updating it after so what I did is then I manually set things off over here and I kind of think it's not the best way to go about doing this simply because if at one point you decide to add more field and you know then come back here and say okay well new field is equal to disconnected or something like that instead what I'll do is I'll do another fine so like an updated account over here some muddled account updated account and we'll do it fine or a DB dot fine account by email we can use the old account an email it's the same one that's not going to change and then send that one over of course when we send it over we're gonna do a get account so we get lucky a dumbed down version of it I could either do it this way or I could have a return value in here I don't really see you know like I don't mind both option to be on this so I'm simply gonna go with this one since I already wrote it so prepare and send our message that message follow-up date a few nice amazing a few that follow is the updated account in this case updated account get account do we need anything else nope so the rest is normal stuff the operation code now we just need to find who are the person targeted by this update and send that information back to them so we'll do a for each for every follow we in DB dot fine followed by and then we'll find it by email again so account email then we're going to do something about here so we're gonna be sending mass messages however we don't want to be sending messages to somebody who's off line so we have to verify first if F active connection is equal to equal to zero is going to mean that the person is not connected we set that on zero once he disconnect we could also be looking at the status so if the Z if the status is on zero of course don't send him that we could be looking at token if the token is no but that's not something we want to play around with but yeah if it's on zero just hit continue we're gonna have to look for other people basically that have followed this person and then let's see once we're done we send that to the client receiving house ID is platform we'll be setting it through that that is always assuming that your friend are using the same platform as you that is of course a really bad limitation that we get through in the second tutorial and then filing our message right here fu good that's our message let's send that fu message over that's our code so that's our code with this we should now be able to update our friends on the other end of the world okay let's grab all of this and copy that / Nordisk in our login event because it's the same exact thing basically so I've grabbed this in my clipboard I will now go down in the here section we create a new message that's correct we have the updated account okay so we have the updated account at this point it has been modified because we modify it in the login account here at the top so it has been modified it has a new status it has a new connection ID it has all of that and therefore we got all we need from doing if an account by email am i finding the right account right now yep I am and in fact since this one already returns the updated information we're not going to need to have an updated account we can simply send it like that so let me kill this grab a count and say account get account at this point this remains the same and we just sent our information and it's quite windy in my house apparently sorry about that okay so our information should be sent over our updates should be done however we never really treat it on the other hand so we really never do anything with that information on the client-side so we've done one more part and the final one is to come in a moment so let's go ahead open up the client scene or client dot CS and code the very last piece of code we'll code together for this tutorial she all right so it opens exactly where I wanted to be in the on data I'm going to create our new case new case is going to be follow update let's break it and follow update with the cast message so net follow update there it is here we go that's our final function guys in this client this yes I'm saying that but we might as well run into a bug and that's not going to be the last one who knows I'm super optimistic so follow it so follow update that's the DFU thing right and what do we do here we call our big friend the hot scene let's call him obscene instance op date friend update follow that's it follow and let's just sign it if you don't follow good a new function doesn't exist yet so let's take it hub scene we're gonna put that same line as ad follow to UI so public void update follow I can't follow what are we doin here well we gotta find our friend in the UI and then changes is around color but we don't have a reference to that we don't even know which one is which so we could we could actually do you you can actually like take all the texts mesh pro you GUI and then look at the text and look for condition where the username meets that but you know what let's let's do it a little bit cleaner not that it's the cleanest version I'll do here but I'm going to declare a dictionary and that dictionary is going to contain fix your nari and this will this will contain our key and a game object as a value so that key is gonna be username discriminator and then the value is going to be a game object so I call it UI follows because it's really just for the UI and I'll instantiate it right there right on the spot so I don't have to do it in the start right so when we add someone to the UI at the end over here I'm going to say UI follow add the key is going to be I have it over here so follow the username plus pound sign that's my key guys follow that discriminator hopefully you can come up with different ways to go about this but I'm I'm quite okay with this because it's such such a small application so we we add that we had to follow eye them so now we have a reference this game object using the username and also discriminator having that out of the way let's let's end this with something quite quite cool like a one-liner that's going to end it all oh wait wait wait before we do something that cool we're going to remove it from our dictionary if we click on on the remove so I'm assuming we click on remove we're going to do you I follow remove use your name plus pound sign plus discriminator I really thing that I should have created some kind of utility function that just makes those together so it looks a little bit better but okay um yeah let's end it all with one one letter let's do it in one big line so you I follow we're going to open up our dictionary we need to find our reference at this point so that's our reference actually what follow dot use name is it's that and then discriminator same thing here so follow that discriminator okay at this point we have our reference when we want to get the object the image that's what we did that transform get child image and we also wanted to set the color so let's do I'll write it down for Dermot like for dramatic effect get child and x1 get component type of image I mind the way I might be in the way let me just zoom in on that and scroll their necks up color we're trying to get the color out of this oh wait forgot our set of parent YZ color and that color is gonna be equal to remember we have the following guy over here so we know his status therefore we're going to test that in a ternary operation just to make this a little bit longer we're gonna be looking at his status if the status is equal equal to zero that will mean I'll basically have the code over here so I'll just take that oh it's not equal to zero if it's not equal to zero he's online so green or gray that is our one-liner to end it all it's actually not that bad I've did that in my code couple of places almost admit but I'm also coding in my own most of the time so people really give a give a crap about what I do the only exception is when I code on YouTube right so let's try this out guys I am going to go fresh deleting all the accounts deleting all the friendships none of that exists anymore and for dramatic effect let's put that on maximize on play fullscreen this thing the server is started in the background username ABCD password ABCD Africa save the children that I don't know if it's a duck that or a.com anyway save the children okay we have created an account let's create a new account for our best friend this guy and let's log in any of those I don't remember what the password was ABCD so we're logged in let's add our friend through his email because we're doing it a little bit different this time adding him he's not online what a shame that's just so shameful I forgot to actually start a new instance of this well at least we know we didn't crash anything that worked before I'm gonna go build a web instance see that also works on the web so I'll be back in a second actually and maybe fight minute that's what it takes to build I'll be back alright we have returned with a build over here I just started my server I'm going to have to reset this connection here so um I also thought about another problem that we're going to have just a small typo basically so we are going to get a crash however I just want to see if I am able to connect with this and if your thing is good let's just connect with a b c d @ 0 0 0 yep ABC these my password and submit and we're connected with WebGL okay now on this other side i will be connecting with another account and see if we get any problem so it says hello errors are cured using well actually errors are cured now I actually know what the problem is it's it's a stupid typo so if we head over to the server we have two spots in which we have to change this one little parameter is over here so at the place we wrote here so at the place where we're sending our friend update or follow update actually the fu message what I'm doing here is I'm actually looking through all the friends I'm looking if they're online if they are I am sending a message to myself the connection ID so yeah that's that's the issue you want to be sending it to eff thought active connection instead and yes stupid mistake so I also have to do it in the login request which I believe is somewhere here so looking requests down there F active connection fu good so I'm gonna have to yeah I'm gonna have to build this again yay me make sure you save everything before you do go with the build process once more and I will be back in maybe five minutes this time not not five hours all right so we got a new build running after our fix and let's see if that makes it so everything works okay so we see that one one one one one is not online now if I go here login he is online and also added their friends so I can see it in both side let's disconnect this one and he went offline that's it we're done we've completed the thing now the only thing left to do is the hosting hosting is gonna be a whole new story another thing I'll do two videos one of them is gonna be to host in Windows it's gonna be quite simple you just make your build and run it I still gonna have to open port and do that kind of stuff and the other one's gonna be on Linux but don't expect those video to come out tomorrow or the day after I might think a little bit more time to do those as I have some other project in the pipeline and I'm also working on the second part of this tutorial which is gonna be a lot bigger but it's social but it's also going to be a lot and that's going to be a lot bigger but also a lot more fun and you know that's a game this is game this is implication but we're gonna be using this with some slight modification and there's a lot of things that are going to change in the second part even this one so the way we actually look at our message the way we register them the way everything is clamped up in client that's CS I don't like that we have a different way of working around that in the second part of the application we use the Evan to base message system so if you receive like Z the follow-up that if somebody you follow have an update to it we can register to that message with any system in our game and actually every sync that message and receive the information inside of it so it's a lot different and client NCS will not get calmer some server Doc's yes would not get cumbersome instead what you'll have is you'll just have a budget system that registers to one specific message and they receive an update on it so there is that there's also the fact that we're gonna be dropping the binary format there because it gives some problem with WebGL and I'd rather be able to write my own by it's like my own message byte by byte so we know exactly in which order we read things and also in which order we write them which is much more efficient and we're dropping all the overhead that comes with binary format as well having that said I hope you guys enjoy I hope you learn something with this whole series that we've made multiplayer content is quite scarce up there especially for unity for some reason maybe I don't think it's the best I'm saying that now once we're done I don't think it's the best way to go about creating a large-scale multiplayer game yet I think it's gonna get a lot better in the future of course but is it need the best way to learn and to host your small smaller scale game yeah that's it once more if you enjoyed this if you enjoy all the information I provide to you during this tutorial please leave a like you should subscribe for the second ball or at least information on the second part coming out soon and also those how staying video what else yeah join this chord and also if you have any problem during the Serie I hope that you join and I hope that you ask I feel like a lot of people are gonna have problem with WebGL but yeah that's something else as for myself I am quite happy I'm done recording this for second time and I shall now go and work on the second part of the tutorial and also those hosting video when I have the time and when I feel like it yeah thank you so much I'll see you on this cord I need a comment section down below Cheers [Music]
Info
Channel: N3K EN
Views: 8,322
Rating: undefined out of 5
Keywords: unity 2017, unity tutorial, unity, unity 5, unity 3d, 3d unity, unity 5 tutorial, unity mobile game tutorial, unity android, N3K, N3K EN, N3K tutorial, friends, updating your friends, unity 2019, unity 2018, unity 2019 ui, unity network transport, unity network 2019, unity network transport layer, network transport, unity transport layer, unity layer, network transport layer 2019, unet, llapi, llapi tutorial, llapi example, hlapi
Id: GIY6hEFriWE
Channel Id: undefined
Length: 33min 39sec (2019 seconds)
Published: Mon Nov 12 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.