Title: Dialogue System and Interactions | Making a Simple RPG – Unity 5 Tutorial (Part 3)
Finishing up our interactions and create a simple dialogue system!
In this series we cover we’ll be creating an ARPG-style movement system, a world full of interactable objects, NPCs with dialogue, enemies and attacking, an inventory system, portals, and maybe even a questing system.
Low Poly Asset Pack:
hey guys welcome back we’re going to set up the dialogue system in this episode but before we do that I want to make sure that an interaction does not occur until they get to the end of the path right so i click on the signpost from here my guy walks over to the signpost and then when he gets there he should interact with it but actually what’s happening is interacting with it before it gets there because all we’re doing is calling interact method on interaction which at this point means right when i click notice interacting with items so what I want to do is I want to set up the check to see when we get to the end of the path and when we do then start the interaction so to do that I’m going to handle this and our interactable and i’m going to set up gonna be with an update we’re checking every frame there’s not really any built-in way to like a callback of any kind that you can call whenever you get to the path where you get your destination in the navmesh that comes with unity so we’re gonna set up our own little check here and the first thing I want to check is I want to check and see if we have a player agent right make sure that snot-nosed only player agent is not equal to know and the next thing i want to check is want to make sure that there’s a path that is found and they were not still looking for a path so i’m going to do and and player agent dot path pending right want to make sure that’s not happening make sure path is not pending and once i have that i want to check the distance between my player and the destination as well as compare that to how far away the stopping distances right so if i am within two units in my stopping distances three units then yeah I’m there I can stop it’s fine we can obviously say you know hey I want to see if the player is at the destination exactly because it’s not going to happen if working with floating point numbers we’re going to have large differences when it comes to that already find differences but going to be very far off when i’m at the floating point so I want to first check to see what the player agent and get remaining distance so remaining distance that’s the distance between the player agent and the destination and I want to see see if it’s less than the player agent dot stopping distance and we’re checking it like that because we are changing the stopping distance based on what we’re doing so for moving to interaction and we’re just using the base interaction method the base moved interaction method it’s going to be three but in our world interaction we set it to 0 if we’re just moving the player to someone on the ground so with that inside of this that’s what I want to call interact which will call this method or it’s over and we call the method that overrides it from a derived class so get rid of that and that should give us a starting point go back into unity just going to start the game here and I want to click on the signpost and when I get there okay so I get there and interacts doesn’t interact until I get there which is what i want but interacts on every single frame because it doesn’t know how to do it once right so what I want to do is gonna set up a flag i’m just going to create a private which I know was assumed but I want to make sure to find its private private boolean and I want it to be has interacted interacted yes and on move interaction whatever the interaction is initiated I want to make sure i set it to false write a new interaction our player definitely has not done it before you even started doing anything so has interacted is going to be false and then once we interact I want to set it to be as he has interacted is true right but also want to check to make sure that it is not true because if it is true that means we’ve already interacted with this interaction we’ve already we’ve already done this interaction so don’t do it again until this flag is reset which means a new an interaction has occurred wanna say not has interacted spacing not has interacted as well as an and player agent is not know and we do have a path and that should give us pretty much we’re looking for click on the post I get there interact one time and that’s that cool that is what we want so that work for now we might have to tweak a few things later on but for now that’s what we want and let’s go ahead and set up the dialogue system we’re going to start off with the dialogue menu in game here so on my canvas i’m going to create going to create a panel and i’m going to call this panel dialogue just like that and going to grab my Rex tool to scale it down I’m just gonna guess what i wanted to be for now but like that right there and we will leave the default colors and everything for now and inside of this i’m going to create we’re gonna have a button for the continued so i’ll be able to click on the button and it will show the next line of text or whatever it’s called us continue and inside of that one access the text and i was going to say it says continue just like that grab button and we’re going to bring it down just there for now and I’m going to align it to the bottom in the center that’s fine i’ll make sure this doesn’t stretch don’t miss their names going to under the bottom the center as well and that should be fine now within dialogue going to create a text field here and this is where the dialogue will be and we’re going to scale this out just like this looks good go and center it and I’m going to Center horizontally and vertically and I want to make it bold and fine-looking normal i want to upgrade our increase the font size a bit actually still best fit check best fit and then sighs want to be like 14 maxsize for us to march make it 20 26 or something and the color honestly I don’t care about this right now we’ll just make it something like that and that seems fine and dialogue want to keep the name of tax with an object because it is just the text inside of the dialog box and then also to make another we’re going to the panel so i’m going to i go down to panel and this is going to be the name of the npc that’s talking to us so we have to add that property onto our npc as well so we can update that / npc i’m gonna call this name yeah name is right it’s going to scale this i’m just guessing it all this right now and just moving a good just like that looks fine so that we can actually see it and inside of that want to create another text element and it’s gonna be called 2x that’s fine name text and this is going to say name for now and obviously we will probably come back into this and make it look like something because it’s that’s ugly but for the purpose of this it’ll be fine and there we go and that’s the UI for the dialogue system now want to create an empty object this is going to be the game object that handles the dialogue someone called dialogue system and this is going to be a singleton object which means i only want to create one if one doesn’t exist if one already exists I don’t want to make one and we’re going to have a static reference to this single object so that we can reference it from other objects without having to have a direct reference some type of reference to the object specifically because we can be sure that the one instance that we find of this through the static reference is the only instance that exists because we set up the single tube and to work for that so i will show you that in just a second but i want to create a c-sharp script dialogue system manager or something like that but assistance fine for this going to drag that and drop it onto the dock hello on the dialogue system and i’m going to open this in visual studio and the first thing i want to do is on a sit at the singleton so that it works with the static reference so we’re going to set up a property i’m going to type in prom and I am in visual studio and I want to tab over and it’s going to set my property for me it’s an automatic with the getter and setter so that I don’t have to define a gator or a setter is going to create the private property for us and all we have to do is fill in this information using a public static and now this is where it gets cool is going to be a dialogue system which is what this class is right that’s when allow us to create static instance of this object and I want to call it instance just kinda makes sense so we’ll keep it at instance now and start actually will use a weight for this you want to be like a first thing right and what we have to do is we have to check to see first of all it does instance exist is it know if it does exist is it equal to this object is equal to dialogue system that we have created here if it is not then we need to make sure that when you reset it to be that way I can handle as I say if and then i’ll say instance does not equal to know right and instance is equal not equal to this if those conditions are met that means that an instance exists that is not this instance so i’ll just go ahead and destroy what use are relying on intelligence to aid you in your typing not necessarily tell you what you can type but a doing your typing right type des I expect it to feel and destroy for me so I continued on to the next thing and it more often hunted scenes with the solution being a project i get these issues where it just stops working right so if I were to save that and restart visual studio everything’s fine like it’s so frustrating especially making a video I don’t continue for now before we start and I will do else so if those conditions are not met that means an instance does not exist it is said to be equal to this which is a reference to this instance of the object the one that we have created by dragging and dropping the component on a game object in our hierarchy right so that’s exactly what you want so now it exists and we can reference it with instance pretty cool so now let’s think about how we’re going to set up the dialogue system first of all I want a way to add new dialogue to the dialogue system and then I want a way to continue the dialogue say we have a conversation open right i want to wait to be able to click the continue button and it will take me to the next bit of dialogue from that npc and once we are done with the dialogue once we have friends others to read i want to close the dialog window and that way i can continue on with whatever I was doing the first things first let’s do the add new dialogue and I don’t call it a DUI to the public we can access it from my pcs avoid want to turn anything is going to add new dialogue and this is going to take and what will take a an array of strings and i’ll call it just lines is fine and this is going to we’re going to assign the value of lines to make it a list in dialogue system so if I were to compare to find a public list first of all we need to on use the generic name space on your system dot collections and I want to access the generic types which is what a list of genetic list everybody pass it a type of whatever we assign without the lines to be which in this case is string will do list string i want to call it dialogue lines is equal to a new list string just like that so now we have the list that we can populate with the lines that we passed over the menu dialogue but what if we only have lines in this from the previous dialogue well let’s see either add or set is what I’ll just I’ll take dialogue lines and i’ll just make it into an empty list by just really creating and using the constructor just like that so it’s now an empty list with no elements in it whatsoever and that gives us the opportunity to add the array of items and the wind want to handle this is I’m going to for each like that and i’m going to say string and we’re going to call it line in lines right so lines is the array that were passing a new dialogue as you can see here and line is what i’m calling the element that were grabbing from that the element in the string and it is a string time basics sharp stuff I’m not supposed to explain this anyway this is not for very beginners so i’m going to stop doing that probably not and I’m going to then add it to the dialogue lines list of grabbed outlines to add i’m going to say line just like that pretty cool and just make sure it’s working once you going to elongate out to logout v amount of lines that we have a dialogue lines so count for a list and that should do that for us and now we have to do is to make this work right to be able to send over this text i wanna go to npc first of all and we don’t need to do a pc is need to create a way to add dialogue to the NBC or in this case monologues pretty much just the npc talking to us so what I want to do is we’re going to use just an array of strings of text and we can add the strings of text in the interface but it’s not necessarily the best way to do it is just in line with keeping this very simple what i would do personally I would create individual files for dialogue in this case in pc file so it would be adjacent file and out structure it / npc and I would have branches of dialogue in the Jason file that i can skip through interact with based on what a PCM with what a pc i’m talking to what part of the tree MN and so on but in this case keeping it simple make it public array of strings and i’ll call it dialogue and it’s all need because we’re going to be assigning it in the inspector and on interaction i need to call the dialogue system and keep forgetting intelligence is not working at the moment so let’s go ahead and fix that real quick i’m going to want that save and restart our and we’re back and it is working now so with the dialogue system which in this case is going to allow us to access the instance just what we want so i’m going to go through instance and inside of instance I want to access it was add new dialogue and notice how that worked right we don’t have a static class will have a static object but we do have a static reference to the object that component attached is attached to and that allows us to access it through the instance now I couldn’t access it through just the class here that wouldn’t work we have to go through the object of a creative it refers to the instance of that class pretty cool so now all this wants is a array of strings want to pass it dialogue and that’s what I want to do I want to create a name though so it’s create and we’re not gonna work with this just yet i want to go to create the name is quite a name and we can pass this through once we set up the system of it more but I want to now go into the hierarchy and grab the npc and look at it and what I’ll have is first of all need to hide this i’m going to hide this one’s yelling screaming I’m going to hide it was an interactive here yeah we’re going to hide this in the inspectors I want to tag it with hide in inspector and that way it’s not going to render the inspector for us because we don’t need that inspector it is handled in the script privately but i’m using it as a public member because it has to be passed around from object the object so just like that i want to grab dialogue and I’m going to say this bit of dialogue have three elements and i’m going to say there’s AK they have spoken for adventures know and adventures and historic adventures known not come back and you’ll probably not probably not listen to mom listen isn’t door my warning anyhow great conversation and the name this guy is misters ankles you can’t so creative and I that would then take us back to dialogue system and what I want to do now is well first of all let’s just grab that name right grab string name NPC named and I want to set up the name let’s create we don’t have anything let’s just go ahead and create see me and also agency named of equal to openness to start npc name is equal to a pc name ok then you work for us just fine this being this instance dialogue system bc name being the perimeter we passed in i’m going to explain the same thing over and over I feel because I think it would explain that so with that setup now what I want to do is I want to grab all of the UI elements from from rui we just created we have to continue the text the name all that stuff so I want to set up and grab a reference going to make a public and it’s going to be just a game object and wants a dialogue panel and we’re going to use this to reference the dialogue panel which we can then use to grab the children of the dialogue panel which will give us continue txt and name so I’m going to go to dialogue system for struggle at the top in drag it to the top and on dialogue system we have dialogue lines which we don’t actually want you can hide and don’t worry about it and public we have an error which is why we’re not of course we do because in npc was passing dialogue need also passed name so now that should allow the paint to compile and give us our new field in the inspector yet so again we’ll probably hide these inspector but not going to worry about right now when I drag dialogue on to the dialogue panel and that gives our dialogue manager access to the dialogue you I panel cool stuff now I want to do is I want to go into awake and you could do this individual if you wanted to set up each link to each UI element in the inspector you can do that but I’m going to handle it all in code so first of all what i need is I need access to the UI namespace because it going to be using the button and the text and the panels and whatnot so wanna go through unity engine and grab the user interface namespace and now i’m going to set up a couple of members for us the first variable i want is going to be a button which again if you don’t have using unity engine wi namespace to call it continue button and then I want to set up text and i’m going to have dialogue text and name text become allows us to define multiple variables of the same time just keeping it on one line nice and then the other thing that I would need all right we’re gonna need to know what index we are on in the dialogue lines list right so if we’re on the third line or the fifth line whatever we have to know where we’re at so that we can render the correct text to the UI so what I want to do is I want to set up just an integer and we’ll call it dialogue index alright sounds good sounds good and then we’re gonna use all this in the awake to grab references to UI elements so continue button needs to grab the buttons on it set that equal to the dialogue panel which is the game object we have the reference to in the inspector go through the dialogue panel and note that the dialogue panel has the children of the continue button the text and the name panel so I want to do is only go through the gate component method of my game object and I’m looking for it’s a button what am i doing and then you get a child first right so i wanna go to the transform component of the dialogue panel object and that allows me to use get child and find child get child is looking for the child by the index by the ID and the fine child is looking for by the string name so going to use that because we don’t necessarily know what where they’re going to be in we might move them around but we know the name is going to be what we said to be so the continue button name is continued and as we have that game object now we also just have to grab that button component because we’re looking for a button type something to get component it is a type of button just like that and now we have to continue but cool so what I can do then is take the continue button we’re going to add a listener to the on click event so in the new unity UI stuff and I would not use this a lot but we have a bit in the inventory system there’s a whole new event system right and this allows me to add a community and listeners inside of the inspector for the on click right for the button go to be on click panel there and i can add what happens when it happens or i can do it do it in code what I like to do it’s my favorite thing to do i can add event listeners to the on click event by saying continue button dot on click that is the agency it is the event for the button click event first of the button is pressed cool so on click then i can say add a listener and now this is going to be where we type in what happens when on-click happens but it has to be a delegate and that means we’re going to define it just like this and I don’t know actually if we’ve talked about events and delegates just yet if not a different video on those because they’re quite important so as a delegate is going to be using event when we click on it let’s go and set one up negative public void continued dialogue just like that right so now on click I want to continue dialogue and you would just put in the method just like you would call it from any where else in your code and don’t forget the semicolon to in the statement there but that’s what we want right there so now the button is wired up i also want to grab the text so it’s going to be a dialogue text is the dialogue panel once again the transform once again will define the child by the name of texts which actually can probably just get the component directly its fine its component you can do i could get child component or whatever it’s called but we’ll just handle this way to go to tell me handle the last one text and there we have it and then also want dialogue or something about that name text is equal to dialogue panel that transform ok so this one though we’re looking for the text within the child panel right so name is a panel inside of that we have the text so I grabbed the checks that inside of the name panel and to do that i will first of all to the same thing fine child and it is called name but then I want to get the child of fun of the child that we found by name so i can do get child and we know it’s the one and only child in that in that object right so i can just pass it the idea of 0 which is going to be the first child in that object but it’s the only child so it’s a safe bet that it will be 0 at all times so we could do that and then i’ll need to do is get the component of text ok so i said you could do this only inspector if you wanted to but I didn’t want to so I did not and that’s that and now also know that on awake there’s no reason for the dialogue need to show because it’s not going to be dialogue unless we want to activate dialogue and you’ll see want to start a new dialogue so I want to make sure that at at star dialogue panel is just turned off and this dog barking great someone’s do setactive to false going to set the active state of dialogue panel the game object to false if you want her back on you just do the true rights on off so said active false and that’s pretty good pretty good and I know that i am going to want to set the dialogue index to be zero whenever we add new dialogue i assume that would be the case at least we’ll see when we get there and now we need to do is handle the sending of all this information that we just got here from our NBC to the UI right we have references to the UN a so it should be no problem what I’ll do is I’ll just set up another method here is going to be just make it create dialogue is going to handle enabling the dialogue panel as well as assigning the text values to the elements inside of that panel so the first thing to do is on to grab the dialogue the dialogue text and grab the text property of that which is just the string of texts they uses to render text on the screen right so the text is just going to be a string and the string that I want is going to be dialogue lines and then just the first index in within dialogue lines right because we’re creating a dialogue we know that we want to show the first bit of text within dialogue but just in case you go back to this and if not reset the index for some reason unique to your game or whatever then we can make sure it just says dialogue index right here i log index there we go and then I want to also set the dialogue name name text click on the you should have called them and said the text property event to be equal to the NPC named and then all I have to do now is re-enable the dialogue panel so setactive true never go so is going to assign the appropriate line of text to the dialogue assign the appropriate name and then once it’s done it’s going to turn it on that way we can see what we assigned and that should be fine now within continued dialogue all this is going to handle is pretty much increasing the dialogue index and then showing the new dialog based on that and so what I want to do first of all is i want to check to see is this index of dialogue is it first of all in the array right are we out of range already are we at the last one if right the last one then we don’t need to show anything else we just need to close it was continues click so let’s check and see if I grab the same dialogue index I want to see if it is what we can handle it we can just do like a less than dialogue lines dot count and ok so it’s going to count one two three four right if there’s one element in the list it will be one but we know if there’s one element in the list and it’s at the first space in the list the first spot in the list is going to be index zero as zero is the base index and c-sharp so what I want to do is also subtract one that way we know that we’re comparing the index to the actual index of the amount of items that we have so then we’re going to handle this is just like this it should be fine and then I just increase the just increase the dialogue index by one so let’s do the double plus sign was going to add one to the current value of that property and then sent the dialogue text to be the new dialogue index line just like that cool but what if it is the what if it’s time to close the panel what we’ve seen all the taxes to see I’m going to announce our checking for that just like that went to an else and say if it is the last one then just grab the dialogue panel and set it to false again need to handle and resetting all the texts and stuff because we’re going to handle that each time a new ones created and that’s a lot that we’ve just done without testing so let’s see what happens when we could play ok and first of all the paneling away that’s good what it’s supposed to do and if I interact with this npc interactive MPC it doesn’t come on did I not tell to come on i’m not calling this at all going to create dialogue after we have added a new dialogue going to create dialogue ok everything’s still fine looking NBC and there we go interact with an NBC there’s a cave up there many have spoken of the whores adventures going into ditches and not come back you probably listened to my warning inhale any continuing goes away well actually works school and go to the cave against his warning right that’s kind of the whole anyway that’s cool i didn’t change the name Adam look yet mr. sprinkles alright whatever have to NBC’s oh it’s tested with to NPCs want to duplicate that NBC and MSNBC we’re going to say howdy I have stomach think that’s all I’m gonna say was going to make it two elements is we called coffee ok let’s move in going to move him right back here right there and i will click on the CC yep yep yep and we’re going to see howdy coffee have a stomachache or guy and this item designing things cool so that is a that is the dialogue system right we’re still gonna work on it you can mess around with how it looks how it functions all you want but for me that’s going to be that’s going to be it for the dialogue functionality at least in this series will cover more advanced system in its own little one-off series and be kind of one-off episode actually the sign with the dialogue up to and we can do that and the same way we handle the MPC right just to show you real quick and notice that was it but they’d be pretty cool to have this do something too and so what I want to do is on interaction snug rid of the log then on interaction with the dialogue manager and just showing you how the how the singleton pattern is really helping the modular earnest of this whole system something through dialogue system going to grab the instance going to add new dialogue and we don’t have to make the string for now in the inspectors contest and say hey son and even i would say the name is time I can’t do that so we’ll have to create an overload for add new dialog you know just for simplicity we could make an overload or it might be a better idea actually if our sign does have an array of things that can say right you can still just say one thing but still just keep the array at one and one length but um having the ability for it to be multiple lines attacks that’s not a bad thing right so just pass it off like that and i will save us some work too not a bad idea I want to grab the signpost and you would have this scientist of hip-hop in this better than I have at least because now I have to go in through all these environment objects here find the thing that has the signpost attached to it which for some reason is that and go into the dialogue and set the set dialogue but you wouldn’t do it that way you would have these as a prefab the whole thing is a prefab you would have it probably on this ryan you would have it where you can just select that and go in and change the dialogue on that you’d probably have everything organized a bit better so you can get to it a bit easier but not me not me so if I played us now and click on the signpost to get the signposts is this is a cave from the sign for continued everything works fine everything works in to expect and that is going to be the end of this video in the next episode we’re going to set up the character stats as well as start on the enemy combat that’s gonna be fun hope you guys stick around for that and after that we have inventory and then who knows what else that’s as far as I’ve got so far stick around for that guys if you have any questions leave a comments below this video and i’ll be happy to get back to you i am more than ecstatic to be back doing this stuff for you and for me really really enjoy doing it so don’t be afraid to ask many questions i am here now and I’ve been away for a while but I’m here now and I really want to talk to you help you out and just let me know what you think alright my name is austin and i will see you next time