Construct 2 - Variables

Construct 2 – Variables

hi i'm adam from english icon comm and in this video i'm going to discuss the variables in contract to the html5 game framework or program or tool or whatever first I'm going to open up construct 2 I'm going to close the start page by clicking on the X next to it I'm going to go to file go to new and then I'm gonna go to new empty project and then going to hold down control and I'm going to mouse wheel out I just tend to do that just to get an overview of the layout now what are variables now variables are values which can vary hence the name that's kind of what they are they're their values which you can change but you don't have to change them and different objects can have their variables so for example if I create a sprite and then let's just color this in a nice light green color and if I come over here to the left making sure I've selected this sprite I can see the words incident variable I can click on instant variable and I can click on this plus button to add an instant variable I can give it a name such as example name and this can be either a number or it can be a boolean or a text now this basically determines the type of variable I want to say so for example if I wanted to save text information I choose type text here and then I put in some text information such as that then click on OK however if I wanted this to be a number I change the type to number I can put text in here because when I try and press ok it just converts it to a number such as that but I can put a new number in here click on OK and it changes the initial value to a number and this is an initial value because while your game is running you can actually change this value you may also know that these are the variables such as lair angle opacity of the sprite authentically variables in themselves and you can actually reference these and you can change them as well now the other infant instance variable type is bullying and bullying is basically a digital type of information or variable it's either 0 or 1 or false or true so to where 0 would be false and 1 would be true and so I can actually type in true here and it'd be okay I don't think I can type in any other characters no but you changed it to false normally with JavaScript if you would have some text within a variable that would if you were to check that that would be true and if there wasn't anything it would be 0 there are other types which aren't considered in construct 2 because they were works out behind the scene scene such as the variable type null and other things like that on top of that the variable type number includes both whole numbers known as integers such as that such as that it also includes promoting numbers which are basically decimal numbers we didn't like that at all maybe oh no it has if you look here to the left that has oh she changed it but the initial value here is displayed in this way so that's an instant variable and it's a variable of this instance of this sprite now if I right click and click on clone this will also have that instant variable because it has cloned that and I can simply change this instant variable or add another one and because it's a clone and not a copy it won't actually change anything with this one this original sprite which I'm going to change this color slightly so we can see the difference but if I right click and click on copy and then click on paste it's inherited again the instant variables and if I add a new instant variable for example variable 1 then the original sprite also has that new instant variable however both of these can have different instant variables because they're do different instances of the same sprite so they can actually have different variables here and you could use that by labeling these different variables with a number which assigns these who are number in way and then using that to identify them now insert variables can be used to store data and there are other methods to store data for example arrays dictionaries XML and web storage to store other parts of data or data of your game on top of that different things such as an array which isn't displayed in the actual layout editor but as you can see we have selected it and if you were to click on instant variable you can give an array an instant variable as well and that's despite arrays themselves storing information so instant variables can be very good now there are other types of variables and you can find these in the event sheet if I right-click anywhere and click on add global variable it can be whatever name so for example global variable and this can either be a number or a text type I can also give this a description and this global variable is now available to every event sheet because it's global not just the event sheet that is within and if you change the global event sheet in this at the beginning in your first layout and it will change in another layout so for example if I open up a project right click and close this project and just to open up the project which I've been opening up recently actually in some other videos and let's take a look at some of the yeah events so I have this fade event sheet and this eventually basically creates the transition which closes the top and bottom of the layouts together as you can see that I just refreshed this so this event basically controls that movement it comes down again and it opens up again and I call that fade because normally I have a fade transition but in this case it's moving and fade is a smaller word then calling it transition I hope you didn't hear that music that made me jump if you did I'm gonna close that isn't come on again ok so yeah if you notice at the top I have a global variable which is a global text variable called next layout and what what happens in my event is that when a function is called basically it's a function called on neck next layout and I call it from other layouts and event sheets then I set the global variable up here to the text found in the first parameter of this function and this is called basically the text this changes and its value changes I then can refer to that global variable elsewhere so for example I go to a layout by its name and I just put the name of the global variable in there and I can unable to do that now this is different from something called local variables local variables are variables of chiffon within groups or nearby to functions under functions and they can only be referred to within that area of your code and I'm actually going to close this again go to file go to new go to new empty project and keep an example if I double click sorry if I right click and click on add global variable global variable correctly I can add a group this can be just functionally this is great go to add add sub event and every tick I'm if I click on write and add another global variable this is called global variable – they have to be independent names because you can't have the same name but if I drag this because I can drag this around the event sheet and drag it within a group that is now called a local number and this local number can only be referred to so if I add an action set value a global variable value I can set this to 3 let's say or 4 but if I actually I just I'll just tie this up to make it easier to read so I'm gonna try to screw this group every tick on press ctrl-c ctrl-v to copy it right click on this and call this now start of layout I'm going to delete this variable number two which is the local variable and because this local variable is different from this local variable it can actually have the same name delete that and I'm gonna double click on this event I'm going to change it to one start of layout I'm gonna go to add action go to system go to set value and I'm gonna click on this drop down menu and this tells you this tells me the scope of this event in this action I can only change the name of a variable 1 which is actually the global variable up here variable 1 I can change that but I can't actually see or I can't see for this action it's not within the scope of the local number because the local number can only be seen or is in the scope of the it's local conditions and local actions and this means these local numbers can be used for temporary temporarily holding information so you may for example have a function which calls an action and above that you have a local number which is only used once or twice whereas global variables can be seen everywhere if I right click on event sheet click on an ad invent sheet I can actually I can actually see that global variable from this event sheet as well global variable so I can set global variables from this event sheet but I can only set this local variable within the scope and the reason that's done it's done deliberately to match JavaScript and it's useful scope and if you're finding it difficult to understand what scope is imagine a telescope a telescope can only see things within its field of view and it can't see everything else and your your action your condition is kind of looking through the telescope and it can only see certain variables within that view ie the local variables such as this now normally you don't want to over use global variables to store information and if I I just give you an example if I could press control C and V to copy these and copy several of these so I've got quite a few in fact I've got 57 of them because it goes from 1 to 3 and this is 58 if I went to the set global variables and I have all those variables then I'll have a huge list and it starts to become unmanageable manageable if it's too big also because you can actually reference global variables this value for example the global variable 16 then plus another global variable 1 you'll notice that it makes things a bit more awkward because it just takes up space so there are other ways to store information such as with arrays which makes storing information a bit more manageable on top of that you if you use local variables for short-term variable storing or Malaysian it's a lot better practice than using global variables because again global variables can be used in other event sheets so you can't become a bit of a nuisance now sometimes you actually want event sheets but you may you may have downloaded a catholics file and you wanted to leave a global variable from one sheet to another sheet and you would see it down here or you may for example want to move a global variable from one of your event sheets oh another one to do that you simply press control X which cuts and copies your global variable you'll see this delete event says deleting a global or local variable also deletes its actions and doing so actually removes this action here but when I come to another sheet right clicking sorry press control V to paste these actions then reappear so this action has now reappeared so to move global numbers I'll do that again press control X to cut them press yes and then move it to where you want it and you press control V to paste it there let's change it as a variable so be careful when you're moving things above groups and objects make sure you go to the top left of the group to make sure it goes above it if you just simply put it above it sometimes it can include it within it there isn't a line you can see which tells you where it's going there sometimes it it acts a bit funny so that's basically global variables and local variables now local variables can be static normally when you use local variables when the events so when these conditions are met so these actions are called and they use the local variable information they reset the information back to the original information so so in this case it will be zero every second because every time this is called then it resets this to zero and then it sets this action to four to make sure you can change the variables and keep them changed so you may actually be increasing the number you double-click on it and make sure static is selected and this means it remembers the changes that have been made to the variable so it can now be constantly changed and the changes are safe as it were now both global variables and local variables can be constant and if you click on constant that means these are non changing so you're changing a non constant variable to a constant one any action setting the variable will be removed so basically a constant a constant variable as if this has its letters now all in upper caps and you can change it back from a constant just by doing that but it was madly has all of its letters changed to capital letters and they don't refer back as you see if I get rid of the constant and it means you can't change this value at all so if I set this as 5 then any actions I can't even set it with the oceans then every any action originally changing this number five gets deleted and you can't change this number ie it's constant and you can do the same with global variables you can make them constant so that they stay remain that number throughout the duration of your game and that can be useful if you're not planning on changing a variable at all so for example the amount of points awarded when you collect a coin may be one because your points may be referring to how many coins you have you don't need to change the number so you can keep it as a constant number but one thing to be careful is wary of if you do this is that the concept too will delete all the actions which originally allowed you to change these variables now I just show that again so if I click on constant so this is not constant anymore it's a static number I even get rid of the static click on add action so every every tick I'm going to add variable to buy one every ticket it's gonna add this variable Y 1 now if I go to local variable and click on so I click on constant click on OK it has now deleted that action so that this remains as a constant so that's the basics of variables as innocent variable this instant variants are a global variables and local variables there are other ways of storing information such as in arrays and other objects and plugins and I will discuss those in later videos I hope you found this video informative thank you very much and please remember to subscribe if you liked it thank you

12 thoughts on “Construct 2 – Variables”

  1. Hello , please how to set a global var. to the number of a sprite ( copied ) , how to set it to how much the sprite copies are exists thanks 🙂

  2. An important topic in game making with Construct 2—–although your production was well produced—I would of saved and gotten more out of this tutorial if you would of set the stage to show the true examples and why one would choose to use these variables to make game making easier to program—slow down, darlin'—-you need to show and tell—not just wiz around—thanks for the attempt, I suppose this not for a beginner

  3. how on earth do you compare variables? for example: a shooter, think defender. the player picks up a Laser, I can SETR it to laser, but I want him to be able to CHOOSE his weapon by hitting a button, 1 thru 0, BUT… IF he doesn't have that weapon yet, hasn't picked it up, I DON'T want him to get it yet. I can DO this in visual basic, but I need to do it here. Lost in Variable Comparisons (global)

Leave a Reply

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

Tags: , , , , , , , , , , , , , , , ,