Part 5 - Adding Dialogs and Choices - Make a Branching Dialog System

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay welcome back so we're gonna be doing today is we're going to make it so that we can go up to our little friend oh here click a button and we have dialogue and then also choices and these choices will branch so we have two options we get to don't be rude and either way don't be rude gets a different response but they still take you to the same output we can ask about say the cave go back and now we can ask about something else so about the town we can ask about the lake and now they're asking us to go on a little quest for them you can say yes or no and it kind of branches from there about the monsters and then we can leave the dialogue there we go that's what we're gonna be covering today so stick around mmm let's jump right in okay welcome back everybody it's been a minute again I know I hope everybody has been well and safe and healthy wherever you are it's been a very strange time I 100 percent thought I would have more time with everything that's going on right now here's the compressor I use the virtual compressor I use but I ended up having a little bit less time but we're in the last week of school no facecam today because I left it at school because I've been teaching my lessons from school virtually but yeah so let's dive in here now let's talk about where we left off last time so currently the way I have this set up I can walk over to MPC that I created and if I enter the NPC's interaction area and press my check button it's going to activate the canvas that and setting it up so that it actually understands what the dialogue should be those are the two things that we kind of went through here so I'm gonna do next is we're gonna make this actually work to create new dialogues from our ink script and add them onto there so let's dive right into that and get started here alright so the first thing I want to do is take a look at our prefabs here inside of our prefabs in the UI stuff I have two specific prefabs I want to look at we have our dialogue text prefab and then we have our response prefab both of these are going to need to have a script on them to interface with the text so that the script can tell the text what text it needs to have so today that I'm gonna go into my scripts folder down here and I'm going to create a few new scripts under my UI folder the first one is gonna be this dialogue object and then we'll create another one for the button so we'll create a nice t-shirt script and we'll call this one is it gonna nope now I just need to delete this apparently awesome Unity's been doing this a lot on me lately not quite sure why okay so now let's create a new C sharp script but an object they're gonna do it to me again good lord how many how many times am I gonna need to try this out I don't know what process is I have running in the background but are making a unity freak out like this there we go response object and then let's delete this new behavior script right there and let's open both of these up in visual studio now for the first time in a long time my visual studio is actually already opened so that's good ok so we've got our response and dialogue object so a few things we need to do here we'll start with the dialogue object we want to have a method that sets this up so that it can then set what the text is going to be based on what the branching dialogue controller sends it so I'm gonna create a reference to text mush pro using TM Pro if you're using the regular unity text this needs to be unity engine dot UI and then I'm gonna create a new method here I'm gonna call this set up and this is gonna need to take in a few different things first it's gonna need to take in an actual string so I'm gonna call this string new dialogue and that's all this is gonna need to take in which is good I need to have a reference to the text mesh Pro object so I'm going to do that here we'll make we realize fields will call this private smash bro you GUI and we'll call this my text and then in the setup method here we'll set our my text on text equal to the new dialogue all right cool now for the button it's gonna be something similar so similar in fact I'm gonna just grab this setup function this is gonna require another argument though and that's because when we make a choice we want our button to be able to take that choice with it just copying and pasting these parts that have to do with text mesh so I'm feeling a little lazy here today all right there we go now oh yeah these voids need to be public as well we can access them when we're creating the objects all right now for the response object we need a string for the dialogue but we also need an integer for let's call this my choice and then all right so it's also make be variable for that choice Brian integer value and we'll call this choice value just default that time oops leave it as it is and we'll also set choice value equal to my choice all right cool now there's some stuff that we need to do in the actual unity editor here to make these work the way we want them to so currently I'm in the response button prefab so in here I could grab my response button she's gonna get the response object script just gonna need to know what the text is so grab the text mesh and I'm gonna do something similar with the dialogue text so the dialogue text I'm gonna go to my scripts that I just created I've got my response object I'm gonna pull that on and this needs to know the text all right cool now in our dialogue our branching dialogue controller we have a enable canvas method that just sets that canvas to be active what we want to do also is we want to create a new ink story object that we're gonna be controlling through this through this script and that ink story object is going to deal with all of the heavy lifting of the branching dialogue so what I want to do first is create a public void we'll call this set story and this is going to require that we have the ink run time using inc dot runtime all right cool so now when we set our story what we want to do is we want to create a serialized field here for that all this story my story story is an ink class you can see as I hover over this that it's an ink runtime story in that story I want to check to make sure that there is a dialog value and if there is a dialog value then I want to create a story out of that dialog value so let's do that here so in my set story I want to check to see if dialogue value dot value so if it exists then I want to set the story so my story equals a new ink story that we're going to create from dialogue value dot value so we're creating that oh and we have to specifically say it's a text all right cool now I'm going to add an else condition here and we'll just do a debug statement I went with the story set up all nice and descriptive debug statement I have a tendency to struggle with making nice descriptive debug statements all right cool and we want to set the story after we enable the canvas now the next thing I want to do is create a new function to refresh the view so I'm going to call this public void rush of you right and then from refresh review what I want to do is I want to break down exactly what's happening with the dialogue and do a few separate steps we want to add dialogue objects as required and we want to add buttons as required we add the buttons we want to delete any buttons that were there but I'm gonna leave all the dialogue that was there because I don't want the dialogue to be deleted all right yeah I want to have like a running tally of everything that's been said in the conversation which is pretty common in Western RPGs so what I want to do here is you know make a new method specifically to make a dialogue I'll call this make new dialogue and this is going to need to know a string that is using to make the new dialogue then I want to instantiate that dialogue and the parent I'm going to instantiate it at he's going to meet my dialogue holder which I'm pretty sure I set up I haven't done that yet so I need to tell it where exactly where I'm gonna be creating that and in unity if I take a look at my dialogue campus here when I'm talking about creating that what I want to do is not just have the panel but inside the panel I created a dialogue scroll and inside the scroll it's the content that needs to be the parent it's this object right here so to visual studio let's make a realize field and this just needs to be a game object I don't have to take it as a scroll container I'm gonna call it the dialogue holder I'm gonna make another one for the choices while I'm here while this choice holder alright so down in my making your dialog I need to instantiate a new dialog but because I want to be able to change using that setup method I need to instantiate it as a dialog object so to do that I'm going to say but I call the dialog top check yeah dialog object and I'm just gonna call this one I don't know new your dialogue is equal to CN she ate a torsion of my dialogue prefab yep what happens when you come back to a project after weeks of helping kids do their algebra okay so it's I like prefab and then I want to give it a parent Aaron is going to be the dialogue holder oh there needs to be dialog holder done transformed because it's not the object it's the components object so that I'm getting the dialogue object from that new object oh I'm already using no dialogue good lord we call this new dialog to object wondering what's going on with that let's bring this down so that I can read it a little bit better yeah that's good now what I want to do is take my new dialogue object setup and I want to pass in my new dialogue to the setup well that's going to be part of refreshing the view is too making a dialogue but I can't just call making a dialogue I need to check to see if the story can continue so I'm gonna say wow story meaning that story that I just created so my story dot can continue it's just a variable of it whether or not they're good choices left to make then I want to make new dialogue and the dialogue I'm going to make is story dog continue so my story continued just creates a string which is the continuation of the story for one line of content it's possible if you're not sure there's more content available you want to check whether you're at a choice or at the end of the story you should call it can continue which is why I did that before calling this function all right so this is going to be refresh with you when you enable the canvas we want to set the story can you want to brush the view alright now if I go back into unity we should be able to see the dialogues start to be created there's a few things we're gonna need to set though before I go any further my canvas here I need to tell it what the dialog holder is choose the dialogue content and the response holder which is the response content all right cool let's have this dialog canvas the whole team I think I had the whole campus enacted by default so let's try this out here I'm gonna go over and this is gonna be the real test here I promise I won't cut if I make a mistake all right so we got our new text but for some reason object is not set to a reference of an object so where is that at make new dialogue mentioned dialog controller lines 59 so which part in on sent to an instance of an object created the dialogue prefab since with 959 she's coming from 51 which is coming from 32 so the issue is it's not finding the dialogue object on my prefab so if I go back to my prefab here dialogue prefab it's responsive object oh that's why all this a dialog object it needs to be a no no dialogue object but I must have put the wrong script I did my dialogue prefab I put the response object so let's remove this component put the right script on here II told you I wasn't gonna cut if I made a mistake hey hey get on there but now it's dialogue object and give it its text and let's go back to the script and make sure it's correct yeah dialogue object all right let's say that let's go back to our game here and let's try this again it doesn't work this time I might cut but I like you know what I do to fix it afterwards so over here sweet what do you want we don't have any of our choices in here and if I walk away like nothing happens but we're gonna get to that now I want to be able to add the choices so I'm going to make a new function to create choices so we're gonna do void make new choices and make new choices is going to have to do a few things first it's going to need to go through and disable or destroy rather all the buttons so we're gonna go for I equal to zero just scroll down so you guys can see that I is less than like question holder where did I call it yeah choice holder good lord choice holder that transformed dot get child count so we want to go through all the children increasing each time um what's wrong with me doing that and in Matthew oh ok child count is a function that's why oh and now it's still wrong just run Becky child careful please obsolete you use transformed a child count okay and that's not a function that's why okay that makes more sense now what I want to do is destroy so destroy Turandot he's get yak a child I and let's destroy the game object part this isn't necessary technically but I like doing it anyway so we're gonna destroy all the questions and then we want to make new ones so we're going to do four and I equals zero is less than story dot so the number of current choices is current choices in the current choices is the actual list of all the strings that the choices have so I wanted to dot count plus plus so I'm going over all the current choices I created an error here there we go fix that so what I want to do then is pretty much the same thing I did with make dialog only I want to do it with make a new response so let's do void make new response and this is gonna require a string we'll just call a new dialog again an integer which I'm gonna call choice value cool now in making response I'm gonna call that first so what I want to do is you response and I want to give it the text from the story's current choices so we'll leave it my story dot current choices and like I said this is a list so I want to grab the I choice which is where we're currently at so zero one two three four five however many texts and that'll give it to it as a text for the string and then for the integer choice value I'm just gonna give it a high and that choice values going to get to be important in just a minute we're gonna do making you choices as part of refreshing the view as well but I only want to make new choices if there are choices to make so while we're continuing we're making that dialogue and then we're gonna say if my story dot current choices doctor count is greater than zero meaning if there are choices then I want to make new vers are not making you response or die call it making you choices all right and then if all of this is done if we don't have any choices to make then I want to set the canvas to be inactive so it's gonna grab my branch in canvas just copy that and then set this to be false cool all right now back down here let's actually make this response work and then we can test this out so just like before I need to instantiate it as not a dialog object this time but a response object so this is going to be a Spahn subject we can call it new response object and instead of instantiating the dialogue profound I'm going to instantiate the choice prefab and instead of in a dialogue holder it's gonna be at the choice holder and instead of get component dialogue object it's gonna be get component response object and then calling it here this needs to be a new response object and I need to also send choice value and I'll explain with that choice all he does here in just a moment alright so that looks okay so far let's jump back into unity here let's hit play and let's test this out all right so I don't know why I didn't just put the MPC right next to the player or just put my player right next to the MPC I don't know why I didn't do that what do you want hello don't be rude now I can click these all I want they're not gonna do anything but I want to show you exactly what's happening here with that choice value so if we take a look at our response content we've got our response buttons let's make this so that we can look at the debug so that we can see all the hidden values so currently this has a choice value of 0 and this one has a choice value of 1 the reason those are important is since these are buttons I want to make it so that when I click on it it goes back to the dialogue canvas and tell us the dialogue canvas to choose a choice index which is how Inc has it set up which is kind of an awkward wording of it and then the choice index is going to be whatever this choice value is that I just passed to them so it was arrow 1 2 3 4 5 whatever okay so in order to do that I need to do something I haven't really done with this before and that is setting up a delicate so let's do that okay so heading back into our script here and when we were creating the button I also want to get some button information so let's grab that in order to get button stuff from unity I need to be using unities UI so in my branching dialogue controller I'm gonna add another using statement we're gonna do using unity engine UI and then to do or what I'm gonna do with that is when we make a new response I have a response object but I also want to grab the button from that so we're gonna do Bonnie I'm just gonna call this response button equals the response object game object dot get component button now it's not great to do a rigid connection like this so I want to just double check really quickly here that I'm going to always have that button at the same level in my prefab as my response object script so the button script is right here the response objects script is right here so that should be good but just in case I'm gonna add a check so I'm gonna say if response button so if there is a response button script or just a button script then I wanted to response button dot listener and did I spell it right I did Oh because I have to do on click so response button dot on click it's on and listener so what this is doing is encode in button this is the on click and then these are the listeners this is encode adding one dynamically because I don't know ahead of time if this is going to be button zero button one button to button three so I'm dynamically adding that on click listener so I want to listen for a delegate and now I need to tell it what function to use now to do that I'm going to make a new little function here we're gonna have to deal with that red for just a moment actually I'll just add this here and I should yeah that fixes the red all right so we're gonna make a new function that's going to specifically allow the button to make a choice so this is going to need to be public because I'm gonna need to actually it doesn't need to be public but I need to be able to reference it from okay I'm gonna check without it being public so we'll do void choose choice see I made fun of the ink a second ago I know I'm doing the same thing and I want to give it specifically sorry for a second so give it [Music] your choice I'm gonna check this here and I might end up cutting this out you guys might never see this what I want to do is my story dot choose choice index choice and then after I do that I want to refresh the view which is why we've been setting everything in kind of its own little thingy thing so when we refresh the view now this choose choice is gray because it's not being used by anything so in my button here in my delegate that I'm creating I need to call that so choose choice and now I need to give it an integer value and that integer value is the choice value that we've already decided and because c-sharp is a little confusing sometimes I need to have a semicolon in there so this is creating a delegate and what it's doing is it's essentially adding choose choice choice value whatever this integer choice value is to that listener which is in the on click part of the button alright so I'm gonna save this and I think I might need to change this like I said I might edit this part out depending on if I need to change that or not but let's let's give it a try and again I don't know why I put my well I know why I put my MVC over there because this is the town this for NPCs live alright so let's do don't be rude okay so we still have what do you want and now I have a bunch of different choices here so let's ask about the monsters now this didn't immediately go down like I wanted to so that's an issue that we'll have to fix so what monsters I don't see any monsters and the answer Oh Katie are you doing let's go back so now the monsters choice is gone I can ask about the cave I know a few years ago Keens father tried to go fetch some treasure he never came back go back let's ask about the town what's going on in town right now now what you can add is like here could be player's name : and then you can put all this in quotes and then it could be NPCs name : and you can put all this in quotes and that's just a matter of changing the ink file itself back let's ask about the lake lost my favorite briefly 30 years ago would you go get for me yes and now the only option left is to leave continued no it's done so we're not setting the player to not be able to do anything but let's fix that issue of the scroll container not going down and the only way that I could figure out to do this so in your refresh you should be able to just tell your scroll container to go to the maximum value as low as it possibly can but if you do that it doesn't for some reason is the load right away and so well it won't go right to where it's supposed to I'll show you what it's going to look like so let's do so realize field I already have the unity engine UI here so let's do the private scroll rect scroll view and we'll call this dialog scroll and after we do all the refresh stuff let's set the dialog scroll time I think it's value No we're gonna have to look it up okay just a minute you okay it's vertical normalised position I want to set that to you or scrollview does not contain it's supposed to be the scroll rect that I need to do this on yep so I need to change this to be a scroll rect instead of a scroll view apologies well I uh I promise you I made notes on this and my notes actually say scroll rect but I thought I knew better all right so if we just set the normalized position to zero that should knock it all the way down to the bottom but something kind of weird with it happens button in UI element stop button what Oh Oh I don't want you i elements that's new experimental okay so let's give this a try here and you can see what the issue is with just setting the scroll position to be the bottom when we're done with this this is gonna be the longest video I've made in a long time apologies to you guys so what do we want let's do hello I just wanted to say hi now you'll notice that doesn't need to scroll yet just ask about the cave so it didn't scroll down but if I hit go back oh hey it's not doing that either see how it's not like this is 0 and this is 1 it should be down there after we set it but we have to put in like a co-routine with a really really small pause so I'm gonna make that co-routine right here some days I mean umarete or and call this roll Co and I'm not going to hard-code of value I'm just gonna have it wait one frame so we'll do you yield return know which weights one frame and then we'll set the dialogue scroll vertical position all this stuff copy that and paste that here get rid of that from there and then after we do all of them will call this Co routine so to start over teen scroll Co alright cool so now if I go back in here okay cool if I hit play we should see that scroll container go down to where it's supposed to be as we go through the dialogue all right so rude why is it not doing that I wonder good lord mr. Taft that's why this hasn't been working there's a reference area down there some of you were probably screaming at me about that okay so let's go to our dialogue and let's tell it what the scroll is dialogue scroll all right sure Epson chat if you were if you were screaming at me about the null reference error at the bottom of the screen that I just wasn't paying attention to okay so there we go now about the lake lost my favorite bracelet there no tie no go back about the monsters right there go back we've continued so I don't need to have that last choice for the continue I could just have it when I click leave I just have it on the dialog and then the dialog ends we're like here it's the end of the dialogue now it doesn't have them remember everything and you'll notice here that all my dialogue remained so before we actually one more thing one last thing we'll do when we're setting our story we need to go through and delete all the old tile logs so I'm gonna make a new void to lead old I was going to say it doesn't remember and that's something that we'll cover in the next video is saving these to another new JSON file so we'll go to Lidl dialogues and what I want to do is grab the just like I was grabbing the I already have the Dalek will be good so I'm gonna grab the dialogue holder so Ford into I equals 0 I is it's just like I did with the button holder I plus plus I want to just destroy it the transform doc get child I thank you mom I just want to destroy them and then when we do our set story before I even do this I'm just gonna delete old dialogues No yeah isn't giving a long video it's been a while since I've done a super long one some people like the long videos when people do not I try to make them short so that not only does it help me in the algorithm but it also helps with people who kind of want just something short and digestible did you oh cool there we go they don't remember any of the stuff that we said to them so they're kind of done that way which might be what you want for your NPC but we'll cover how to some kind of more complex things we think to save that out in the next video here so I hope you guys enjoyed I wanted to talk really really quickly about my patreon before everybody left same thing as always give me a like if you learned something new feel free to share the video with people you think might enjoy it subscribe it's about like hung well just go nuts on those buttons but about my patreon I've had it on hold for a few months because I wasn't creating content for a while there I didn't want to charge anybody for something I felt like they weren't getting there their money's worth on and then the pandemic it and I think I'm going to keep my patreon on hold until the world he's kind of in a I don't know a less precarious situation so you guys can still add me on patreon whatnot I'm just not gonna charge anybody on that website until things are kind of I don't know a little more steady I personally lucky enough to have a career that was pretty unaffected by this so it seems kind of gross to be looking for patreon money when I don't need it and I'm in a situation that's far luckier than I'm sure some of my viewers so those of you who have been contributing on patreon thank you very much he said I'll reinstate it when I feel like things are a bit more stable for the world so yeah I hope you guys have yourselves a wonderful day
Info
Channel: Mister Taft Creates
Views: 19,991
Rating: undefined out of 5
Keywords:
Id: 4nWaa8pcDOQ
Channel Id: undefined
Length: 42min 1sec (2521 seconds)
Published: Tue May 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.