Game maker add in gml




















There are also some that store all the key-presses as a string or that can tell you what the last key pressed was, as well as others that allow you to clear the keyboard state completely. You may find some of the built-in variables and constants aren't valid on other platforms and many of the functions won't work on mobiles.

Each input character from a key or multiple keys is defined by its UTF8 code, which is a numerical value. If any of the following examples resolves to true then the code would be run:.

Examples of use:. It is worth noting that placing this before or after the value to be added to or subtracted from will have slightly different results. The other option you have is to create a Macro. A macro can be a constant value or a single code snippet for more information on macros, please see here. To make a macro, simply click the Macros button in the Extension Properties window:. Note that if the macro is a line of code it will be evaluated every time the macro is called.

This is achieved by adding only one of the link libraries to your game as an extension and placing the other s into the list of Proxy Files from the Extension Properties window. For example, say you have a Windows extension with the dll "Haggis.

You would simply replicate this dll as a Mac DyLib, naming it "libHaggis. This DyLib would then be added into the Proxy Files and set to export when the game is run on the Mac target, and GameMaker Studio 2 will automatically use it when the extension functions are called. To add a proxy file, simply click the button in the Extension Properties window for the extension and browse to the file location.

Once it is added you may need to change the extension target to the appropriate module, and when you next run your game it will be included as a proxy for that platform.

You can remove proxy files too by clicking the button, which will remove the last one in the list each press. Once you have added your proxy files they will be stored in the YYP of your game, in the Extensions folder you can find them easily by right-clicking on the extension and selecting Open in Explorer , so if you need to edit them, you should edit the copied files that are bundled with the game and not the originals, as GameMaker Studio 2 will be using the ones in the project for compiling your game.

It is very important that your proxy files follow the naming conventions listed below, as GameMaker Studio 2 will parse these names and assign the target module for the file according to its extension and name and will automatically link the files for you.

If you do not follow these conventions your game may not work at runtime, as GameMaker Studio 2 will not be able to work out which file to use or it will not load the file properly. To create an extension for Android you have to do it in two parts. The first part would be to add the extension itself, along with the required files etc The functions and constants are added using placeholder files to group them together, so you'd add a placeholder and then define the functions and macros as explained in the section above.

To add the rest of the files though you need to first tick the Android check-box in the Additional Features section of the editor:. Once you have set this up correctly, you will need to add the required files for your extension package to work. To do this you need to click on the buttons at the bottom, either Add SDK or Add Source and then browse to the files you wish to add. Added files will be stored in the AndroidSource directory along with your extension.

You can open this location at any time by right clicking on the extension and selecting Open Extension Directory. To create an extension for iOS you have to do it in two parts.

To add the rest of the files though you need to first tick the iOS check-box in the Additional Features section of the editor:. We can use this ID to set values and properties on the instance, straight after it has been created, as shown in the following code:. What's going on here then? Well, we have create a local variable called "inst", and we're using it to store the returned instance ID from the create function.

A local variable is a "use and throw away" variable, that will only exist for the duration of the script or the event that it was declared in. This is useful for data that we don't need to hang around for more information on variables and variable scope, please see the manual.

We then use this variable to set the direction of movement for the instance we just created. When we use the ID value followed by a point". So, i this way we are setting the bullet direction to match the angle of rotation of the ship sprite. The final thing we need to do is set the speed of the bullet. Now, you could do this in the player object using inst. You can test the game now, and you should see that every time you press the keyboard spacebar down, a single bullet will be released, and you have to release and press the spacebar again to create more:.

You might think that this will prevent any further code from running after the function is called, but in GameMaker Studio 2 destroying an instance doesn't happen until the end of the event, so although we've called this function, it doesn't exit the event and the instance won't actually be removed from the room until the collision event is resolved.

What other code do we need? Well, we want the bullet to destroy the asteroids it hits, and we also want it to "split" the bigger asteroids into smaller ones.

To do this we need to be able to access the instance ID of the asteroid that is being detected as colliding with the bullet, and for that we'll use the special keyword "other". This keyword, when used in the collision event, will reference the "other" instance in the collision, so in our game, the bullet is colliding with an asteroid, so "other" will reference the unique ID of the asteroid. Here we introduce the with statement.

So, in this case, while the code is in the bullet object, it will be run as if it was in the asteroid object. This means that we can access variables and run functions on the asteroid in the collision. Like the bullet, we also want the asteroid to destroy itself, so we fill in the code like this:. We also want to "split" the asteroid based on what size the sprite is, so for that we'd add the following:.

