INTRODUCTION TO FUNCTIONS
We will talk about all functions. The function is significant, especially when it comes to making your code not only more readable but more functional. You can make your code do more than one thing at once instead of rewriting a ton of code. Let’s start a new project to see what I mean by that.
We’ll call this ‘Function Test’, and it doesn’t matter what the size it is at this point.
It’s basic. The first thing we add is a function.
It allows us to add a function, let’s add a ‘Sprite’ here.
We will create 32 x 32 and black.
We call this ‘Player’
We have this ‘Sprite’ on the screen, and let’s see some functions.
You will have the functions here.
You can click ‘Compare parameter’
Or you can click ‘On Function’
Let’s say you change the opacity. That’s what you name your function and what it will do. If you’ve ever taken a coding course, you probably are aware of functions, and you’re probably wondering why there are no functions in ‘Construct 3’. I mean, is it the essential part of programming? Technically you can do a lot of this stuff in ‘Construct 3’ without the idea of a function, but since functions are ingrained in coding thus, I had to put it in. The name of the function should say what it does. You don’t want a function name like this because it’s not descriptive enough of what you need.
Let’s say you set the name to ‘ChangePlayerOpacity’. As you can see, all words are capitalized.
Some people like to add an underscore.
Whatever style works for you, this is the one for me.
‘On function’ we can change the opacity of the player to 50%
Now at this point, nothing happens. Nothing happens because we haven’t called the function yet. If I go to ‘System’ at ‘On start of layout’, I call the function and that function changes the opacity of the player.
Let’s take a look at this here.
Very easy. If you want to add ‘Keyboard’, you can call this function another way.
I want the keyboard on a key pressed, we click ‘On key pressed’
I will remove it from the ‘On start of layout’ now. If I push the ‘W’ key, you can see that the function gets called.
Therefore, if you need to set the player opacity ten times, you can call this function as many times as you can.
One of the best ways to use functions is with controls.
Let’s say we’ll change this to ‘MovePlayerUp’
If I want to move the ‘Player’ up, I will add some basic controls such as setting the ‘Player’ moving an angle of 270 and the distance of 32.
I press ‘W’, but it doesn’t work anymore because I have to call the correct function ‘MovePlayerUp’
Let’s move the ‘Player’
You see that it moves up.
You can work with it on any single platform.
Therefore, if you’re working on this on a mobile device like a phone, tablet, laptop, or computer, you can run it. That’s the whole idea. Let’s say you move the ‘Player’ up.
Now, this is one thing you can do. You have a bunch of different things like ‘Set animations’ or you can ‘Set blend mode’ or ‘Set height’
You can set all these different items as you move ‘Player’ up and when creating the game.
Now, we have a ‘Keyboard’. Let’s say you want to add another type of control such as ‘Up Arrow’
Let’s look at a few things.
Let’s add ‘Touch’ and ‘Gamepad’
The first value is ‘Player.Y’ which is greater than ‘Touch.Y’. Then we will call the function again.
You just need to copy and paste it here.
You must also add another condition. We have three different types of controls here.
The next thing we can also add in ‘Gamepad’ is ‘Gamepads are supported’ and another condition is ‘On button index pressed’ and ‘On any button pressed’
Now, we have ‘MovePlayerUp’ and a function.
I want to show you a project I have been working on.
These are all different types of tabs, and I will show you how they work here.
It is a simple game. Here are all functions left to right and work.
I have ‘Gamepad’ controls and ‘Platform’ controls.
This game I am showing you is easy. Therefore keep in mind that the more complex your game becomes, the more functions you will have to add.