Install Steam
login
|
language
简体中文 (Simplified Chinese)
繁體中文 (Traditional Chinese)
日本語 (Japanese)
한국어 (Korean)
ไทย (Thai)
Български (Bulgarian)
Čeština (Czech)
Dansk (Danish)
Deutsch (German)
Español - España (Spanish - Spain)
Español - Latinoamérica (Spanish - Latin America)
Ελληνικά (Greek)
Français (French)
Italiano (Italian)
Bahasa Indonesia (Indonesian)
Magyar (Hungarian)
Nederlands (Dutch)
Norsk (Norwegian)
Polski (Polish)
Português (Portuguese - Portugal)
Português - Brasil (Portuguese - Brazil)
Română (Romanian)
Русский (Russian)
Suomi (Finnish)
Svenska (Swedish)
Türkçe (Turkish)
Tiếng Việt (Vietnamese)
Українська (Ukrainian)
Report a translation problem
once you create a script, it's simply scriptname(variableblah)
scriptname(scriptvariable = change); doesnt work for me. its in the step event?
So as an example, if you wanted to set a certain object's value via script from another object, you need a reference to that object. There's a lot of ways to do this; you could assign an object upon creation ie.
If the object already exists, you could try
Or, if there's only one of the object, you can simply have the script look for obj_whatever and things should work out fine (failing that, proper employment of a with statement should leave you covered for a back-up plan).
The last case will guarantee you're looking for a given thing, but outside of that you're going to want to assign things appropriately in your script.
If we're going with the first example up above, when you call the script from the object that declared whatever it's baby...
... should set the other instance's diditwork value to true.
If you want to decide which specific value you want to change in an object, a switch statement and an argument looking for the value in question can be really helpful, but you may need to get interesting with it (I'm a personal fan of employing constants for things like this! Or Macros. Whatever they're called now). Alternatively, strings can serve your purpose equally well and are probably more suited to more complicated situations, but your script's switch statement would need to account for every possible answer.
That is, unless GM has a way to look up if a variable with a certain name exists, and manipulate that. I think that's a thing in some languages, but if GML has it I think it's pretty recent? hm...
Scripts can't HOLD variables, but they can have variables passed to them, or reference variables within whatever's calling them; if you have an object with a variable named Nachos and call a script from it, that script can manipulate Nachos as if it's part of the object (but will return an error if you call it from an object lacking Nachos, since Nachos don't exist). If you need a variable for the duration of a script, var is your buddy.
This entire script is utterly useless, and also inefficient; we don't need CandyCorn at all! But it demonstrates what I'm talking about. Any variable declared with var gets dropped once the code is done executing, so as long as you don't have any name conflicts the variables should run through the script and then be dumped (don't declare "var Health" if there's a high chance of the script being called by objects that already have a Health variable, for example).
I also slapped a return statement on there, which is what sends information back in situations like this:
In this case, instance_nearest returns the closest instance of an object type, and that returned value gets passed to the variable calling it. Another example would be
In this case, one would expect there to be an expression - if x < y then z, for example - which gets evaluated for the if check, but ultimately all expressions do is return true or false. Since instance_exists is checking if an instance of an object exists in the room, it's returning true or false as is, so you don't need to do the expression! It's handy to know.
(Consequently, if you do "if instance_exists(whatever) = true", and the instance is present, what actually gets evaluated is "if true = true", which... also returns true! But is pretty redundant.)
So returns are super useful! But there is one other important thing to keep in mind when using them, and that is that once return is called, the script ends. This can be used to your advantage, but it's far more likely to trip you up if you don't know it's coming, so as a general rule the return expression is usually only called at the end of a script.
(I hope this isn't overload. Scripts are pretty great! I love scripts.)
For example, you want a script to return the SQUARE of an input number (scr_square)
You would call the script as
scr_square(variabletosquare)
Inside the script
Or a script to subtract the smaller of two variables from the larger and return the difference
It's possible you don't even need the VARs, I just like using them
It's just easier to type "temp" than "argument"
It might also be possible to write it as
An easy way to remember things would be to look at some other script, say instance_create()...
Everything in parenthesis is that function's arguments, so instead of (x, y, object), you could just as easily consider it to be (argument0, argument1, argument2).
GM won't give you helpful little fillers so much as it would with proper functions, though (ie, when typing the arguments for instance_create(), there's a reminder as to what those are at the bottom of the window). I wish it did! That'd be super helpful. But for now, as a general rule, you'll notice up yonder that all of my scripts tend to say what the arguments are in comments right at the start, because I'm stupid forgetful and need that sort of thing. It's a handy practice, imo, especially if you end up writing a lot of the things.
So any time I wish to manipulate a variable I am pushing to a script, I must tell the script what variables are arguments?
scr_runattackdamage(Player, TargetMon, Power)
The script doesn't need to know what strength or armour are.