Scripts

Having trouble using scripts?  Don't know how to call one from your game? Wonder what the heck argument0 is?  Well you've come to the right place. I'm going to give you a basic introduction about scripts and how to use them.  I'm going to use simple examples, as well as examples from a game I'm writing at this time called N - Type on how I used scripts to make things easier and to reduce the amount of code duplication I had.

 

What are Scripts?

Think of scripts as pieces of code that you can use over and over again from one place by different objects. You've probably been using Execute a piece of code in your objects and for most of the time this is all you need. But sometimes you'll find that you're duplicating certain pieces of code in many different objects and if you wanted to change any of that code, you'll have to go through each and every one. This is where scripts come in.

 

With scripts, you can pass arguments to them so one script can work with a number of different things. Let me explain with a few simple examples, take this simple example for instance:

 

show_message(string(30 + 10));

 

This will add the two numbers together and show the answer (in this case 40), but even this simple function has arguments.  In this case, there are two arguments that we'll look at that can be changed (not including the plus sign):- the two numbers:

 

show_message(string(10 + 67));

 

show_message(string(43 + 3872));

 

So how would you do this using scripts? Well, create a script called scr_add and place the following in it:

 

show_message(string(argument0 + argument1))

 

and when you want to show the answer to two added numbers, you use the following:

 

scr_add(10,30);

 

This will call your script and pass the two numbers as arguments to it.  In this example, the first number is interpreted as argument0 by the script, so wherever argument0 is in the script, it will be replaced with the number 10 and wherever argument1 is in the script, this will be replaced by the second number, in this case 30.

 

So, argument0 is the first thing after the open bracket when calling a script, argument1 is the next thing after the comma, argument2 would be the next thing and so on.  So anywhere you have argument0, argument1. . . will be replaced by what you pass when calling the script.

 

Take a look at the following table to see what the various arguments will hold when data is passed to the script:

 

call to script

argument0 =

argument1 =

argument2 =

argument3 =

scr_add(10,30)

10

30

-

-

scr_mess("Andy","is,",fab")

Andy

is

fab

-

scr_rectangle(55,10,200,300)

55

10

200

300

scr_destroyenemy(id,20)

(id of obj, e.g: 100432)

20

-

-

 

divider

 

 

Here's another example, a bit more complex [taken from my game N-Type]. This script is called scr_destroyenemy:

 

// Does it needs more hits to die?

if hits > 1

{

   // remove 1 from number of hits before it's destroyed

   hits -= 1;

   // flash enemy to say that it's been hit.

   image_blend = c_red

   // Set time to return enemy image back to normal

   alarm[10] = 3

}

else

{

   // If destroyed, create an explosion and remove the ship from play

   instance_create(x,y,obj_explosion);

   SS_PlaySound(global.explosion01);

   score += 20;

   effect_create_below(ef_explosion,x,y,1,c_red)

   instance_destroy();

}

 

This example is placed in the enemies collision with the player's bullet, it simply say's "If the enemy has hitpoints left, flash the ship, else, if it has no hitpoints left, create an explosion, play a sound, add 20 to the score and destroy itself.

 

This is all well and good if I had a handful of enemies, but there will be at least 50 different types of enemies, so this piece of code must be placed in every one of the enemy->collision with bullet events. But what happens if I wanted to change something? Say I wanted the enemies to flash blue instead of red (image_blend = c_blue)?  Or I wanted the score to be different for every type of enemy?  This means I'd have to go through each and every one and change them all by hand, there must be an easier way! Well, there is, and, yup, you've guessed, it scripts.

 

As we've seen from the examples earlier, You can pass arguments to the script which will be interpreted by what is passed.  Let's first look at being able to change the score by passing a number to the above script.  The script does need a slight change:

 

// Does it needs more hits to die?

if hits > 1

{

   // remove 1 from number of hits before it's destroyed

   hits -= 1;

   // flash enemy to say that it's been hit.

   image_blend = c_red

   // Set time to return enemy image back to normal

   alarm[10] = 3

}

else

{

   // If destroyed, create an explosion and remove the ship from play

   instance_create(x,y,obj_explosion);

   SS_PlaySound(global.explosion01);

 

   // Here's the change***********************************************

   score += argument0;

   //       ^^^^^^^^^*************************************************

 

   effect_create_below(ef_explosion,x,y,1,c_red)

   instance_destroy();

}

 

So now we can call the script and change the score:

 

scr_destroyenemy(30);

 

This will add 30 to the score as anywhere in the script argument0 is will be replaced with what was passed.  But now how do we know which type of enemy is calling the script? Well we can determine that by passing another argument, the id of the instance (this can be done quite simply by using the in built variable "id". Each instance of an object has it's own unique id, so you can have 10 'copies' of the same object and be able to manipulate just one of them).  Again, the script needs a slight adjustment:

 

/* As were passing 'id' as an argument, this script works with the particular

  instance, not object, that called the script. This way, this script

  acts on just one instant of an object and will destroy that particular one

*/

 

// Here's the first change ************************************

with (argument0)

//   ^^^^^^^^^*************************************************

{

 

// Does it needs more hits to die?

if hits > 1

{

   // remove 1 from number of hits before it's destroyed

   hits -= 1;

   // flash enemy to say that it's been hit.

   image_blend = c_red

   // Set time to return enemy image back to normal

   alarm[10] = 3

}

else

{

   // If destroyed, create an explosion and remove the ship from play

   instance_create(x,y,obj_explosion);

   SS_PlaySound(global.explosion01);

 

   // Here's the second change****************************************

   score += argument1;

   //       ^^^^^^^^^*************************************************

 

   effect_create_below(ef_explosion,x,y,1,c_red)

   instance_destroy();

}

}

 

and to execute the script we use

 

scr_destroyenemy(id,20)

 

id will pass the id of the particular instance that's calling it, and 20 is the score to be added, so GM will actually interpret the script something like so (pay particular attention to the with part and the score part):

 

/* As were passing 'id' as an argument, this script works with the particular

  instance, not object, that called the script. This way, this script

  acts on just one instant of an object and will destroy that particular one

*/

 

with (100683) // argument0 - One particular enemy out of a number of the same enemies

{

 

// Does it needs more hits to die?

if hits > 1

{

   // remove 1 from number of hits before it's destroyed

   hits -= 1;

   // flash enemy to say that it's been hit.

   image_blend = c_red

   // Set time to return enemy image back to normal

   alarm[10] = 3

}

else

{

   // If destroyed, create an explosion and remove the ship from play

   instance_create(x,y,obj_explosion);

   SS_PlaySound(global.explosion01);

   score += 20;  // argument 1 - the score to be added

   effect_create_below(ef_explosion,x,y,1,c_red)

   instance_destroy();

}

}

 

And so concludes the Script Tutorial, I hope you found it useful and could understand it!

 

Revision #1