10 Minutes with Construct 2: Functions pt I



hello I am David ascetism and I'm trying to create perhaps a couple of videos the small videos around a suggestion that I received on YouTube first I would like to remind you about my contact so my blog my twitter my twitch channel of course and especially of mention I guess it's making templates you can find here and my youtube channel most of the videos and uploading later here even if I missed streaming them or if it's some sort of a screencast that I'm doing I try to put everything there and here you can find some of the constant 2 templates that you may use so now what what is the question that I received in my youtube channel and it was just suggesting around like why didn't you create something for functions in constructive so I'm going to jump into project I already created a couple of elements and I added a couple of things just to explain like in the video like to make it more more visible whatever we're going to do first thing I added this sprite that is an enemy and right now I'm just going to add a behavior that is going to be bullet I'm going to live like that and I'm actually going to put a speed zero at the moment the other thing that I added is the plug-in of touch so that's going to allow us to touch the screen or use our mouse and do something with that and that's very much it the outcome sorry the other thing that I added to the enemy is that I have two other animations that are yellow and blue so just to make them a little bit different and that's it so what is going to be the example well the first thing that we have to do to when we are talking about functions is to add the plug-in a function so then your object there we have here that and I don't know where this of course I'm just going to look for it and it's called function okay so how's this thing going to work basically you can put in function here in a vendor that is going to become functions almost anything that you can do in order events but the thing is that usually you are going to call a function min times you're going to call a time like a type a number of events many times and you don't want to be just writing the same code over and over and over again that would be perfect to perhaps modify where do they do it here to modify um one of my first games that I dated I think it was this one so it wasn't super gee I don't remember if it was approaching no space chained I don't know if I have it here okay this is my blog perhaps I haven't shared that that one there and it's it was one of the first games that I did with with construct 2 and I realized that it was in a lot of code so that should be a full media just to create how recreate that game and make it better but well right now what we're going to do is let's suppose that I just want to create an enemy and that's going to be our function create enemy so I'm going to write the function first and we're going to store modifying from there so I'm going to add an event I'm going to say function and you have two main things here one is unfunctional and this is when you're going to be calling the function and then compare parameters this video today I'm just going to concentrate on the basic so I'm just going to use on function and here I'm just going to call it create enemy so this is important because you could create as many functions as you want and you have to call them by name so let's suppose that well I'm going to copy/paste here and I could have another function that could be I don't know destroy enemy and I could make another function that could be I don't know and since I added the bullet behavior to this one I am going to say stop enemy so that one is not going and each one can do different things so for example I stopped enemy since this one has a bullet behavior I could say pick the enemy well pick the enemies and just say that this speed is zero that's it and then what if I just say destroy enemy well then I'm just going to say here and you need to strive that's it you basically add actions to do whatever you want so this is the one that I will wanted to create this we're going to put a little a little bit more of effort into this one and what I want to do is to create an enemy there is going to be a system create object this case is going to be the enemy and because what I want to do is to create the enemy whatever I do touch whatever I click on the on the screen of our game I'm just going to say touch dot X 2 dot Y ok now originally my enemy has a speed 0 so I'm just also going to add something here like said set a speed I'm going to add a random random value they'd have some changes that it could be between at about 10 and 300 have any control of that and the other thing that I'm going to do well I should have changed animations right so I'm just going to do something like this I'm going to rename them so I'm going to duplicate this and this is going to be color 1 this is going to be cool color too and you're going to see why I'm renaming them I'm just being lazy on how to create this and also to make it random so I'm going to do this so I am going to change the animation for color you remember that's why I named demo color and then an ampersand then I'm going to add well I'm in this case I'm not going to use random I'm going to use choose that it all works similar to the random but it's going to pick one of the numbers that I am adding to the list in this case is 1 or 2 or 3 that are the colors here so it's going to be able to pick the right thing so what's going to happen now right now this functions even if they have actions there's nothing that is calling them so that means that if I try to run my project I'm going to do it on exposure a nothing happens I touch I click and nothing happens so what I'm going to add is like the call for the function so in this case I'm going to say that when the any touch ends I am going to call my function and it's going to be here and in this case is going to be called create enemy and that's it now this piece of code basically is going to be replaced by all the sections so this section is almost like repeating all this here and it should work so now let's let's see I start touching and it's creating enemies with random values which is cool but now if I am calling the function just in one place well it makes it clear like it just like you can read it whenever I touch somewhere I just create an enemy and then all these things I can at any moment change what is greater than an enemy but it would be awesome if not only I can do this like one time I shouldn't well it would be great if I I can call the function in an order times so what happens if I want to start creating enemies every let's say two seconds so I can go system and say every X seconds every two seconds so here I want to create an enemy and if I didn't have the function I would have to copy all these friends here so I could have literally just copy here and do it here control me okay but let's suppose that I have to make a modification on how to create enemies I would have to change the code here and here and everywhere else whenever I have everything on a function the only thing that I have to do you just call it here and then all the modifications that I do here are going to affect this and disco so now let's let's see if I touch I create enemy but also every two seconds I'm also going to be creating enemies that in this case is the position zero zero because I'm not touching it in the particular place so this touch X and toge Y is just going to be the position sewers here but hey it doesn't matter so you see that right now is calling twice the function it still works so what happens about the older ones what about like destroy enemy and what about stop enemy I'm going to make them really go slower that's it so I am going to say that uh this is going to be interesting yeah well it doesn't matter it's going to do a like a weird behavior but Atlas is going to illustrate me problem what happens if I touch an object that in this case is going to be the enemy I want to stop that enemy so I in hopes of dance in general so I'm just going to say call function stop enemy at this point I'm going to mention that I didn't try to pay attention to what well how the function is called so you see that I'm right and it'll be exactly the same and it's because if you don't do it right oh well it's not going to give you any error better the same time it's not going to do anything so it has to be exactly the same thing and this is case sensitive so if you write this with like everything is capitalized like it's top enemy or capital letters and this is not really the same and then it's not going to work so just be careful of that be mindful of that so let's suppose well I'm going to have at some point and you monster can you see that is it stopping the enemies all the enemies when I'm very touched only one of them and at the same time is creating and you want why because it's executing two things at the same time it's executing on I touched but at the same time I touch it started so it's calling both functions who's calling yourself enemy and create enemy so I'm doing both at the same time but hey it works it illustrates what what I wanted to do and now how do I say that a like how the coldest on destroy enemy I am just going to create an order object over here and it's going to be but do I know green sprite something like it is it doesn't matter the whole thing that I'm going to do and want to call it destroyer and I am going to do something similar to this so on when I touch it an object but in this case is not going to be the enemy it's going to be the destroyer I'm going to call a function that in this case is not going to be a stop enemy it's going to be destroy enemy and you see that I'm I'm not specifying which enemy and this is something that I'm going to live for another video that is like how to specify which enemy I want to do it bad you're going to see it like this so here I'm going to then I'm going to create a couple more and then he touch the destroyer and destroys all of them but of course and it's also creating any one because I'm doing I'm calling basically or I'm triggering both events that are calling on and sometimes different functions this won't destroy enemy but also they want for creating enemy also here so well basically this is the first BD around functions with construct here it's a little bit late so I don't know if I'm going to create another video today if not I'm going to do it tomorrow that is going to be something around yeah functions and like a little bit more of detail on how to use some parameters but well you you get the basics here that is just create an event that has a name of the function and then some actions and then how to call them that is just having your normal event with a normal a condition and then just using the Col function action that is going to call whatever function you created I'm going to save this and then quite likely tomorrow we're going to start checking like how how to modify this and how to make it work better thanks for paying attention have a great night

7 thoughts on “10 Minutes with Construct 2: Functions pt I

  1. hello I need help because I can not put the behavior in my construct 3 it appears in the menu of complements but not in the menu of behaviors I can not use it you can help me

Leave a Reply

Your email address will not be published. Required fields are marked *