How To Create Randomly Spawning Items - Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another version 4 tutorial today's going to be another quick one showing you how to create randomly spawning items so we'll have items in which a random item will spawn in that so we'll play something down and then whenever the player plays the game so each time it will be a different item as it's going to randomly choose one from the items we choose for it to be so let me show you what this looks like now so if we get in we see that where we have these three circles here that's where the ax is going to spawn you see this time around we've got a campfire and two tvs and you can see as well that these actually do work as well the actors here do work so we have the fireplace i go up to it we're getting this and then these tvs are also playing the videos that we have on here as well so each actor works in its own way the way that the blueprint should if you just place it down itself so again if i was spawning again this time we get two tvs and nothing if i go in again you can see we get a tv can't fight and nothing now we're getting tv quite a lot that's just because i've only got four different items it can be so obviously there's a very high chance that each one will be picked so try again tv nothing campfire and you get the idea this is what we're going to be getting so obviously the more items you add the less likely you are to get looping ones like so so let me delete this code and i'll show you how i've done it so what i'm going to do first is we want to create a new actor so i'm going to right click go to blueprint class i'm going to create an actor i'm going to name this one random item bp and this is just going to be the master class the masked item which will swan the other items which we want we'll open it up straight away in here we're going to add a component we're going to add a child actor like so and this is what is going to be our other blueprint which you want to set so then go to the event graph delete begin overlap and event tick and we're going to come off of event begin play we're going to drag and drop a reference to the child actor there out of this we're going to set child actor class and the reason we're doing office event begin play is because then that means that every time the player plays the game or every time this is spawned this blueprint spawned then it's going to be a different item if we do offer construction script it will only do it when we update it which isn't when the player plays the game so event we can play it's how we want to do it for the in class we're going to come off that and we're going to get a select node like so and this is where we put the different options that we want so what i'm going to do is i'm going to right click on option 0 promoter variable i'm going to name this one tv as that is what this blueprint is going to be is it's going to be my tv blueprint which i've created and i'm going to select that hit compile i'm going to change its default value to be my tv bp which i have here on option one i'll right click promote to variable and do the exact same thing so this one i want to be fire pit like so so i'll name it fire pit drag it down here compile and change its default value to be my fire pit bp like so i'm gonna hit the add pin here so i can add another one right click from it to variable name this one draws as again that's what i want this to be compiled changes default value so this is our drawsbp here and so what we're doing with this is this variable here is basically just this blueprint and it's going to be setting it as the child actor so this which is going to spawn in that blueprint inside of this blueprint which is obviously how we want to do this i'm going to add another pin and this one i'll just leave it blank so that means it's nothing so there is a chance there'll either be a tv a fire pit draws or nothing and so obviously because i've only got four different options here then that means that we've got a 25 chance of each one spawning so that's still quite a high chance which is why we had quite a few overlaps earlier when i'm showing you it so we'll see the more options you add the less likely they are to appear which is great that's good that's what you want for randomly generated stuff procedural stuff like this so now how do we actually pick a render one well that's the index here this index basically selects one of these values here so i'm going to come out the index and we're going to get a random integer in range with a minimum as zero maximum as three as we have option zero one two and three it's gonna pick a random number there so now if we compile that works perfectly like so so what it's going to do is it's going to pick a random number between 0 and 3 whichever number that is it will pick that option so if the number is 2 it's going to pick option 2 so it will spawn in the drawers so like i say this is the code done so i'm just going to select hit c to comment it i'm just going to name this spawn random actor what i'm going to do after this is also give it a random rotation now this isn't necessary for you you might want it to always face the same direction but i wanted to give it a random direction as well so what i'm going to do is i'm going to come off of the set child actor class i'm going to set actor rotation like so i'll right click new rotation and split the struct spin as i only want to change the z value so it's rotated on this head which is obviously what we want so i'll come out with new rotation z and i'm going to get a random float in range and this is going to be between 0 and 360. so it can spin all the way around in a 360 circle like so again i'll just comment this random rotation like so and again the reason we're doing z is because we go to the viewport rotate it on the z like that so you can't see it changing but you can see on the axes which one that's rotating like so so like i say this is the code done it's very quick and very simple so now if we minimize and hit play to test this and also drag these in sorry you can see this working so let's just get quite a few in here so i've got seven there so i hit play you can see we have two five bits two drawers another five bent two tvs like so we get in again and these are not gonna be different so we've got a lot of none there going again got quite a few again so it's all random again because i've got 25 chance we get a lot of overlapping but the more options you add the less likely you are to get overlaps like so so this works perfectly so i think that'll be this video done everything we want to do we've set it up so we can have randomly spawning items in here again the more items you add the less likely you are to have overlaps so you get something which looks like this and it's just completely random each time and also the player is not likely to have them all around them like this so you wouldn't really notice either so thanks so much for watching i hope you enjoyed it i hope found it helpful and if you did make sure to like subscribe down below so thanks so much for watching and i'll see you in the next one
Info
Channel: Matt Aspland
Views: 28,531
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, games design, graphic designer, ue5, unreal engine 5, random, randomly, spawn, spawning, item, items, horror, how to spawn random items, randomly spawning, ue4 random item spawn, ue4 random item, select, blueprint, actor, actors, multiple, random item spawns, bed, notes, note, campfire, drawers, drawer, dresser, dressers, fire, firepit, tv, child, class, integer, in range
Id: hdAO6098Jy0
Channel Id: undefined
Length: 6min 20sec (380 seconds)
Published: Sat Jan 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.