© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2023
B. TyersGameMaker Fundamentalshttps://doi.org/10.1007/978-1-4842-8713-2_24

24. Functions

Ben Tyers1  
(1)
Worthing, West Sussex, UK
 

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.

Some examples for scripts:
  • 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

First create a function in Scripts assets, as shown in Figure 24-1.

A screenshot has a dropdown of various tools, and select script tool, which opens a sub-menu. Highlights create options from the sub menu.

Figure 24-1

Creating a new function

You can then name the function and pop in some code, for example the following:
/// @function         get_total(value1,value2,value3)
/// @param {real}     real
/// @param {real}     real
/// @param {real}     real
function get_total(value1,value2,value3)
{
      var total=value1+value2+value3
      return total;
}

which returns a total of all three values.

This would look like that shown in Figure 24-2.

A screenshot of 9 line code. Introduce get total functions for three values. Add 3 values and execute the results of the total.

Figure 24-2

Showing function added

Calling a Function

You could call this function with the following:
get_total=function_example(10,12,40);
Then do something with the value, for example:
draw_text(100,100,get_total);
You can for example send through the ids of two instances function_check_y:
/// @function function_check_y(value1,value2)
/// @param {real} instance 1
/// @param {real} instance 2
function function_check_y(inst_1,inst_2)
{
      if (instance_exists(inst_1) && instance_exists(inst_2))
      {
            if inst_1.y < inst_2.y
            {
                  return true;
            }
      }
      return noone;
}

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.

A script does not have to return a value, it can just be used to make something happen, for example, you could set it to make an effect over two given ids:
/// @function function_effect()
function function_effect(inst_1,inst_2)
{
      effect_create_above(ef_spark,inst_1.x,inst_1.y, 1,c_yellow);
      effect_create_above(ef_spark,inst_2.x,inst_2.y, 1,c_yellow);
}

Basic Projects

Create a script to do each of the following, and display any result onscreen visually as required, remembering to set up any text drawing style and alignment.
  1. A)

    Take in five numbers and return the average value (rounded).

     
  2. B)

    Check whether a player is within a certain distance of another object instance. Return true or false.

     
  3. C)

    Takes in text and a position. Draws onscreen with a shadow.

     

Advanced Projects

  1. D)

    A script that draws an effect at the midway point of two given object instances.

     
  2. 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.

For example:
/// @function function_example(value1,value2,value3)
/// @param {real} value1
/// @param {real} value2
/// @param {real} value3
function function_example(value1,value2,value3)
{
      total=value1+value2+value3
      return total;
}
Then when typing function_example in an Event, you’ll see a reminder at the bottom, as shown in Figure 24-3.

A create window for Object 1 has 2 lines of code. It displays function examples for 3 values as 10, 23, and 99.

Figure 24-3

Showing function information

You can also set it to use a default value if an argument is not passed to the script, for example:
function draw_text_shadow(xx,yy,text,col=c_red)
{
      draw_set_font(font_text);
      draw_set_colour(col);
      draw_text(xx-2,yy-2,text);
      draw_set_colour(c_white);
      draw_text(xx,yy,text);
}
So the following would draw the text with a green shadow:
draw_text_shadow(200,200,"Example Text",c_green);
whereas this would draw a shadow with a red shadow:
draw_text_shadow(200,200,"Example Text");

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.25.195