Collect 'X' Items To Win / Open A Door | Collectable System - Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another mention 4 tutorial in today's video i'm going to show you how to create a collectible system so you have to collect x amount of items in order to do something else in this example i'm going to collect some items to be able to open a door and we have to collect all of them or as many as you want and you can do anything for example open the door go to new level spawn something in or win the game again in this example it's going to be to open the door so i'm hit play and show you what this is going to look like so i can get in walk over to this press e to pick it up you see it says one out of three collected up at the top and it will also fade in and out we'll go over two out of three we'll go over three out of three now if we go to this door we can also now open this door and go through let me also show you beforehand we can't actually open this door so i go over we can't open the door until we've picked up all three of our collectibles or however many you have and once we do pick them up we can now open this door so this is what we're going to make today and again it's very easy to customize and change what you want so without further ado let me delete this code and i'll show you how i've done it so the first thing we want to do is we want to open up our game mode blueprint and this is where we're going to be storing the integer to know how many collectibles we have or haven't got we can do this in the character blueprint but i'm just going to find it easier to do in the game mode blueprint instead so what you can do you can go to the world details here see the game mode override you want to use this game mode over right here if it's set as none you're going to want to set it as something and if you're using the third person blueprint you should have the third person game mode and we want to open it up so you just press the magnifying glass there to take you to it and double click to open it up here again i'm using the third person gamers blueprint and i'm going to press open full blueprint editor here what we want to do in here is we want to go off of event begin play so when the game begins so i'm going to right click and get event begin play there out of this i'm going to get all actors of class so we can get all of our collectibles the actor class being our collectibles bp which we're going to create in a second the out actors we're going to get length like so which will return however many actors there are in the level so how many collectibles we have placed down and we're going to right click the return value promote a variable naming this collectibles left setting that off of get all access of class and right click that return value and also print promoted to variable naming this one total collectibles so we know how many we have in total and how many we have left because this left one we're going to be modifying to decrease it this total will stay the same so let's create the x class there so we're going to minimize this go back to our folder for collect x to win or whatever you want and we can right click get a blueprint class create an actor and i'm going to name this collectibles bp like so open it up straight away and go back to the gaming blueprint pressing this arrow here or just setting the x class to be collectible bp there then compile save and now we should see it will print however many we have and to test this out what we do is just get a print string here with the in string being the total collectibles there like so i'm going to minimize this and just place in some random ones in here so let's just place in four for example if we hit play it should say 4 up in the top left there which works perfectly like so so now let's start setting up our collectable bp so we can double click to open up add a component add a static mesh like so and i'm just going to simply make mine a cube you can set it to whatever you want but for me i'm just going to have a cube and i'm going to minimize it a little bit like so as well and then we're also going to add another component of a box collision like so making this just a bit bigger than our cube like that compiling and then go to the event graph and delete these three nodes here what i'm going to do next is right click on the box collision add event add-on component begin overlap right click it again add event add-on component end overlap and the other actor we're going to cast to our character which for me is the third person character like so and you want to do that on both begin and end overlap with the object being the other actor there because we're checking to see if it is our character which is overlapping or not overlapping in the box and if we do overlap it we want to enable or disable the input so out of the begin overlap cast we want to enable input and out of the end of labcast we want to disable the input so if we're close enough to the collectible i.e in the box collision we're going to enable the input so we can pick it up and if we're not close enough so we're out of the box vision we're going to disable the input so we can't pick it up the target will be self and the player controller will be get player controller there like so for both the enable and disable the input so now we can actually interact with this item if we're close enough and when we interact with it we want to pick it up so what i'm going to do is up here right click and get e keyboard events or your interact action wrapping if you have one and out of pressed we're going to cast to our game mode blueprint which for me is the third person game mode so cast to third person game right there with the object and get game mode very simple like so and as third-person game mode what we want to do is get collectibles left that integer we made earlier and out of this we're going to then get a decrement integer or an integer minus an integer connecting that into the cast there and then as third person game mode once again we're going to now set collectibles left that integer before setting it after the decrement with the return value of the decrement going into there like so so essentially all we're doing is we're just getting the amount of collectibles left and taking one away from it because when we pick up a collectible we want to remove one so there's one ls that we need to pick up which this will then work perfectly for us so we're setting the amount left minus one now what we want to do after this is also destroy the actor but also make it so we have the widget appear on screen telling us how many we've collected so what i'm going to do is after the set i'm going to create widget like so the class will set up in a second and the return value will be add to viewport like so so it's going to add to the screen and after this we're going to destroy actor so we've actually picked up the item it's disappeared it's been destroyed so let's set up that widget now so we can minimize this right click on our content browser again go to user interface and create a widget blueprint and i'm going to name this collectible widget like so opening that up straight away what i'm going to do is go back to my collectible bp and again press this arrow here just to put in the collectible widget in the create widget there i'm going to compile save and that is this now set up for a collectible item so we can go back to our widget and what i'm going to do is just simply get some text place it in the middle there i'm going to anchor it to the top middle as well hitting slice to content there in the text what i'm going to do is just write a reference so i can position it where i want i'm going to write x out of y or x slash y collected so now i can move that back into the center where i want it to be like so because what i want this to be is i want it to be how many we have out of how many there is collected so if we picked up one it would say one out of four collected if we picked one up and there's four in the level and to actually make that work all we can do is hit bind next to the text and create binding and we can just input that in here so all the information we need will go into return value here and to get that information we want to go back to the event graph of this collectible widget remove event tick and event pre-construct and instead work off event construct because that's essentially like event begin play and now of this we want to cast to our game mode which for me is the third person game mode with the object once again being get game mode there like so as thought passing game modes this time what we're going to do is right-click promote a variable name in this game mode reference just so we can access it whenever we want and we compile save and go back to our get text function here for the binding so what we're going to do is get the reference there so get game mode reference and we're going to then also out to the return value here we're going to format text and a format text just allows us to edit what the text is going to look like and so format it so i'm going to get one of these open brackets there i'll put on the screen why i mean it's the one with the kind of dip in it i'm going to write in x and then get close brackets then put a slash open brackets y close brackets space collected and if we hit enter you can see we can now input x and y so what those brackets do is it allows us to put in information and put in data into this text for us to use and so the x and y obviously want to be our collectibles so out of the game mode ref what i want to do is get total collectibles with that one going into y like so and you can see that's now made y an integer out of game mode ref again i'm now going to get collectibles left and to figure out how many we've picked up that's very simple because collectibles left isn't how many we've picked up it's how many are left so what we can do is out of total collectibles and get an integer minus an integer and we're going to minus to collect what's left connecting that into x there so to figure out how many we've picked up we're going to get the total collectibles minus the collectibles left inputting that in there so now what's going to happen is when we pick one up it will tell us how many we've picked up out of how many there were originally which is going to work perfectly for us and we can compile and save that like so so now that text is going to work perfectly but what i also want to do is i want to make it so it fades on and off the screen so it looks a bit better so to do that i'm going to go back to the designer select my text and also make sure i have the animations and timeline sections on screen if you don't have them you can go to window up at the top and make sure you hit timeline and animations there then we're going to hit a plus animation naming this fade select that animation add a track adding the text so the text block we have there so if you've named it something different it will be whatever you've named it we can add a track on that text and i'm going to add color and opacity because i want to fade it in so i want to change the opacity opening that up and i'm also just going to drag over the timeline here and i'm going to make it last about 0.25 seconds so i've dragged it all the way out to 0.25 then leaving the timeline at the start i'm gonna set the a or the alpha or the opacity to zero and you can see it's fully transparent so it isn't there if i move the timeline all the way to the end so 0.25 and change that to one you can see it so if we play this you see it's fading in and if we reverse it you see it's fading out so that's gonna work perfectly for us so we can compile and save that and now we just need to play this animation for fading in and out so go back to the graph and go to the event graph and we're gonna again go off of event construct so when it appears on screen and what we're gonna do is after the set game mode ref here we want to play the animation so we can drag in a fade there or whatever you name the animation from the left over here and out of this just get play animation very simply like so and you don't need to do anything else with that it's incredibly simple then we're going to hold down d and left click to get a delay and the duration of this i'm going to set to 3 seconds but this is how long you want it to be on screen for so i want it to be on screen for three seconds again set this to whatever you like and now the completed of that delay we're going to again play animation with the in animation once again being that fade animation we made earlier connecting into in animations there and the play mode this time isn't going to be forward it's going to be reverse because if this fade fades in reversing it will fade out very simple like so after this i'm going to hold down d left click to get another delay with the duration of this being 0.3 because the animation length for me is 0.25 so 0.3 will make sure this is fired off after the animation but again make sure you set that accordingly to how long your animation is then the completed i'm just going to get remove from parent like so because what's going to happen is it's going to fade it out and then i'm also going to take it off screen but because it's faded out we won't notice it disappearing and that will work perfectly like so so compile and save and now what we've done is when we pick up a collectible it's going to appear on screen to show us how many we've collected out of how many there is and when it appears on screen it's going to fade in and fade out which will work perfectly like so so the final step is to make sure that when we've collected all the collectibles we want we can do what we want as well and this example is going to be opening a door but again you can do this for well if you want and if you want more specific help for different things let me know in the comments down below so i've already got the main part of my door blueprint set up but i do have a lot of videos going over how to create doors and again i can help further in the comments or on discord if you like so i'm going to minimize this and open up my door exit bp here and as you can see all i've got is pretty much how to open the door so again i'm going to enable and disable the input and then this timeline here it's actually going to open and close the door so what i'm going to do is offer my e or interact button again here i'm going to cast to the gamemode blueprint so cast to third person gamemode or whatever you have it named as object once again being get game mode and as third person game mode what we're going to do is want to see if we've collected all of the collectibles so do that we can get collectibles left like so and out of this we get an equal equal integer making sure it's equal to zero because if there are no collectibles left then we've collected them all we can hold down b left click to get a branch connecting the condition into there the input into the third person game mode and true interplay of our timeline to open the door because obviously if this is true so there are zero characters left then we pick them all up and we want to be able to open the door and false will go into nothing or what you can do is go into another create widget just to appear on screen to say you need to collect all the collectibles or something along those lines again that's very simple false we're just going to create widget and add the viewport perfectly like so so this should now work perfectly for us because again what this is going to do is when we want to try and open the door it's going to check to make sure that we have collected all the collectibles so there are none left which will work perfectly for us like so so compile save minimize and hit play to test this out again i've already got my door in there and i've already placed in however many collectibles i want let's say five instead so what i'm gonna do is hit play collect this one and it says up top one out of five collected and it should fade out as well perfectly like so now two three four out of five collected and that will fade off screen like that if i try to open the door it won't work because we've only collected four out five so if i now collect the fifth one as well we can see that the door should now open perfectly like so so this works perfectly and i think that'll be for this video is we've done everything we want to do we've created a collectible system in which we have to collect all the collectors placed in the map in order to do something else in this example opening a door and when we collect them they disappear and it comes up at the top of our screen saying how many we have collected and it has a nice fade in and out transition like so and again we can only interact with the door or do whatever we want once we've collected all of them so thanks so much for watching i hope you enjoyed and i hope you found it helpful and if you did make sure to like subscribe down below so thanks so much for watching and i'll see in the next one [Music] bye
Info
Channel: Matt Aspland
Views: 60,242
Rating: undefined out of 5
Keywords: ue4, unreal engine 4, unreal engine, tutorial, ue4 tutorial, unreal engine 4 tutorial, how to make a game, how to, 3d modelling, blender, unity, games design, graphic designer, ue5, unreal engine 5, collect, collectable, system, amount, of, items, to, win, open, door, collect 10, collect 10 items to win, pick up, interact, collected, widget, left, remaining, how many, fade, in, out, animation, be able, finish, game, the, find, notes, item, all
Id: r5Cx180UFEQ
Channel Id: undefined
Length: 15min 48sec (948 seconds)
Published: Mon May 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.