How to write script function in gamemaker tutorials

We will write the script/function to receive 3 parameters num1, num2 and operator using gamemaker. The script will calculate the result based on the parameters inputed then return the result (the Operator will be addition "+", subtraction "-", multiplication "*", and division "/")

Step 1: Create a new game maker project and name it with FunctionScriptGameMaker
Step 2: In the Left Directory, Right Click on Folder Scripts to Open the Script Menu Tab
             Click on Create Script ( 1st option) / or just click on Script icon on menu tool bar to create a new script
Game Maker Script Tutorial

Step 3: Name the Script as Calculator and copy these lines of code

///Calculator(num1,num2,method)
//method could be "+","-","*","/"
num1 = argument0; // argument0 is first param - number 1
num2 = argument1; // argument1 is second param - number 2
method = argument2; // argument2 is the third param - method
if(method == "+")
return num1 +num2;
if(method == "-")
return num1 -num2;
if(method == "*")
return num1 *num2;
if(method == "/")
return num1 /num2; 

Name Script Game Maker

Step 4: How to use the written script? Create a new object name it with print_Cal
            This print_Cal object will have a Draw Event. Create a new Code Action inside Draw Event.
Step 5: Copy these lines of code into the Code Action of print_Cal

num1 = 5; // first number
num2 = 4; // second number
// + method ... draw result
result = Calculator(num1,num2,"+");
draw_text(x,y,string(num1) + "+" + string(num2) + " = "+string(result));
// - method ... draw result
result = Calculator(num1,num2,"-");
draw_text(x,y+32,string(num1) + "-" + string(num2) + " = "+string(result));
// * method ... draw result
result = Calculator(num1,num2,"*");
draw_text(x,y+64,string(num1) + "*" + string(num2) + " = "+string(result));
// / method ... draw result
result = Calculator(num1,num2,"/");
draw_text(x,y+96,string(num1) + "/" + string(num2) + " = "+string(result));

Using Script Game Maker

Using Your Pre Written Script/Function by calling its name. ScriptName(argument0,argumen1,argument2...).
Here our script/function name's Calculator and we need 3 params so we call it like this Calculator(5,10,"+") for the sum of 5 and 10.
Then we use draw_text function to print the result to the game screen. (Also our script returns the result after it calculated) 

Next >> Tip 2: Gamemaker drawing text tutorials - changing fonts, colors, and positions of text

Alice

It's really useful. thank you a lot

6/3/2016 3:31:40 AM