Hello there, goobers. Today we're going to be creating a dialogue system using Game Maker Studio 2. We'll be using structs and methods, taking full advantage of the tools introduced in the 2.
3 update. Would you like to get started? Uh, well that's the only thing this video is about.
Let's try that again. Would you like to start? Great!
Let's dive right in. You can see that we don't really have much to start with. I only have a font for drawing our text and I have a sprite to act as our text box.
This sprite is a nine slice, so it's able to stretch across the screen, and it's going to act as the backdrop for our text box to render text onto. You can see we have absolutely no code to start with, and just an empty room that'll just contain a demo object later. So let's get started by creating a text box object.
I'm going to make a new object, and I'm going to call it obj_textbox. We're going to want to give it that sprite that we have already in our Sprites folder and we need to define some variables. So let's add a Create Event and start adding in the customizable variables that you can edit to change the way it looks, and also the private internal variables that it needs to run correctly.
We're going to put the customizable variables at the top of our Create Event so they're a little easier to find, and the first one we'll define is just an input variable. So this is going to be the button that we press to advance to the next page. Next we're going to add some variables that control the positions of things, so margin is going to control how much space the box has from the edges of the screen, and padding is going to control how much space things inside the text box have so like how far from the edge the text itself is going to be drawn inside the box.
width and height are going to be set based on first the the GUI's size, and then we're subtracting margin from each side, left and right, that's why we're multiplying by two; one for left, one for right. And the height is going to be controlled by the height of the sprite that we defined earlier. Next we're going to use these variables to position our text box so it's always in the right spot on the screen.
So you can see that x is set to the GUI's width, minus our own text box width divided by two, so that places the left corner of the text box such that the whole thing will be centered on the screen. And then the y position is going to be placed at the GUI height minus our own height, so we're raising the box such that it's touching the bottom of the screen. But then we're also raising a little bit more based on margin, so there's going to be a little bit of space beneath the text box as well.
Now let's declare some variables that control the text itself, so we're going to have a variable that controls what font we're going to use, what color the text is supposed to be, how quickly does it type itself out, where inside the text box the text is going to position itself, and also how wide the area it can type within is. So this is the maximum width the text can reach before it has to drop down to the next line. You can see that it's going to be almost as wide as the text box, but then once again we're just going to be subtracting the padding from each side, so that the text doesn't actually touch against the edge of our text box, it stops a little bit before then.
And that's all of the customizable variables that we're going to be defining, now let's add some internal variables for the underlying system to work. The first set of variables we're going to add is an actions array and a current_action variable. The actions array is going to store all of the different actions our text box needs to perform, so mainly this is going to be the pages of text that it's going to write out.
But there's other things too that a text box can do; for example later on, we want to maybe add some actions that set what portrait we're using, or who's speaking, or maybe we want to present a set of options to the player to pick from. We're not just storing strings or plain text in this array, we're going to be storing structs that contain data specific to what the kind of action is. And we're going to be adding that a little bit later on in this video.
current_action stores our position in the actions array so it's basically what page we're on. We'll initialize current_action as -1 just to signify that we haven't started the process yet. Next we need some internal text variables, so text is going to control what text is currently being shown on the screen.
Progress is going to control how much progress we've made typing the text out, so this will gradually increase and gradually fill that text like a typewriter action, until all the text is being displayed on the screen. And text_length stores how long our text is. And now we need to add some methods to our text box.
If you're not familiar with what a method is, it's just a function defined inside an object. Our text box right now needs three methods. We're going to start with the setTopic method.
This method is just going to start a conversation and tell our text box what the pages of text that we need to render are to have this conversation. All of our dialogue is going to be stored as topics inside a global struct that we'll create in a little bit. Every time a new conversation starts, we'll reset our current action variable and then we'll call the next() method.
And to put it simply, this method really just goes on to the next action. You can see that it increases current action by one, and if we are at the end of our array of actions, then we're just going to destroy the text box to close it. But if we're not at the end then we're going to grab the current action that we're on, and we're going to call the act method inside it.
So when we define our actions in a little bit, we're going to be giving each one of them an act method, which is going to be called here. And the very last method that we're going to add to our text box is the setText method and this really just sets our text. It sets the text variable to whatever text we need to render, it sets the text length variable to the length of that text, and it resets our text progress so that we start typing fresh.
And that is everything that goes in the create event for our text box, so now let's go to the step event and add the behavior for our text box. So I'm just going to create a Step Event, and I'm going to start here by grabbing our input again just setting a confirm variable using the confirm key that we defined in the Create Event. I like doing this at the beginning so that it's always available throughout the rest of my code later on, and this step event will be growing as these video episodes continue, so I want this to be as clear-cut as possible, so everything is easy to add to later on.
After that, let's run some codes that actually creates the typing effect, so we're going to be increasing text progress by our text speed, but we're going to be capping it so it never gets larger than text length. Now we need to run some code that handles what happens when we press the confirm key. And different things need to happen at different times, because if the text is still typing itself out, then pressing the confirm key should skip that typing process to the end.
But if we press the confirm key after everything has typed itself out, then it should advance to the next page of text. So we need to add an if statement that checks if we've actually finished typing our text or not. If we have finished typing our text out then pressing the confirm key should just make us go to the next page of text, so if we press confirm and we finish typing then we're going to go to the next page of text.
But if we haven't finished typing our text out, then pressing the confirm key should just fast forward that process and set text progress equal to text length. So we've skipped that increasing phase and just cut the end and made it so that everything is now written out on the page immediately. And that's everything that needs to go in our Step Event so we could move on now and add our Drw GUI Event to draw the text and the box, but before we do, we need to create a function that draws text in a more specific way than the default draw text functions.
So, the reason why we can't just use the built-in draw text functions directly is because they are not robust enough to do everything we need them to do. First of all, when we later on add things like text effects; changing the color of a word in the middle of our sentence; there's no way that the built-in draw text functions are going to be able to handle that on their own. And as well as that, they aren't going to handle line breaks correctly either.
To demonstrate what I mean, let's see what happens when we type normally with the draw text ext function. You can see as we type a large word like "sesquipedalian" for example, it started out typing on the first line, then dropped to the second line, which is no good. It looks kind of jarring.
So instead what we'd like to do is write a function where when we type a long word like that, it begins typing from the second line, from the start, instead of partially typing on the first line then dropping to the next one. So, in order to do this, we're going to make a custom function in our scripts folder we're just going to make a new script and I'm going to call this scr typing because it's going to be about typing. And I'm going to create a typing function, so "function type.
" We need to specify where it's supposed to type, what text is supposed to type, how much of the text it needs to type out, and what the maximum width of the area it types in is. So this part is probably going to be the most complicated code that we write in this tutorial, but if we just do it in chunks I don't think it will be so bad. So I'm going to start with a very simple version of this typing function.
It's just going to type out our text as though it were using the regular draw text function. So I'm going to have a draw x and a draw y variable which are going to change over time to define where we draw each letter. We're going to run through the letters one by one, starting at 1 because for some reason strings start at the index 1 unlike pretty much everything else in GameMaker.
. . We grab the current character at where we are in the loop, and then we draw it onto the screen, and then we shift over by the width of that character.
So if we stopped here we basically would have just recreated the regular draw text function, so let's add more to it. We're going to add in handling for regular line breaks so if we ever come across the new line character which is a "\" followed by an "n", then we're going to reset our horizontal drawing position back to the left side, and we're going to shift down our vertical drawing position by the height of a line, which is just going to be the height of a very large character in our font. We're going to add another block of code in the middle, and this is the complicated part, but if we just do this bit by bit I'm sure we'll be okay.
You can see this only runs when we find a space character and that's because we're looking to do this at the start of words. And what comes before the start of a word? Well, a space does, because if a letter came before the start of the word, that letter would be part of the word.
So it wouldn't be the start. So the only place where we want to insert these new line breaks is on spaces. When we find one (a space), we're going to skip past the space and then we're going to enter this sort of sub-loop, and you can see it's very similar to the loop that we started with up here.
You can see our sub-loop uses a different iterating variable, "ii" instead of "i", and we're also going to keep track of how wide the word we're currently looking at is. If we reach the end of the word, so if, as we're looking ahead, we find a new line or another space then we're going to end, because that means that we went through this whole loop without actually getting too wide to cause a line break. So we're just going to break out of this sub-loop and go back to what we were doing before.
If we go past this and we increase our width such that we are getting past the total amount of width that we're allowed to type within, then we're going to do exactly like what we did before when we found a new line character. So you can see we set draw x to 0 just like we do up here. We set draw y, we're increasing by that big letter capital "A" just like we do up here.
Armed with this function we can now create our Drw GUI Event, but before we do, we're just going to create a regular Drw Event. But we're not going to put any code in it; we're just going to make it exist to override the default drawing, and that way we won't draw ourselves in the room and only on the GUI. Now let's create the Drw GUI Event.
And the first thing to draw in here is just, well, the box part. So we're just going to draw our sprite stretched out by our width and our height starting at our corner that we defined in our Create Event, and then we're going to draw our text. So we're just going to set some basic draw variables; the horizontal and vertical line are going to be top left aligned, we're going to set the font to what we specified in our Create Event, the color, and we're going to use that typing function that we just created to actually type the text out.
So that's all of the code that needs to go in obj_textbox, but we still need to create the building blocks for how we're going to write our dialogue, and then we need to write some example dialogue to test this out. So let's start by creating some dialogue actions. I'm going to make a new script again I'm going to call this scr_actions and in here we're going to put all of those structs that have the act methods that we talked about earlier.
The very first one that we're going to create is very simple it's just going to be called DialogueAction. You can see this is a constructor function so we can call this to create a data struct, and within that struct is going to be basically nothing. There is an act method inside it, and that act method contains literally nothing.
This is just going to serve as a template for all of our other dialogue actions to build off of. So for example, the most important dialogue action that we need is the TextAction, so this action is going to what be what we use to define a page of text. Some text that is going to get typed out by the text box.
You can see that it inherits from DialogueAction, and it sets its own act method. And inside its act method we're calling the setText method that we defined in obj_textbox earlier. You can also see that as we construct this text action, we're going to tell it what text it's supposed to type out, so for example if I wrote like new TextAction "Hello.
. . world" ha, then this is going to tell our text box "write a page that says 'Hello World.
'" But because I don't want to write out "new new new. . .
you know, new new new new TextAction" over and over and over, I'd like to condense this repeated part a little bit. So what I'm going to do is, at the top of this script, I'm just going to make a macro called TEXT, and this is just going to act like I wrote "new TextAction" out. This way, I don't have to write "new TextAction" over and over and over.
Instead, we can just specify "TEXT TEXT TEXT. " This looks a little bit nicer, and as we add more dialogue actions later on, it'll be a little bit cleaner. So, now that TextAction is defined, let's write some example dialogue that our text box can actually use.
So if you recall, we store all of our conversations in topics, and we're going to put them all in a global topic struct. So let's create that struct right now. Again in another script, so I'm going to make a new script.
I'm going to call this one scr_topics, and scr_topics is just going to create an empty struct called global. topics. So this is just the struct that all of our conversations are going to be stored inside.
And that way, when we make our text box object and tell it what conversation to have, it can look them up inside this struct. Now let's create an example topic, and I'm going to call this topic, well, "Example," so I'm going to make an entry in our topics struct. I'm going to call this entry "Example" and it's going to be an array of the dialogue actions that we defined in scr_actions.
Right now we only really have the TextAction, so let's just add a few pages of text. I'm going to add a few example text pages right now, so as we run the project and we have the "Example" conversation then the text box is going to type out "Hello world! " on one page, then it's going to type out "I sure hope this dialogue system works first try!
" (and I really hope it does because if it doesn't, then I have to re-record everything) Then just to make sure that our line breaks are working correctly, I'm just going to have a page of text that has a whole lot of words in it. And this will guarantee that it needs to break a line in order to fit everything on one page. Now we also need a function that we can call, that actually creates the text box, and sets the conversation.
So let's add a new function to our typing script. I'm going to put it above "type" because this is the one function that you're going to actually need to call yourself in your project whenever you want to create the text box in the game. This is just going to be called startDialogue and you pass in a topic to it.
It checks if there is already a text box, and if there is, it just stops early because we don't want to have two text boxes going at the same time. We spawn an instance of the text box. We just spawn it really really high up (depthwise) so that we make sure it's above everything else on the GUI, and then we set the topic calling the setTopic method.
And with that we are done coding! So let's just make a demo object that implements this startDialogue function so that we can see the text box running in action. I'm going to make a new object I'm just going to call it like obj_demo or something, and when I press the space bar I'm going to start the example dialogue that we created.
So let's add this object to the room. And let's give it a run. I'm going to press the space bar; "Hello world!
" I'll press spacebar again. Looking good so far, press it again. Seems like it dropped the line exactly where it should have!
Press spacebar again and that closes the text box. But did you see something weird happen? I'm going to show again.
I'm going to press the space bar one more time. The first page didn't actually type its text out. It just immediately wrote "Hello world!
" and the reason why is order. So our demo object spawns the text box by pressing the space bar, which creates the text box, which then ALSO checks if the space bar is pressed on the same frame. And since it is, it runs its skipping code.
So we want to make sure that there's no weird order issues going on, on the frame that the text box is created, so we're going to add a little bit of extra code now just to ignore input for the first few frames the text box is active. So we're going to come on over back to the text box. I'm going to open it up.
I'm going to open its Create Event. I'm going to come back over to our input section and I'm going to add two new lines of code. So I'm going to add a max input delay and an input delay.
Max input delay is just going to store how many frames in total we wait before accepting input, and input delay is going to act like a counter that counts down to zero, and when it hits zero we start accepting input again. Now let's go over to the Step Event and we'll implement this input delay. So we're going to put this new code right before we do any input checking stuff, so right here above the confirm key parts, we're going to add in an if statement that checks if our input delay is more than 0.
And if it is, then we'll count down the input delay by 1, and we're going to stop the whole event which means we're not going to do any of the input-reading part beneath it. When input delay finally decreases down to 0, this if statement stops running and we can start accepting input again. So let's try running the project now with this fix added.
And let's try pressing the space bar. Looks like it types out on the first page now. We can also try skipping, so I'm going to press space twice, and you can see that it skips the end.
I'm going to try skipping this page as well, all good! So we have now fully created our text box system. Real quick let's also just mess with these customizable variables a bit so you can see what happens if we change them around.
Like maybe you're going to make the margin really big. I'm going to make the text speed type out really fast, uh, and let's make the total width that I can type in a lot a lot thinner as well. Let's run that and just see in action all of the properties changing.
So let's make our text box. "Hello world! " You can see it's quite a bit smaller now.
Types a lot faster, and now it's wrapping the lines way more tightly. So you can edit the text box to look however you want. You can make it, you know, feel right, whatever works best for your own project.
I want to encourage people to mess with the code in here and change it in ways that they see fit. And that's going to do it for this episode. Later on we're going to be adding more features to this system.
We're going to be adding portraits and we're going to add a speaker tag and we're also going to add effects; so like colors of words in the middle of a page changing, or maybe the letters waving up and down, in like a sine wave pattern. We'll also add custom code blocks that can run in the middle of dialogue so you can do things like set story-flags based on decisions that players choose. And of course we'll also add in branching dialogue and choices that you can make to change how the conversation flows.
But for now, I'll see you!