GameMaker: Studio

GameMaker: Studio

View Stats:
darksouls Feb 14, 2015 @ 10:27pm
Pushing variable changes to scripts?
I just have a single argument but

script_execute(script, variableblah=this)
just gives me an error?

I just want to manipulate variables with a script when its being called from another object and am wondering on the proper syntax? Thank you
< >
Showing 1-15 of 26 comments
Blind Feb 14, 2015 @ 10:28pm 
script_execute isn't actually for scripts, it's for calling events when they aren't to be called

once you create a script, it's simply scriptname(variableblah)
darksouls Feb 14, 2015 @ 10:41pm 
yeah I tried that and it wont work for some reason

scriptname(scriptvariable = change); doesnt work for me. its in the step event?
Blind Feb 14, 2015 @ 10:46pm 
What are you trying to do with your script - a script accepts a series of inputs, you wouldn't have math in the argument list
darksouls Feb 14, 2015 @ 10:48pm 
trying to control a true or false statement from another object using my script. the variable is stored inside the script.
Blind Feb 14, 2015 @ 10:49pm 
nothing is stored in a script, it's stored in the object calling the script
Sera Feb 14, 2015 @ 11:09pm 
scripts work like any other function in GM; that being name(argument, argument, etc.). Individual functions don't hold values, but objects and their instances do, as do globals as an alternative depending on what you're after.

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.

MyBabbu = instance_create(x, y, obj_ShutUpMom);

If the object already exists, you could try

MySlave = instance_nearest(x, y, obj_YourAnticipatedPunchlineIsInPoorTaste);

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.

//Let's call this scr_set_val(); //argument0 = object being called //argument1 = value to set. { argument0.diditwork = argument1; }

If we're going with the first example up above, when you call the script from the object that declared whatever it's baby...

scr_set_val(MyBabbu, true);

... 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...
darksouls Feb 14, 2015 @ 11:16pm 
oh I see now, all scripts are just lines of code called inside of an object? So they only interact with a defined variable found elsewhere that can be called inside of the script but only stored in another object?
Sera Feb 15, 2015 @ 2:42am 
Pretty much, yeah. Whatever you name the script is what you call in the code, and the arguments provided are how ever many you end up asking for in the code itself (argument0, argument1, etc., through something like 16 I believe? It gets pretty up there).

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.

//scr_multiply(); //argument0 = value to multiplied //argument1 = value to multiply by { var CandyCorn = argument0; return CandyCorn * argument1; }

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:

MyTarget = instance_nearest(x, y, obj_Target);

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

if instance_exists(obj_tatertot) then { ... }

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.)
darksouls Feb 15, 2015 @ 7:33am 
What exactly are arguments? Are they just numbers inside of a script or is there some more documentation I can read on them? Right now I just launch scripts without any arguments as I do not know how to use those.

Blind Feb 15, 2015 @ 9:34am 
An argument is an input for a script. It's possible to have a script with no arguments, to perform known as repetitive tasks, but any time you try to do something specific you'll need an argument

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
var temporaryvariable = argument0; (argument0 is the first argument) temporaryvariable = temporaryvariable * temporaryvariable; return temporaryvariable;

Or a script to subtract the smaller of two variables from the larger and return the difference

scr_difference(somenumber1, somenumber2) var tmp1=argument0, tmp2=argmument1 (argument1 is the second argument) if tmp1=tmp2 return 0; return max(tmp1,tmp2)-min(tmp1,tmp2);

It's possible you don't even need the VARs, I just like using them

if argument0=argument1 return0; return max(argument0,argument1)-min(argument0,argument1);

It's just easier to type "temp" than "argument"

It might also be possible to write it as

var tmp1=max(argument0,argument1) var tmp2=min(argument0,argument1) return tmp1-tmp2;
Last edited by Blind; Feb 15, 2015 @ 9:38am
Sera Feb 15, 2015 @ 3:07pm 
Aye, arguments are super handy and great!

An easy way to remember things would be to look at some other script, say instance_create()...

instance_create(x, y, obj_whatever);

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.
darksouls Feb 15, 2015 @ 4:36pm 
So to use arguments I must declare them in the script like argument0 = 0+1 ?
Sera Feb 15, 2015 @ 5:16pm 
arguments are predefined and default to 0. The number you use determines how many GM assumes the script requires.
darksouls Feb 15, 2015 @ 5:34pm 
so script_name(slot1, slot2, slot3) is how I should read this depending on what the argument is?

So any time I wish to manipulate a variable I am pushing to a script, I must tell the script what variables are arguments?
Blind Feb 15, 2015 @ 5:42pm 
Ya, so if you want to do, say, an RPG style damage calculation

scr_runattackdamage(Player, TargetMon, Power)

var attack, defense, critical; attack=argument0.strength; defense=argument1.armour; critical=1 if rand(6)>5 then critical=critical*argument2; return (attack-defense)*critical;

The script doesn't need to know what strength or armour are.
Last edited by Blind; Feb 15, 2015 @ 5:45pm
< >
Showing 1-15 of 26 comments
Per page: 1530 50

Date Posted: Feb 14, 2015 @ 10:27pm
Posts: 26