Get text to appear and disappear in GameMaker with trigger events DND

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so in this tutorial we are going to look at creating uh some interaction with objects so you can see i've got my player here so my basic game at the moment we've set up some collisions so my play can move on the arrow keys and we've got basic collision setup between the boundaries here what we want to do is have it so that when my player comes into contact with this other object here which is the oh helper we want a message to appear and if we collect the apple we want the apple to disappear and then we want to be able to collect the chocolate bar but what we're going to do is to make it a little bit more complex we're going to add in a uh if statement to say that if we try to collect the chocolate bar before the apple we're not going to let our player do this so it's a little bit logic nice and easy and we'll get going so the first thing that we are going to do is we are going to have to work out the a way to get our message to appear now what i i'm going to do is i'm going to put the message appearing at the bottom of this screen here now on the side on the right hand side you can see i've got some objects already one of them is okay manager so what i'm going to do is just draw a rectangle at the bottom here i'm going to use um a draw ui event to do this you don't have to um but it does give you a few advantages if we were to do that so i'm going to click on add event draw for gui and what we could do uh is you could have this faded in fading out only when messages are appearing right but i'm not going to do that on this uh example we're just going to just do it uh straight away so um in the draw gui event of the old game manager we are going to look for a draw rectangle so i'm going to drag that in and you can see i've got left top right bottom now before we do that i just need to get with the rough sort of area so it's it's this area here at the bottom so if i go with my mouse and keep an eye on this uh the bottom numbers here it needs to start about sort of 670 pixels down so if i go back to my workspace and we say right okay but left is going to be zero top is going to be 670 right i'm going to cheat now with a little bit i'm going to say room width and i'm going to say then same that bottom room height and what we're going to do is also make sure we choose a fill color so i'm going to use this pink bucket at the top here um i'm going to make sure this is ticked and by default there's white so let's change the color to make it stand out a little bit so i'm going to go with the yellow i'm going to click ok and let's just run that now to see what that's done okay so quite nicely there you can see we've got now a yellow patch right to the bottom um it's always visible it's always going to be there that's absolutely fine for this uh quick tutorial so so far so good we've made a um we've made a little banner here so the next thing we're gonna do now is have some form of interaction with our helper so what we are going to do is i'm going to open the o helper and we are going to get a message to appear now before we do that let's just think about this we need to know how far away my player is from this object here so we need to work out the distance between them now fortunately gamemaker does have a few things uh there to help us so what i'm gonna do is in our old game helper i'm going to add an event which is going to be a create event and i'm going to create a variable called distance and i'm going to set it to zero and what i want to do is i want that distance to update all of the time so i'm going to add another event and this time we're going to add what's called a step event and inside the step event we're going to add in another variable and we're going to say distance again and this time i'm gonna work out the difference between the o helper and i want to know the position of the o helpers x minus the o player x and note this i put a dot x and dot x on the end of these that gets me the x variable now i'm also going to put in just to show and make it visible for now i'm gonna get a debug message and we're just gonna output the distance variable here so let's play that okay let's see what that does so at the bottom now you can see it says eight nine six in this output and if i move towards the right you can see this number is going down down down down until uh where if i stop there if i keep on going and we go past it goes in then to a negative number okay so i don't need to worry about that negative at the moment but obviously we're coming in between a certain number now what i'm going to do is get the message to appear really when we're about 140 135 sort of pixels away so in order to do that we now need because we've got our distance worked out we need two i'm just going to delete that for a second we're going to add in another draw gui event and this time i'm going to use a little bit of logic so i'm going to use my if statement and i'm going to say distance so if the variable distance is less than we'll start with 140. so if it's less than 140 i'm going to do something over here so let's go and get some text in so i'm going to say important oh the guide says and i'm going to use what's called an escape character this backslash n and what that does is it puts anything i want then onto a new line please get the apple before the chocolate bar so this is a really good way to give our player instructions right uh or you could have like little wizards uh throughout the game just to point the player in the right direction so if we just play that let's see what happens now so you can see nothing's appearing on screen at the moment if we just walk over towards now we can s knowing either 140 let's go and all of a sudden we're now within this 140 so my place less than 140 way you can see the messages appeared in the top corner here now obviously we didn't want the message to appear there we want it to be down the bottom um and it's obviously chosen yellow the reason it's chosen yellow is because yellow is the last color we used so let's sort that out um i'm going to put it in the y position of 700 so 700 pixels down and i set the draw color set to a black it's black on white so black on yellow looks uh easy to read so let's try that again all right let's go down down down and there you are look the guide says perfect now let's see what happens when we go away and there we are when we've gone away from the guide that's uh that's fine now what you will notice there's a no black bag but there is a little bit of an issue with the way we've done this um if i was doing this properly in the game i'd look at the x and the y position um of this uh as we're only looking at the x um it doesn't matter where you are if you're up here or if you are indeed right next to the player um this will work right and the reason this works is because obviously it's just looking at one axis it's comparing the position x here to the player x here so if i was going to do this again i basically say look uh if the x is less than whatever and the y is less than whatever then you show the message and that would be as simple as just adding another uh when these f blocks in to look at the difference between the x and the y there okay so we've got some basic uh things working here so let's now just go down to the apple and what i want to do is just do a really quick collision between the apple and player so if i double click on the apple object and let's add an event and let's do a collision event with the object player and all i'm going to do is i'm just going to do a destroy incidents so that's just gonna get rid of it at the moment so essentially destroy the incidence of it get rid of it when we hit just play let's try that let's go down just go down let's go down lovely the message appears just for dough bang lovely so now we've got rid of the apple that's absolutely fine it's good and let's go back up towards the chocolate bar now we haven't done anything with the chocolate bar at the moment um but we do need to do a test now as i said earlier what we want to do is tell the player that you need to go and get the apple before you get the chocolate bar right so think of it like having a bullet for your gun right or a potion before you go and see the witch whatever you want to do with this so what we need to do is we need to have some form of trigger now the way i'm going to do a trigger is i'm going to use a variable and i'm going to use a global variable i don't normally use global variables but for this purpose i am so i'm going to double click on the ok manager and because the og manager is in my game always so i'm going to click on add event and create and what i'm going to do is drag in my thing here and i'm going to say global.apple and i'm going to set this to false now the reason i want this in my okay manages because i'm going to destroy an apple right and once i destroy it um i'm not going to be able to reference it so i make the variable here in the okay manager and because it's global it's available by everything so we've set it to false and what i'm going to do is i'm going to go back to my oh apple and inside this collision when i've collided i want to say oh apple no in fact i don't i want to say global apple and i'm going to set this value to true so basically when the game is started global apple is set to false when we collide with the apple it's going to be set to true and it's this trigger that we are going to use to check to see if chocolate bar um let's see if we can get it basically so let's um go and just try this out just make sure everything works this shouldn't it shouldn't not work but let's just make sure it does unless you didn't give any errors lovely it doesn't give any errors so i know everything i've done works so far okay let's close that and let's go to my chocolate bar and what we're going to do is we're going to do another collision now with the chocolate bar so if i click on add event and let's go to collision with the object player and before we do this you can see i've already got a variable here i'm just going to delete actually i don't need that so um in my object player collision here so when the player collides with the chocolate bar what i'm going to do is i'm going to do a really quick test so i'm going to say if the global apple so if the global apple is equal to true then what we want to do is we want to destroy that instance okay now just to test this let's see what it'll do so if we go down here now i'm going to go on purpose and try and grab the chocolate so i'm trying to grab the chocolate but obviously nothing's happening to the chocolate let's go back down to the apple i got the apple this time let's go back up to the chocolate lovely so you can see that little piece of logic now helps us to basically get certain objects in order now you can do that if you wanted let's say for example you have to get things in a certain sequence or save an order you could do these if statements just to basically see like has this one being uh had yes as this one we've had no okay go back and get that one first now wouldn't it be nice though if we had um a mechanism to basically say look if we're trying to get the uh chocolate part so let me just start out again so wouldn't it be nice if we had obviously we've got the warning here the guide says but wouldn't it be nice if we had right let's go try and get the chocolate bar another message up here and saying go and get the apple so let's now look at how we can do that now fortunately there's a few different things that we can do um the first thing that we are going to look at is using a draw event so let's open up our chocolate bar and i'm going to add in an event so i'm going to click on add go to draw and i'm going to go to draw gui and what we're going to do we're going to use the same set of objects we used before so i'm going to say right draw this so global dot apple so if the global apple is false and what we need to do then is we can use a draw value and what we can say is important and go and get the apple okay so let's just try that and see what this does okay so the problem we got now at the moment is as soon as the uh program loads it's doing this test to say look have we got the apple and you can see there look it's saying nah you run with the apple so it's pulling this up here now i'm not only using the wrong place but we need them here's the wrong color and it's just coming up straight away so what we need to do is add another testing so just before i do that i'm just going to move that down to 700 again and we're going to set the draw color to something black okay so what we need to do is we need to do another quick test and we're going to use an if object that place which is here now i'm gonna have to do a little bit of reordering here so bear with um okay so what i've done just to show you is reordered this so this is now what the logic looks like so we're basically saying like if the global apple is set to false and what we're going to do then is say because we're doing this inside the chocolate bar event if the object right the player object is rel is 0 0 with the chocolate bar then we're going to display this message this is only going to display if we touch the chocolate bar now this might i think i can spot a really quick error here the um this here needs to be above my bad so your color needs to be above the text there you are important go get the apple because it's touching let's get away from there and you can see there now it has disappeared let's just test lovely okay so you can see now we've had menus appearing sorry text appearing rather if you wanted to you could take this a little step further and you could do another little bit of logic to say that once you've had the apple the next time we appear close to this uh player you could say right go get the chocolate so you then really sort of diversify the menu you could also have a hidden piece here if you really wanted which could disintegrate as soon as you hit the apple and i would allow you in almost like a secret hidden chamber um but you see this is how we would put a lot of text events into your game to give your player a little bit of help and to give them a little bit of direction as they wander around the game
Info
Channel: A bit of everything
Views: 5
Rating: undefined out of 5
Keywords:
Id: R7_s3EFrG08
Channel Id: undefined
Length: 17min 19sec (1039 seconds)
Published: Fri Oct 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.