GameMonkey Script

GameMonkey Script Forums
It is currently Wed Jan 23, 2019 1:59 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Thu Jan 19, 2012 3:26 pm 
Offline

Joined: Wed Jan 11, 2012 10:10 pm
Posts: 3
Sometimes I want to grab more parameters than the ones I defined.
In Lua I would just add an ellipsis:
Code:
function test( hi, there, ... )
   for k,v in pairs( arg ) do
      print( v )
   end
end

and the rest of the parameters would be stored in "arg".

Is it possible to have this feature in GMS as well?
I've been trying to make it so it sets a local variable, "arg" to a table containing all the parameters but so far I'm failing.


Top
 Profile  
Reply with quote  
PostPosted: Sun Jan 22, 2012 10:52 am 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 708
Not exactly. If I throw my mind back a long way, I think the debate on this topic while designing the language ended up with the thought that simply passing a table was just as logical as having some kind of variable argument syntax.
So there are 2 work arounds...

1) Explicitly declare lots of parameters, the maximum you would ever need, then use as few as you need eg.
Code:
function myFunc(parm0, arg1, arg2, arg3, arg4, arg5, arg6)
{
  print("parm0", parm0);
  print("value1", arg1);
  print("value2", arg2);
  print("value3", arg3);
  print("value4", arg4);
  print("value5", arg5);
  print("value6", arg6);
}

myFunc("vargs", 1,2,"apple", "orange" );

/*
output:
parm0 vargs
value1 1
value2 2
value3 apple
value4 orange
value5 null
value6 null
*/


2) Just use table syntax when passing multiple arguments eg.
Code:
function myFunc(parm0, vargs)
{
  print("parm0", parm0);
  count = 0;
  foreach( value in vargs )
  {
    count += 1;
    print("value",count, value);
  }
}

myFunc("vargs", {1,2,"apple", "orange"} );

/*
output:
parm0 vargs
value 1 1
value 2 2
value 3 apple
value 4 orange
*/

From the native code side, you can handle variable args more flexibly because the bound function can query the number of parameters pushed to the stack and access them.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group