Using functions for code that is used often within your game is great way to make it easier to read and quicker to update. Additionally, it makes it easier to organize and understand how your code works. A GML function is also useful in processing data, especially if you will be doing the same calculation again and again. This can include sending data to the script, processing it, and returning an outcome or variable. If you are using the same code twice or more anywhere in your program, then you should consider using a function. This allows you to make just one change to update your code everywhere. Imagine a game that had over 100 enemy monsters with their own code; changing the code for each would take many hours and be prone to errors. Using a function, you could do this in a few minutes. It also allows for nice and tidy code. A function also allows you to easily share code between different game projects (which is a must if you go on to a career in game making) – saving you potentially a lot of time.
Doing a math calculation and returning the answer, even if only used once; it means that your code is easier to read through and understand
Setting a drawing or font type, formatting, and color – makes code easier to read, and quicker to set
Playing sound effects and voices – you can send through which asset to play. For example, to play a music track and stop any music already playing
Sending through an object and returning the closest instance – great for complex weapon systems
Drawing code that’s used multiple times – allowing you to quickly update it
Recording bullet hits against multiple different objects
Adding things to a DS list
Find if there is a clear path between two points
Taking in a set of numbers and returning the average
Any other GML that’s used more than once
A function needs to be declared before you can use it. There are a few choices when declaring it, I use the following method.
A function is basically a container that includes one or functions. Then, generally return a result, for example a variable, a resource name, true false, etc.
It’s good practice to give your function an appropriate name, so when you’re calling it is clear as to what it does.
Creating a Function
which returns a total of all three values.
Calling a Function
This will return true if the first instance of the first instance is higher up on the screen than the second instance, returning false if not. No one will be returned if either or both don’t exist.
Using return will return the given value and exit the script at that point, without processing any following code.
Basic Projects
- A)
Take in five numbers and return the average value (rounded).
- B)
Check whether a player is within a certain distance of another object instance. Return true or false.
- C)
Takes in text and a position. Draws onscreen with a shadow.
Advanced Projects
- D)
A script that draws an effect at the midway point of two given object instances.
- E)
Takes in two object instances and draws the angle between, as if on a compass, that is, North or West.
Useful Functions
You can also set up a function that will tell you what the given variable needs to be.
Summary
You should now understand how to set up a script, when it should be used, and how to pass arguments to it. You now know how to test if something returns as true or false and make things happen based on that outcome.