Function and Macro Libraries

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's talk a little bit about blueprint function libraries and blueprint macro libraries because they seem very similar but they have somewhat different uses so let's get started at how to make them in the blueprints subcategory here we can choose a macro library or a function Library we've already talked about blueprint interfaces in a different video before so we're going to cover that today let's get started with a blueprint function library and we'll call this something like extra math if we open this up what we'll see is kind of like a normal actor except that it doesn't have any components uh we can't add variables to it it's only a list of functions as the name implies so what we'll do in this case is we'll make a couple of math functions that are usually uh annoying to deal with so the default function here let's make something that gets us the average of a bunch of floats we can do that with an integer array by default but it doesn't have a version for float so what we can do is we can say something like a average float array and that will have a input for of course uh that being uh floats and we'll make that an array call that something like float array this is just an example of course and then what we can do is we can do a for each Loop just like we would do uh in any other scenario and then there we simply say hey uh we add a local variable and we'll call that total this works the same way as making any other function on any other actor but now what we'll be able to do in a moment here is we will be able to call this function from anywhere inside our project which is really really nice so what we can do is we can say our total will be able to uh be set to our total uh plus whatever this new array element ends up being and I will do that for all of them so that way we can total up all of the values and as a matter of fact that in itself is maybe potentially a uh very useful function to have so let's change this to uh total float array and just once this is completed we will return the value of total so we will just drag that in here and we'll have that total value now we can just make a new function as well and now we can do uh the average for a float array and the wonderful thing is we can again just make an input here a float array and this will now be able to call the total float array function as well from inside of this same function Library so we can just kind of pull that through the world context doesn't matter in this case uh we get the total out of it so that's just a existing function and then we can get um the array length which is the amount of items that are in this array and we simply say hey we want to divide our total by the amount of entries we have and we want to return that so we return that as our average and just to keep going with one more example uh we can do uh plus equal which is a thing that I always come back to because it doesn't exist by default inside unreal as a blueprint function anyway so I always give it as an example of something while like C++ more uh but we can just make it right here so we can say hey add two floats so this we'll just be two single floats so this will be our um float reference that's going to be the float that we wants to end up increasing so that will also be set to being a pass by reference I have a separate video on pass by references if you don't really get what that is about and then we'll get the value to add which will not be a pass by reference because it doesn't need to be and then with that we can set a variable by reference which will simply be our float reference plus our value to add and that doesn't need to return anything so now that we have that plus equals as well we can actually make our total thing a little bit easier too because we can just say hey our total uh plus equals that with our array element and again the word context object doesn't do matter in this case so we can just do it like this and all of these functions are now calling each other from within this function Library which is kind of neat but if I now go into just a random actor that existed in this project before I can also very easily call the plus equal or the uh total float array or the average float array functions these are just functions that I can now call wherever and whenever and this function over here can be a pure function instead I don't know that this one can be actually I set that to Pure offscreen I don't think it can be because it does have some execution uh going on but anything that is not a execution uh function so anything that doesn't have these pins over here you can set to a pure function and that way when you call them uh they become one of these smaller green notes it's just nice to be able to do so that is what a function library is you usually group these together where they like thematically fit so we have things like extra math you could make a separate function library for things that have to do with like actor location calculations or whatever that's entirely up to what you want to do with it but then what is the difference between that and a macro library because we can also go into here and can make a macro Library what is the exact difference there and the answer is the difference is actually quite small in a lot of ways uh I don't really think that this is all too useful but a macro Library will be based on a parent class so we can choose any class in our game so that's custom blueprint classes custom C++ classes custom whatever and we can then access all of the information that is on that class so we can for instance make a uh function Library here let's say um call this character macros and where in our math extra if we want to uh like get character movement it doesn't do that because of course it doesn't have any idea what a character is it doesn't have any like references to that so we would need to have a parameter in for a character or an actor and Castor to a character and that's just kind of where this falls apart now if we have our character macros this is inherited from the character class itself meaning that we can uh um do anything that we can do in the character itself in here without any trouble so we can very easily get the character movement if I spell it correctly and that's just a thing that we can do and whatever character blueprint that we uh use this function then on or this macro rather uh it will just use itself as a reference what this is doing is instead of calling a function that exists elsewhere in the code it's just using uh these inputs and outputs everything you put in between it's just condensing all that into a node that you can call very very easily uh but it is not actually a separate function call so if I go into my event graph you can ignore all this that's just uh from a previous video we can uh say our new macro that doesn't do anything because we haven't programmed anything into it but actually this is quite useful uh to be able to show because what we can do here is we can just select a full of nodes and we can collapse this into a macro within this blueprint and that then just creates a macro here so I don't know what this was uh something debugging related and then we can just call that same macro every single time and it's just a slightly more compact way of dealing with things that take a lot of noes a macro library is just something that does that for anything that is of a specific class or the children of that class and other than that it works very similar to making functions we can give them inputs something like um let's make something that sets our character movement speed so uh we say speed that will be a float input and then the output will be it doesn't necessarily have to be anything but let's make the was is updated for instance just for the example to be honest and check whether or not the speed that we're trying to set uh was something different from what it was already uh that might be useful information in some cases so we can uh get the walk speed and we can check whether or not that is equal to the speed that we're trying to set it to if it is uh what we do we put that into a branch and then we hook up these execution pins as well it's a little bit messy the way I'm doing this right now but I'm just showing it off to you and unlike a fun function this is also important a function can have multiple return paths right we can have as many return nodes in a function as we want uh for a macro that works slightly different uh the way you do that in a macro is you can see we have these inputs and outputs um and that includes the execution which I generally like to be on top uh so those are just inputs and outputs so we can just say hey uh we want to was equal and that will be a execution and then we can also make an execution for was not equal so I we'll do that was not equal and this is the way you make a lot of the nodes that you know that have multiple different output pins uh there's some other things that have to do with that but we're not going to get into that right now uh so instead of doing a bull uh we can just do two different execution paths here if they are already equal uh we can just output it like that if they weren't already equal what we can do just set max walking speed to whatever the speed we inputed is and then that will go into the was not equal path and let's call this uh set movement speed and now back in our third person character we can just call a set movement speed as a mecro which takes in a execution and a speed and then has two different outputs depending on whether or not it actually updated the speed to something new or not that is how we can use macros still very very powerful still very good and nice to be able to use personally I think the function libraries are a more important thing to get a good Gras B but you really need to understand uh when and how to use both in order to make the most of them and a very big thank you to all of my patreons you can see them on screen right now if you want to help out supporting the channel there's a link Down Below in the description to the patreon page and a special thanks to my cave Digger tier patreons Serge us
Info
Channel: The Game Dev Cave
Views: 945
Rating: undefined out of 5
Keywords: unreal functions, unreal macros, function vs macro, unreal function vs marco, unreal engine, gamedev tutorial, gamedev, coding, blueprint, the gamedev cave
Id: vsE-kpwE_ak
Channel Id: undefined
Length: 11min 33sec (693 seconds)
Published: Wed Mar 20 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.