Here we're showing just the code for when the other asteroid in the collision is a "huge" asteroid. What it's doing is checking the sprite of the asteroid, and if it's a "huge" sprite, then it will create 2 new asteroids and assign them the "medium" sprite. You'll remember that we set it to a random sprite in the Create Event of the object, and here we're overwriting it it with a different value. This works because the moment an instance is created, its create event is run and then the code continues in the event that created the instance.

We've used the "if Using "else" is simply a way to have another set of code run when an "if" evaluates to false, and adding another "if" after the "else" we are saying:.

We could add another "else if" after that to check for a small asteroid sprite, but instead we'll do something slightly different We're going to add a "debris" effect into our game, and not just for the small asteroids, but for whenever any asteroid is destroyed. For that you need to create a new sprite, set it to be a 1x1 pixel only, then colour it white.

We'll give this object a Create Event where we'll use the following code to give it a random direction and some momentum:. We also want to give this object a Step Event , so do that now.

In this event we'll make instances of the object fade out and then destroy themselves when they are no longer visible. This may seem a bit contrary to common sense, but it's a fact of life when programming! The final thing to do now is add some code to create these instances when the bullet hits the asteroids, and, just because we can, let's add them into the player object when it hits an asteroid and is destroyed too. Run the game now, and shoot some asteroids!

If all has gone well, then they should explode into smaller asteroids and create a nice puff of debris:. Before we finish this section of the tutorial, we need to do some cleaning up. In programming, there are many ways you can leave things lying around that will "clog-up" the computers memory and cause performance issues or worse.

In general this kind of error is called a memory leak , and it's something you want to avoid at all costs in your own proects, meaning that you have to be careful to make sure that your game is programmed efficiently, and you don't leave things when no longer needed, but instead destroy them in some way.

In our game as it stands, we have a memory leak! Our room is only xpx, and we wrap our player and our asteroid instances if they go outside that area. But what about our bullets? They fly out the room space Well, then nothing! Once outside the room, they are just taking up memory space without actually performing any useful task in our game, so we want to destroy them when they can no longer be seen.

In this event we'll simply add:. That's all we need to tell the instance that if it leaves the room, it should destroy itself. Memory leak averted! In this chapter we're going to be creating lives and score to make our game more interesting, as well as a few more rooms to deal with different game "states".

To get started we're going to make a new object. This will be a "controller" object, which means that we won't be assigning it a sprite as it's going to sit in a room and deal with things "behind the scenes". We want this object to track the player's score and lives values, so we'll just use the built-in global variables score and lives.

A global variable is basically a variable that has no "owner". It belongs to the entire game, and not one particular object, and can be accessed and changed by everything at anytime. As the name implies, this event will draw things to the screen.

Like the Step Event, it will run every game frame, otherwise the things you drew would only be visible for one frame and then disappear. Add the following code now:. You'll notice we call the function string on the global variables. This is because you can't add two values that are of different data types, and in this case we have a string "SCORE:" and a number the value of the score global variable.

To avoid this issue, we use the string function to turn the value of score into a string data type - so if the score was , string score would return "", and that can then be added onto the "SCORE: " string adding strings like this, concatenates them.

Press play now and test the game! The score and lives values should be displayed in the top-left corner:. Before we continue with the programming of the display we're going to add a new resource type to our game: a font resource, which is simply a collection of characters to use when drawing text.

To create this, use the right mouse button on Fonts in the resource tree and select Create Font , which will open the Font Editor :. In the tutorial we'll use Consolas , and we will also switch off the anti-aliasing option to give a more pixelated and retro look to the font. We can now look at updating the score and lives values as we play.

At the top of the code block, before everything else we're simply going to add in the following:. Since the score variable is global in scope, if we add to it from any instance then it will be updated for all instances, meaning that if we run the game now, we'd see the score value being drawn to the screen go up as we destroyed the asteroids.

However, before testing this, let's deal with the player lives too. Here we're going to deduct 1 from the lives, so go ahead and add the following code before the rest of the code in the event:.

If you test the room now you'll see the score go up when you shoot the asteroids and if the player ship collides with an asteroid the lives will go down. There's still work to be done here, but we'll come back to it later after we've set up some more rooms.

In order to add things like menu screens into our game we will create a few more rooms. You can hold down and then click and drag to select the instances to remove and then press to remove them.



0コメント

  • 1000 / 1000