Global array game maker
As with local variables you have to take care not to name your global variables the same as any instance variables as that may cause you problems and make bugs creep into your games due to variable overlap, which can be a difficult issue to debug sometimes. In general you should have a single object that declares all your global variables at the very start of the game for example, in the Room Start Event of the first object instance placed in the first room of the game or a single script function that declares them all together, as this gives you a handy place to go back and reference everything at once should you need to check a variable name or edit a value.
GameMaker Studio 2 has a collection of "built in" global variables too, so you should be aware of them as you may name one of your instance variables the same or wish to have your own global variable with the same name and wonder why you are getting errors! They are easy to spot, however, as they are shown in a different colour in the code editor and also come up in the auto-complete bar at the bottom.
The majority of built in global variables are very specific and will only be used on rare occasions - and are listed in the appropriate sections of the manual - but there is one important one that is used frequently and isn't listed elsewhere:. There are also three deprecated built in global variables which you should be aware of these variables are only designed to support legacy projects from previous versions of GameMaker and should not be used in new projects :. The following form can also be used to declare global variables, but it is only included for backwards compatibility , and it is not recommended that you use this form for new projects as future versions of GameMaker may not support it.
This is not strictly necessary but is the optimal way to do it as it will reserve a space in memory that is the exact size of the array, whereas if you initialize an array from 0 upwards, the memory has to be re-allocated for every additional value added. The speed difference is negligible for smaller arrays, but larger ones should be optimised as much as possible in this way. But what if we want to initialize the array with different values for each position?
Well for that we have to manually type each and every position ourselves, but there is a nice trick to help us keep track of things there too! As you can see, we haven't used any numbers in the actual array, rather a variable to count down through the values. This has two benefits - One, we don't have to worry about typos or errors when writing out the array positions, and two, we have in the variable "count" the number of positions that the array contains, which can then be used elsewhere in the object.
Very useful! With that done how do we use an array for practical things? Exactly the same as we would use a normal variable, as shown by the following examples:. Since arrays are numbered consecutively, this means you can loop through them to perform extra actions too, like we did to initialize it:. The above code will add up all the values in our array, draw each of them and draw the total value at the end. Now that we know what a normal array looks like, let's look at a 2D two dimensional array.
As before, each number points to a position within the array, only this time each position has an "a" and "b" coordinate. Think of it as an extra dimension to our container, as it now has height and width whereas the 1D array only has height. Here is an extended example:. So essentially, an array is a container with a number of slots to store values, and each position in the container has a specific number to identify it, which is what we put in the [].
It's worth noting that the contents of an array always start at 0 and can never be negative! We've shown how to check an array for data, but how do we create the array to start with? First it has to be initialized before we can use it or GameMaker Studio 2 will give us an error. Initializing an array just means that we give each slot of the array an initial value in preparation for it to be used elsewhere in the project code.
This is important to remember as it means that you have to do a certain amount of planning before using arrays, but it is easy enough to initialize one using a repeat loop like this:. This simple code will initialize a ten-slot array from 0 to 9 to hold 0, ie: each slot in the array contains the value 0. You will notice that the array has been initialised backwards , with the last value being defined first. This is not strictly necessary but is the optimal way to do it as it will reserve a space in memory that is the exact size of the array, whereas if you initialize an array from 0 upwards , the memory has to be re-allocated for every additional value added so for a ten-slot array, initialising it in a loop would change the memory allocation ten times.
The speed difference is negligible for smaller arrays, but larger ones should be optimised as much as possible in this way. However, if you try to access a value in an empty array then you will get an error. If you already know which items you want to put into the array, you can add comma-separated values between the brackets when declaring the array:.
You should always take care to only access valid array positions, as trying to access a value outside of an array will also give an error. For example, this will cause the project to crash when run:. The array was only initialised with 5 positions, but we've tried to get position 7 - since arrays are numbered from 0, array[6] is position 7 - therefore the game generates an error and crashes. Now how do we use an array practically? Exactly the same as we would use a normal variable, as shown in the following examples:.
Since arrays are numbered sequentially, this means you can loop through them to perform extra actions too, just like we did to initialize it:. The above code will add up all the values in our array, draw each one of them and then draw the total value at the end. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.
Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta.
0コメント