Jump to content


Photo

script arguments


  • Please log in to reply
10 replies to this topic

#1 Mcmunchly

Mcmunchly

    GMC Member

  • New Member
  • 113 posts

Posted 15 January 2011 - 12:34 AM

how do I use arguments in a script?

So let say I use

script_execute(thescript, firstvar, secondvar);

how do I identify and use firstvar and secondvar in the script? Is there a special 'argument' keyword or something?
  • 0

#2 frankpiet

frankpiet

    ^destroyed evil chicken^

  • New Member
  • 760 posts

Posted 15 January 2011 - 12:39 AM

firstvar will be called argument0 in the script
secondvar will be called argument1 in the script

Remember you can call it as:

thescript(firstvar,secondvar)

It does the exact same thing

If you want the script to do something, use the return statment in the script. This stops the execution of the script.

For example:

WHERE YOU CALL THE SCRIPT
number=add(5,9)

THE SCRIPT CALLED "add"
return argument0+argument1;
  • 0

#3 FoxInABox

FoxInABox

    GMC Member

  • GMC Member
  • 5908 posts
  • Version:GM:Studio

Posted 15 January 2011 - 12:39 AM

for example let it sum together all the arguments?
value = scriptname(arg0,arg1, ... ,arg15)

would be like this:
return argument0 + argument1 + ... + argument15;

or as an array
var sum,i;
sum=0;
for(i=0;i<16;i+=1) sum+=argument[i];
return sum;

  • 0

#4 Mcmunchly

Mcmunchly

    GMC Member

  • New Member
  • 113 posts

Posted 15 January 2011 - 12:51 AM

can you pass objects as arguments?

not just the value but the actual object so I can change it's values?

Edited by Mcmunchly, 15 January 2011 - 12:55 AM.

  • 0

#5 GameGeisha

GameGeisha

    GameGeisha

  • GMC Member
  • 4502 posts
  • Version:GM:Studio

Posted 15 January 2011 - 01:18 AM

can you pass objects as arguments?

not just the value but the actual object so I can change it's values?

Yes, you can pass object instance IDs as arguments. Then you can change variables that are local to them. Here's an example of how this can be applied:

Script: damage
/* damage(inst, amt) */
argument0.hp -= argument1;

Collision event of obj_bullet (with obj_enemy):
damage(other.id, 5);
instance_destroy();

GameGeisha
  • 0

#6 Mcmunchly

Mcmunchly

    GMC Member

  • New Member
  • 113 posts

Posted 15 January 2011 - 02:35 AM

can I rename the arguments?

can I go something like:
attacker = argument0;
attacker.hp -= 12;

I tried doing this but it didn't seem to work.

Edited by Mcmunchly, 15 January 2011 - 02:36 AM.

  • 0

#7 frankpiet

frankpiet

    ^destroyed evil chicken^

  • New Member
  • 760 posts

Posted 15 January 2011 - 03:46 AM

make sure you delclare it as a variable.


var attacker;
attacker=argument0;
attacker.hp-=12;



Also note that local variables are still avaliable. So you could easily use.change x and y in your script and it would change the object.

For more information look in the manual (look at Game Maker Language > GML Language overview > extra variables AND scripts)

Edited by frankpiet, 15 January 2011 - 03:48 AM.

  • 0

#8 idenii

idenii

    GMC Member

  • GMC Member
  • 23 posts
  • Version:GM8.1

Posted 03 January 2014 - 05:02 PM

is there a loop that allows me to know exactly how many arguments were passed to a script, like:

var args;

for(args=0 args<16 args+=1)
{
    if (is_real(argument[args]))
    {
        break;
    }
}
return args;

but instead of is_real, i need the right function, the one that can know whetever the argument is void. in game maker studio, there is argument_count, but for gm80, i need a equivalent function.


  • 0

#9 TheouAegis

TheouAegis

    GMC Member

  • GMC Member
  • 10412 posts
  • Version:GM8

Posted 03 January 2014 - 06:31 PM

I think in Studio, but don't think there is in legacy versions. In legacy versions, all undefined arguments are set to 0.


  • 0

#10 GameGeisha

GameGeisha

    GameGeisha

  • GMC Member
  • 4502 posts
  • Version:GM:Studio

Posted 03 January 2014 - 06:38 PM

With GM8.0 and earlier, there is no reliable way to detect how many arguments are passed in. Unused arguments are set to 0, and that makes them indistinguishable from used arguments that have 0 passed into them. Finding the last non-zero argument is the best you can do.

 

That's why argument_count was introduced in GM8.1.

 

GameGeisha


  • 0

#11 idenii

idenii

    GMC Member

  • GMC Member
  • 23 posts
  • Version:GM8.1

Posted 03 January 2014 - 07:35 PM

well i'm thinking about a loop but i still can't finish it. i'll just post a lnk the the finished script that will return the argument count in 30-45 minutes with some others i made

 

edit: no link posted, just the raw code

var i, args_number, max_args;

/* add here the maximum number of arguments your script 
 * can handle (set it into the variable max_args). For
 * my example, i'll use at most 16 arguments (max index: argument[15])
 */
max_args=16


for(i=0 i<max_args i+=1)
{
    if(argument[i]=0)
    {
        args_number=i
    }
}

Edited by idenii, 03 January 2014 - 10:31 PM.

  • 0




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users