Did the way scripts work changed?

Elicarus

Member
Hello, I am currently watching a tutorial for gamemaker, and when it comes to textboxes and how to use them, they introduce scripts.
I know a bit of programming, and what i see in the tutorial is that they use variables create in the "create" event in a script, and when they use it it works perfectly. I think the error has to do with that, but i didn't manage to understand it better. In the tutorial, they don't have the message saying " https://help.yoyogames.com/hc/en-us/articles/360005277377 for more info", so i assumed they were in an older version than 2.3
Here is the error, and the script that caused it just behind. I think the problem is about "y" can't be called in the script because it has to do with the object and the script is independant, but I can't explain why or what to do.
pnj_not_talking_error.PNG
Any help would be very appreciated! thanks :)
 

chamaeleon

Member
If you read the whole linked page, you will see you need to wrap functions (previously just known as scripts) in a function definition. The name of your script file no longer has any relevance for functions defined in it, and can be more or less thought of as just a filename. As can be seen they (scripts) still "do" something though, so not exactly identical (code not within a function definition will effectively run at program startup, not when you think you call the code, and not in the context of some instance, hence the lack of x and y coordinates for instance to get values for).
GML:
function pnj_not_talking() {
    if (...) {
        is_talking++;
        global.talking = 0;
    }
}
Take the code from an old script without function definition, and just wrap it with the function definition and determine good names for parameters if any (using argument array or argument0, etc., is mostly not necessary).
 
Last edited:

samspade

Member
Hello, I am currently watching a tutorial for gamemaker, and when it comes to textboxes and how to use them, they introduce scripts.
I know a bit of programming, and what i see in the tutorial is that they use variables create in the "create" event in a script, and when they use it it works perfectly. I think the error has to do with that, but i didn't manage to understand it better. In the tutorial, they don't have the message saying " https://help.yoyogames.com/hc/en-us/articles/360005277377 for more info", so i assumed they were in an older version than 2.3
Here is the error, and the script that caused it just behind. I think the problem is about "y" can't be called in the script because it has to do with the object and the script is independant, but I can't explain why or what to do.
View attachment 36828
Any help would be very appreciated! thanks :)
Yes. They changed pretty dramatically. 2.3 is pretty recent, this summer, so almost all YouTube tutorials will be out of date on this. The link you posted is probably the best written resource for understanding how to convert the old format to the new format (as is @chamaeleon's comment) but if you want something from the ground up I have a bunch of current videos on it.
 

Elicarus

Member
If you read the whole linked page,
Sorry x)
I'm good at english but I can't read texts like this naturally yet

I understood! The post-2.3 version script feels way more like coding, I should manage without too much issues :)

But it leads me to the question : what should i do with a script i only use for one object? Still giving him his whole script? Or like... putting the function in the create event (and it that case, will I be able to access variables specified for the object)?
Thank, you all!
 

chamaeleon

Member
Sorry x)
I'm good at english but I can't read texts like this naturally yet

I understood! The post-2.3 version script feels way more like coding, I should manage without too much issues :)

But it leads me to the question : what should i do with a script i only use for one object? Still giving him his whole script? Or like... putting the function in the create event (and it that case, will I be able to access variables specified for the object)?
Thank, you all!
Personally, I won't be putting any function definitions in events (maybe there's some corner case where it could come in useful, but I haven't come across it for my own needs). I'd recommend simply using scripts to have one or more functions in (and using multiple scripts to separate different functions based on whatever criteria makes sense to you).

Any calls to those functions from an event in an object will let you use instance variables without any particular notation or knowing what the instance id or anything like that. If you want to use x or y just type x or y in your function, and when called from an event, the current instance's x or y variable will be used in the function.

You don't need to pass any instance variables as function parameters (regardless of whether they are built-in or variables you declare yourself to be defined on the instance), unless you wish to make the function instance agnostic and should only work with data being passed by parameters.
 

Elicarus

Member
Any calls to those functions from an event in an object will let you use instance variables without any particular notation or knowing what the instance id or anything like that. If you want to use x or y just type x or y in your function, and when called from an event, the current instance's x or y variable will be used in the function.
If the current instance's variable is incremented in the script, will it increment the variable for the instance too?
 

Elicarus

Member
Any calls to those functions from an event in an object will let you use instance variables without any particular notation or knowing what the instance id or anything like that. If you want to use x or y just type x or y in your function, and when called from an event, the current instance's x or y variable will be used in the function.
I guess i'm just way too used to programming in python where you can't do that. Thank you very much!
 

chamaeleon

Member
If the current instance's variable is incremented in the script, will it increment the variable for the instance too?
Referring to an instance variable by name in a function will modify it for the instance.
GML:
function move_left() {
    x = x - 1;
}

function move_right() {
    x = x + 1;
}
will do what you would expect if you call them from a Step event for instance, moving the instance to the left or the right. There's no separate variable in play just because code is executing in a function.

You can declare local variables inside the functions if desired if they serve no purpose outside the functions, and does not need to be global or instance variables.
GML:
function draw_score() {
    var str = "Score: " + string(global.game_score); // global.game_score is just something I made up
    draw_text(x, y, str); // use instance x and y variables, and local variable str
}
Called from within the draw event, it'll draw some text at the x,y position of the instance (which would benefit from the additional function calls that sets font, alignment, etc. but it's just an example).
 
Top