10 Minutes with Construct 2 Functions pt II

welcome to gain deaf adventures I am degree citizen and today I'm going to try to create a small video like the second part of the video that revolves around functions with constructing just don't forget to check my youtube channel right here you can check most of my videos and after I string them on Twitch usually tend to upload them here so just give me your feedback and hopefully all your going to like what I'm going to do so the previous video I created a small example that was covering the basically basics to use functions with with construct 2 so here you can see that I created several functions that are called in different in different scenarios and I was just trying to show how you could in certain cases call the same piece of code the same events basically and you don't have to repeat everything so in this case is quite interesting because then I am calling basically the create enemy when I'm touching the screen or every 2 seconds anyway I'm creating an enemy because I'm using the same actions to create them so today I think I'm going to cover just one aspect of functions I feel it's well functions are quite a quick powerful are quite powerful tool in constructeam so I don't know if I'm going to be able to cover everything in these videos and I want to keep them short so today I'm just going to talk a little bit about the parameters and how you how to use them so first I'm just going to show you what we have so far and then we're going to start modifying this example so right now every 2 seconds the create enemy a function is being called and it's gonna run the enemy here but also whenever I touch the screen like here here here then I create well new enemies and whenever I touch the little green square I destroy all the enemies so that's that's really good but what happens well I'm going to run it again and you should see for example here that whenever I have several enemies and he touch one of them it stops all the enemies that we had an on screen at that moment right you see that and then it of course create an another enemy whatever I am touching because well we are calling to different to different functions that do different things so this one is screwed in the new enemy when I touch anywhere on the screen and this one is stopping the enemies that does that so but what happens if I don't want to if I don't want this of all the enemies but Eve what happens if I just want to stop one enemy so for example I create this enemy now I have to and you just want to stop this one I don't want to stop all the ones that on the screen that is what happens here so one option is to add parameters so parameters is basically a value that it could be a number or it could be a string that you send to your function so for example at this moment in this event number six we are touching an enemy so constructing knows which enemy out of all the ones that are on screen I touch that it's calling this of enemy function and it is stopping all of them because they are in different context you are in different events they're not like the link per se then here when we are saying is stop the enemy we're not talking about our particular enemies is all the enemies and that's what happens when when well for example do you destroy enemies here you are also calling all of them so how do I say like I want to stop one in particular not all the enemies so one option this is not the only one but it is to illustrate how functions work here would be to modify how I'm calling this top enemy and you see here that you can add parameters and you could add as many parameters as you want okay so 2050 whatever that I am just going to modify to add one in particular this one is something that in this event I already know that is enemy dot UID so the UID is basically the universal identifier number and I'm going to show here for a second that whenever you pick one sprite and not only sprites but in this particular case it is you see that it has UID 0 this one has another UID 3 so every element that is created on construct 2 has a UID and even if I create a copy of this let's say I do this this one has a different UID and this one is zero this one is 4 so they are both enemies that you can tell tell them apart you can say I want to work with this probably want to work with this this moment I'm just going to delete this and what I'm saying here is I am calling the function coalesced of enemy ID on top of that I'm sending a parameter in this particular case is enemy UID now how do I use that enemy to I you ID in this function well the easier way that I have is I'm going to create a sub event so you see I create just press the letter B they could have done something like this like add blank superb and so you see that is the letter B this is the one the one that I did so I'm going to keep just one and I am pick I'm going to pick one enemy well I'm also going to move this here so what let's do that letter I am going to pick one enemy so I'm going to say enemy pick and we several options and you see here that there's Wanda says pick by unique ID that's the you ID so that's the one that I want and this is where it gets tricky it's not really that complicated a bed a bed it's not perhaps obvious at first is that we have a parameter so I'm going to say done for a second for a second this is not going to work just to show you we sent a number to the function so when we're doing this we're sending a number in this case for example it could be zero there is this number here so zero goes here but I'm picking a UID that in this case I'm concerned it's not correct it should be the parameter that I have here so that's what I'm going to write notice a function dot param and then zero now let me explain this what this is saying is function is just like well the reserved word the param is too to have access to the parameter and then inside parentheses you specify which parameter you're sending so in this case is zero because I'm the counting here starts at zero so if I have let me show you if I had something like this you see that it always starts at parameter seed over zero so this is the one that I want if I wanted this one that I'm just going to send a random number if I wanted this number I would say parameter one or this one and that it could be a string so I could say hello so I am sending three parameters primer zero one and two so the one that I want is this one the enemy UID that's why I'm saying here parameter zero if I wanted to access the hello I would say parameter two and now what do I do I am just going to move this action from here to here so instead of like just saying stop all the enemies I am picking a particular enemy and then I am stopping it now let's see if this works going to create several enemies and then I'm just going to pick this the other one and that's the only one to stop all the others Arkan are moving so let's move another one here and I want to stop this red one and it stops of course I am just going to disable for a second um yeah let's let's just skip doing it like that it illustrates anyway what what we want and is that we're picking a particular enemy not all of them if I didn't do it like that if I just move this one here so it doesn't really matter I'm not putting on a connection here look at the difference I stopped all of the ones that have been created up to that point so it's quite different the result now let's suppose that I want to do something similar with would the destroy enemy right now what we're doing here is that when a whenever I touch the green square that we have here I am calling the function destroy enemy but I am destroying all of them right so let's suppose that I just want to destroy the last one that has been created so I am just going to add a global variable I'm going to call it last one well okay I could do it yeah I'm going to do it like that it doesn't matter I guess that if it's a global variable I could use it there but to explain these it works and whenever I'm going whenever I'm creating a new enemy that is here I'm just going to update this variable so I'm going to say set value last one to enemy dot you I did right so in last one I have the UID of the last enemy that has been created now just do something similar I am going to say hey whenever I touch the destroyer this green square here going to call the destroy enemy and I'm going to add a parameter and this parameter is just going to be the variable last one of course I understand that this is a global variable I could like try to use it here but this is just can I would like put an example right so what I'm going to do here is create a sub event it's going to be similar to this one actually I'm going to even copy it and well I copied inter event so linter event and I am just going to move this here so now instead of just destroying all the enemies that I do this is what was going to happen with the action here I'm going to peek in a specific enemy and I'm going to destroy it so it's going to be the last one so I'm going to create several this is the last one that well actually it's one of this so the red one over here another one and you see that it is being destroyed now the blue of course blue blue so depending on when I'm creating one I am destroying a different one better it's always last one it's not just all of them so it works now like I said before you can keep adding parameters here usually if you're passing more than ten perhaps you're doing a little something a little bit crazy but hey it's going to be your game with mine and if you don't want to have this and what you can do is just remove parameters and then I start doing like that the cool thing is that here is telling you exactly the number in case that you don't remember this thing dead the parameters start being counted at zero so you can just open here and what do you say done and if you don't know which parameter you want to use just double click on the call and you can tell that you want this one in particular for example I want parameter for so then I would use here param for so that's pretty much it in the previous example I'm going to save this one save this a single file functions example number two so we can have what do we have done so far and quite likely what I'm going to cover in the next video around functions is going to be how to set the returning value of a function and I think that's going to cover most of what you can do with functions after that really it depends on what you want to do inside the function so it's just an order event like you see that here I'm putting events and solvents and actions however you want then the function could do almost anything that you want if the advantage is that you're going to be able to call it at any point during your while event sheet and that you think it's something that is going to be repeated constantly like creating enemies or destroying enemies or updating the high score or different things then you just have to call it once and then you don't have to repeat the same code so it's going to be cleaner it's going to be easier to update and to maintain that that dead code or well this events in case that you don't think that that's code well I think that's pretty much it for the moment please let me know by my Twitter or me or in my blog or in my youtube channel that you're going to see here what do you think of this videos and if you have any other questions comments suggestions or if just want to say hi you won't be welcome and go ahead and do it thank you