• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Is there a better manual for scripts?

A

aelyrin

Guest
I've read the pages on scripts in the manual several times, but I just can't grasp it.
I feel like it doesn't use terms I can understand, yet at the same time also doesn't explain enough.
What are arguments, exactly?
What do I do with them?
How do I read a script?
These are questions the manual isn't answering for me.

I know I'll want to use them.

Please be kind, I know I'm not smart.
It takes longer for me to wrap my head around things...
 

Slyddar

Member
A script is basically some code that you can call and run, just by using the name of the script. You could use a script when you have a line or lines that are being duplicated in your project. By placing the duplicated lines in a script, it means you can just call the script, and the code will run. It also makes it easier if you need to change the code, as you just change it once in the script, and all the places that call it will run the updated code. If you had not placed it in a script, you would have to go around to the multiple locations where you have those lines, and edit them all manually. Not a good use of your time.

Scripts are just called by writing the name and brackets with any arguments. If you had a script called "scr_collision" you would run it like this:
Code:
scr_collision();
Arguments are the names of any variables that are passed to the script. In the example above we could pass x and y. e.g
Code:
scr_collision(x, y);
Now inside the script, the value of x and y will be referred to as the variables argument0 and argument1, respectively.

I don't want to give you too much to digest, but scripts can also return a value. You could have a script that performs a check and returns if the result was true or false, for example.

Hope that helps.
 
Last edited:
A

aelyrin

Guest
I do understand the basic concept of scripts and how they can be useful, but that's about it.

How exactly does "passing" work?
I can kind of understand the bit in the code example, but how would the script recieve that?
 

TheouAegis

Member
If you want to get technical, values you enter for each of the arguments are saved to a set of temporary global variables called argument0, argument1, argument2, and so forth. In gamemaker studio, they are duplicated also as the array argument[ ].

In your script, if you use the array format of referring to arguments, your script can use any number of arguments. If you refer to the actual variables, then you must use the same number of arguments in the script call. For example:
Code:
///sums()
var n=0;
for (var i=argument_count-1; i>-1; i--) n+=argument[i];
return n;
That code could be called then with any number of arguments inside the parentheses. It would then add up all of the arguments together, save the result to a temporary variable, and then exit the script going back to the code that called the script.

If however you refer to each of the arguments like argument0, argument1 and so forth, then you would need to make sure you had that same number of arguments inside the parentheses every time.

Do you know how to use g ml? Are you familiar with many of the functions such as place_meeting()? Those are all scripts built into the program. The values you have to put inside the parentheses when using those functions are the arguments.

look up various gamemaker functions inside the manual. They all have short example usages. Those examples are exactly the same as if you used scripts of your own.
 
Last edited:
L

Lonewolff

Guest
The script receives it as argument[0] and argument[1] in the example above.

Something like this

Code:
// scr_add(a, b)

var value = argument[0] + argument[1];

return value;    // Return the value of a + b;
var result = scr_add(4, 8);

'result' will now equal 12.
 
A

aelyrin

Guest
Took me a while, but I think I get your example.
4 and 8 are sent to the script which adds them and sends back the result, correct?

I thought about it for a while and it felt like I was getting closer to understanding, but looking at your example again, maybe I was thinking backwards.


-----
//Replies backwards because you edited your post...


Yes, I've been slowly learning different functions as I work on bits and pieces of my game.
So any that has me put something in the parentheses, they're technically arguments?
If that's true, I think I'll have an easier time of grasping it if I think of them like that.
 
T

Taddio

Guest
There's a simple concrete example you could use sometime. Say you have 2 players, each with a gamepad connected. Instead of writing in player 1 step event
Code:
key_right = gamepad_button_check(0, gp_padr);
key_left = gamepad_button_check(0, gp_padl);
//And so on ffor all keys
AND having in player 2 step event
Code:
key_right = gamepad_button_check(1, gp_padr);
key_left = gamepad_button_check(1, gp_padl);
//And so on for all keys...
you could make a script called scr_gamepad_input which could look like
Code:
///@desc scr_ganepad_input(GAMEPAD_INPUT)
///@arg gamepad_input

key_right = gamepad_button_check(argument0, gp_padr);
key_left = gamepad_button_check(argument0, gp_padl);
//And so on for all keys you want checked
And then in player 1 step event, you just type
Code:
scr_gamepad_input(0);
And you're set (you would type (1) for player 2, (2) for player 3, and (3) for player 4).
You can see it saves a lot of typing when you add up player numbers if you compare to the hard-coding method (as in the first code section).
 
Last edited by a moderator:
A

aelyrin

Guest
I think I'm starting to understand.
Tomorrow I'll try playing around with something simple and see if I can get it to work.

Thank you all so much for the help~
 
Last edited by a moderator:
A

aelyrin

Guest
Here's what I managed to come up with.
It's not much, and the script itself is pointless as it doesn't really save time in this case.

Hopefully that gif sticks around long enough. I don't have anywhere to upload it.

There is one object and the code goes like so:
Code:
//creation event just to set these

click = 0;
draw = false;
num = 0;


//draw event

draw_set_font(font0);
draw_set_colour(c_white);
var text = script0();

ty = text*25

if click = 1
{
    draw_text(x, ty, short_array[text]);
}
I shoved all this stuff in the script because it let me.
The stuff above the array used to be in the step event.
Code:
if keyboard_check_pressed(vk_space)
{
    if click = 0
    {
        click = 1;
        draw = true;
    }
    else
    {
        click = 0;
    }
}

if keyboard_check_pressed(ord("R"))
{
    game_restart();
}

short_array[0] = "zero"
short_array[1] = "one"
short_array[2] = "two"
short_array[3] = "three"
short_array[4] = "four"
short_array[5] = "five"

if draw = true
{
    num = irandom_range(0, 5);
    draw = false;
}
return num;
 

samspade

Member
While you've already received good answers, I went looking and actually couldn't find a great stand alone video tutorial on scripts in game maker. However, watch any series and the will probably explain it pretty early on. I would suggest Friendly Cosmonaut, Gloomytoad Sutdios, or Shaun Spalding. Honestly, while I don't always like his tutorials, Heartbeast has a stand alone one which is pretty decent:


If you want to watch a video outside of the game maker language, I would recommend this one:


It's a different language, and the way you write functions in it is different than GML in a couple significant ways (Processing is a typed language so you have to define return types and you write the functions in the editor) but the basic idea and use is identical and seeing it from a slightly different angle may be useful as well.
 
A

aelyrin

Guest
I was honestly trying to avoid having to watch videos, especially if they're not specifically on what I want to know, but I'll check them out, thanks.
 

Slyddar

Member
I was honestly trying to avoid having to watch videos
If you are going to be competent in Gamemaker, or most anything really, that's an attitude you should probably adjust. Watching and learning from others is one of the best ways to find out what you are seeking. There are heaps of Gamemaker tutorials, and they really are a great resource.
 
A

aelyrin

Guest
I mean specifically videos.
I can follow along reading tutorials just fine, but I have a hard time listening to someone telling me what to do.

...Yea, I did poorly in school.
 
Top