Game Maker Studio 2 - Broadcast Message Event

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
greetings and welcome my name is aaron craig with let's learn this together today we're going to be tackling the broadcast message event something that is relatively new in game maker studio but has an incredible amount of power and is probably being underused by most of the community using gamemaker studio so first off what is the broadcast message event well gamemaker manual breaks it down for us really well it is an event that you can call on your objects to listen for messages being sent out by sequences and by sprites now it also is used in the gesture events but we're not going to worry about that right now once you understand how to use it here you'll know how to use it in the gesture events as well so essentially a sequence and a sprite if you add a broadcast message it will send that out for every object that has the broadcast message event and then you can look to see which kind it is and if it's the specific message you want and then you can do something based on that information so this is a really powerful way to have code run at a specific time in your objects based on a sequence or where the sprite is at now the sprite i'm going to be using today is the knight from the medieval fantasy character pack you can find on itch io and it is by o c o however that is i'll put a link to this in there in the comments below if you want to check this out and support them so let's go ahead and check out what i've got here i have a project with just an object i've created nothing in there and the sprite that i've brought in and it is a death sprite so the goal of what we're going to do is create a broadcast message inside of both this sprite and a sequence that we create and then have this object pick it up and run some code based on that broadcast message so the first thing we're going to need is that broadcast message event which you can find under other and inside of broadcast message here it is so i'm going to delete everything inside of here now the manual does have an example of how to do this so i'm actually going to just grab this code and then throw it inside of here and so let's break down this code really quick we have an if statement checking the event data now event data is a built-in global variable that you can access pretty much from anywhere but it's only going to be useful inside of the broadcast message and it comes in as a ds map which is a data structure map essentially a 2d array and this question mark is a direct accessor to the key event type that's all this is it looks a little weird if you haven't used accessors before but all we're doing is getting the key the value for the key event type and we're just accessing it directly right here and if it is equal to sequence event then it checks to see the message type and then we have a switch statement here which goes through case by case of what it could be so let's do the sequence first and then we'll do the sprite so i'm going to make a new sequence i'm not even going to bother naming it and i'm just going to drag our sprite inside of here and zoom in because it's a pretty small sprite now this actually plays for 89 or 90ish frames uh if we go through if i go all the way to 90 it actually restarts so i cut it off here so if we play this it's just playing the death animation and then it stops right there perfect what i want to do is at the very end of this animation is i want to run some code and i'm going to do that with the broadcast event by adding a broadcast message and i'm just gonna say this is called death and if i put this right here at the end i now have a broadcast message so at this frame in the sequence it will look for any objects listening for the broadcast message and it knows that if it has the broadcast message event so if no objects have it it's not going to work it won't do anything because you don't have any objects listening for it and inside here if you don't have this code that's specifically checking to see if it's the kind of event and message you want it also won't do anything so there's a lot of setup to have it work exactly as you would expect but once you get that in you can use this in every sequence you have in your game and it's very powerful so we have a broadcast message called death that is triggering at the end of the sequence now we're going to switch this case of hit to death and you got to make sure it is capitalized and spelled the same because it's looking for that string specifically it is not a number it is not just an id it is actually a string it's checking for and once we do this then we can put in whatever code we want i don't have this audio file but you can imagine you might want to do something such as reset your game so put your player back to where they were change their sprite update the score do all of these things so i am just going to say game restart and this is just going to reset everything just to show you that this is actually happening now the last thing we have to actually do before this works is go into our room and put in the object that it's listening for if you don't put in an object in the game well it's not going to work so we've got the object here and then i'm going to make an asset layer and bring in our sequence that we've got right here now this is super small so i'm going to go ahead and make a camera i'm going to do 340 i'm going to change the width to 320 by 240 and then i'm just going to have it follow well we don't actually have any i guess i'll put it on this object listeners will be the quickest way to do it and i'll put the listener right there this way we can actually see a little bit easier the sprite playing there we go it dies and it triggers that game restart so let's actually take a look at the code of what's being brought inside of here and how it's working so that we can understand it a little better now if you enjoy this kind of thing check out my website let's learn this together.com for some more advanced courses and a new book that i just launched becoming a game developer in just 30 days if you don't know where to start that is the perfect place now here we have our object so we're triggering right here we have event data so now to view the event data remember it's a global variable so we'll go over to variables but it's also a built-in piece of data so it's not going to show up in the globals because that's our variables so instead we can actually go to watch and type in event data and then we can click view as and change it from real to ds map and then this is going to allow us to see everything inside of the event data so we have an event type which is what we're looking at right here and it says sequence event and this is in quotes which means it's a string the message is death and then we also have an element id you can use this element id to check which sequence or which broadcast message this is coming from or where it's coming from so that you know which one it's on because there are times you'll play several sequences and sometimes the same sequence multiple times in my card game i had an event broadcast message just like this and it was triggering the damage that was being dealt but when i had two cards that attacked it triggered that sequence twice so i had to find a way to make it so that it only triggered one time even when multiple cards attacked and you can do that with an element id or by adding in another check here that you only trigger once so this is our ds map and that's how we look at it and that's how you run this code inside of here now i also talked about doing it inside of sprites and you can because right here is a broadcast message so if we come over here and this time we add another one let's put dying this time and we'll come up here and this time it's not a sequence event but i actually don't know what the name of it's going to be so instead i'm going to go ahead and delete this death event so i'm going to come over here click the trash icon so that's now gone and now what i want to do is actually listen for this so i'm going to go back into our room and i'm going to delete the asset the sequence and just bring in our sprite and i'll put it like right here and i think that should be closer to the middle of the actual camera so now when we just run this it's our sprite and our sprite is set to a loop so it's going to keep going kind of looks like it did before with the sequence but the sequence isn't in here it's not actually running that code but the broadcast message is still being sent out so i'm going to run this in debug mode and boom here it is now i want to see what this actually is i'm going to change the event data to dsmap and now this time the event type is sprite event that's what i needed to know i figured it was something like that but i also didn't know the exact wording on it and if you don't know find out so now i'm going to come down here and say else if event data open this up we're still looking for the event type first but this time we're looking for sprite event and so if it's this then we're going to do a switch on event data the message inside of it this part stays exactly the same because we're actually looking at the message and the case that we're looking for is dying so we'll put this in here and this time we'll do a show message we'll just say i'm dead and now when we run this it's going to pick up this event and it says i'm dead so you can imagine here that you can put in any code you want you can run your own functions you can create a new object and the sequence change the sprite do anything you want at any time during any of your sprites and any of your sequences playing which is really really powerful so you could easily use this instead of the animation end event and it would give you even more control because you could in theory put a broadcast message wherever you want inside of here so one thing you could do is if you had an attack combo and you had a broadcast message you could put it right where you want in the attack combo and if the player is pressing the right key at that event then they can continue with that combo here maybe there's a chance to actually like heal for one if they press the right key before the player is all the way down and dead the options are limitless but that gives you the tools to be able to use the broadcast message event and now you know how to use it so this is for sequence sprite and you also use it for gestures and now you know how to figure out exactly which kind of event that is and anything else you need to do because you can actually go in and view event data in the debugger and see what you need to see so hopefully that was helpful and you enjoyed it if you want to see anything else for me leave a comment below if you have any questions don't hesitate to hit me up thank you very much for joining me and as i like to say keep making keep learning and i'll talk to you later a huge thank you to all of the awesome people who support me over on patreon their names are on the screen now and every dollar pledged helps me create more awesome content you can support me for as little as one dollar a month and get access to exclusive perks like my discord server your name in the credits early access to my youtube videos and courses and more check it out at patreon.com letslearnthistogether.com or find the link in the description below and become a patron today [Music] you
Info
Channel: Let's Learn This Together
Views: 561
Rating: undefined out of 5
Keywords: GameMaker Studio, GameMaker Studio 2, GameMaker Studio Tutorial, GameMaker Studio 2 Tutorial, GMS, GMS 2, GMS Tutorial, GMS 2 Tutorial, LLTT, Let's Learn This Together, GameMaker 2, GameMaker Projects, game maker studio, game maker 2, Game Maker Studio 2, Broadcast Message Event, Broadcast Message, Message Event, powerful, underutilized, run code, run code sequence, code sequence, event data, ds map gamemaker, game dev skills, game maker studio 2 tips, gms beginner, indie
Id: q5-82sebeYc
Channel Id: undefined
Length: 13min 14sec (794 seconds)
Published: Fri Sep 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.