Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > New Scripting Engine (GS2)
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 02-06-2009, 01:11 AM
Loriel Loriel is offline
Somewhat rusty
Loriel's Avatar
Join Date: Mar 2001
Posts: 5,059
Loriel is a name known to allLoriel is a name known to allLoriel is a name known to allLoriel is a name known to all
Variable argument count?

How do I pass all or some of my however many arguments to another function taking a variable amount of arguments?

As in lua's function foo(x, y, ...) something_with(x, y) bar(...) end, I guess.
Reply With Quote
  #2  
Old 02-06-2009, 01:14 AM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
Uhhh...could probably use the params[] array.
Reply With Quote
  #3  
Old 02-06-2009, 01:24 AM
Loriel Loriel is offline
Somewhat rusty
Loriel's Avatar
Join Date: Mar 2001
Posts: 5,059
Loriel is a name known to allLoriel is a name known to allLoriel is a name known to allLoriel is a name known to all
Quote:
Originally Posted by Crow View Post
Uhhh...could probably use the params[] array.
How do I use the params array?
Reply With Quote
  #4  
Old 02-06-2009, 01:57 AM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
Quote:
Originally Posted by Loriel View Post
How do I use the params array?
Well, it's an array that stores the arguments. Example:

PHP Code:
function myFunc(arg1arg2arg3) {
  echo(
params[0]);
  echo(
params[1]);
  echo(
params[2]);

Would echo arg1, arg2 and arg3 in that order. So you could probably check the size of that array, do some magic, and then pass the arguments to another function.
Reply With Quote
  #5  
Old 02-06-2009, 02:11 AM
Loriel Loriel is offline
Somewhat rusty
Loriel's Avatar
Join Date: Mar 2001
Posts: 5,059
Loriel is a name known to allLoriel is a name known to allLoriel is a name known to allLoriel is a name known to all
Quote:
Originally Posted by Crow View Post
Would echo arg1, arg2 and arg3 in that order. So you could probably check the size of that array, do some magic, and then pass the arguments to another function.
Could you elaborate on the magic?

Edit: Uh, how do I even call that function to make params[0] actually hold a value?
Reply With Quote
  #6  
Old 02-06-2009, 02:13 AM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
Quote:
Originally Posted by Loriel View Post
Could you elaborate on the magic?
No idea what one could do in this case. Though, since this is still Graal, using that array is probably the only way to go. I don't have an idea how exactly you could do this though.
Reply With Quote
  #7  
Old 02-06-2009, 02:38 AM
Programmer Programmer is offline
Coder
Programmer's Avatar
Join Date: Jan 2008
Location: -78.464422, 106.837328
Posts: 449
Programmer has a spectacular aura aboutProgrammer has a spectacular aura about
Send a message via AIM to Programmer Send a message via MSN to Programmer Send a message via Yahoo to Programmer
Quote:
Originally Posted by Loriel View Post
Could you elaborate on the magic?

Edit: Uh, how do I even call that function to make params[0] actually hold a value?
params[] is set to the total sum of the variables passed into a function. Consider the following:

PHP Code:
function Foo(barbaz)
{ ... }

Foo(12true999); 
bar would equal 1, and baz would equal 2. However, the params array looks like this:

PHP Code:
{12true999
And thus, the above function can literally be transcribed into:

PHP Code:
function Foo()
{
    
bar params[0];
    
baz params[1];

This is not dissimilar to C#'s implementation of parameters, if you're into that:
PHP Code:
// C#
private void Foo(params object[] parameters)
{
     var 
bar parameters[0];
     var 
baz parameters[1];

__________________
- Iᴀɴ Zɪᴍᴍᴇʀᴍᴀɴ
Reply With Quote
  #8  
Old 02-06-2009, 02:45 AM
Loriel Loriel is offline
Somewhat rusty
Loriel's Avatar
Join Date: Mar 2001
Posts: 5,059
Loriel is a name known to allLoriel is a name known to allLoriel is a name known to allLoriel is a name known to all
Quote:
Originally Posted by Programmer View Post
params[] is set to the total sum of the variables passed into a function. Consider the following:

PHP Code:
function Foo(barbaz)
{ ... }

Foo(12true999); 
I cannot quite get this to work. I inserted echoo({bar, baz}); into the function body for testing. Indeed it outputs 1,2 as I expected.

Quote:
And thus, the above function can literally be transcribed into:

PHP Code:
function Foo()
{
    
bar params[0];
    
baz params[1];

Now when I do this, the output is "",0. If I say echo(params), I do not get any output at all. Am I missing something here?

Quote:
This is not dissimilar to C#'s implementation of parameters, if you're into that:
PHP Code:
// C#
private void Foo(params object[] parameters)
{
     var 
bar parameters[0];
     var 
baz parameters[1];

I think it is indeed dissimilar, because I could call that function with a single array instead of a list of parameters.
Reply With Quote
  #9  
Old 02-06-2009, 04:04 AM
Programmer Programmer is offline
Coder
Programmer's Avatar
Join Date: Jan 2008
Location: -78.464422, 106.837328
Posts: 449
Programmer has a spectacular aura aboutProgrammer has a spectacular aura about
Send a message via AIM to Programmer Send a message via MSN to Programmer Send a message via Yahoo to Programmer
Quote:
Originally Posted by Loriel View Post
I cannot quite get this to work. I inserted echoo({bar, baz}); into the function body for testing. Indeed it outputs 1,2 as I expected.
Try:

PHP Code:
function Foo(barbaz)
{
    echo({ 
barbaz });
    echo(
params);
}

Foo(12true999); 
Will echo:
HTML Code:
1, 2
-- Comment: 'true' is represented as 1.
1, 2, 1, 999
Quote:
Originally Posted by Loriel View Post
Now when I do this, the output is "",0. If I say echo(params), I do not get any output at all. Am I missing something here?
0 in Graal is literally translated as 'null', and will print absolutely nothing to the RC. An empty string literal will obviously print nothing as well.

Quote:
Originally Posted by Loriel View Post
I think it is indeed dissimilar, because I could call that function with a single array instead of a list of parameters.
In C#, it's similar in this manner:
PHP Code:
// C#
private void Foo(params object[] parameters)
{
     var 
bar parameters[0];
     var 
baz parameters[1];
     
     
Console.WriteLine("Result: " bar ", " baz);
}

Foo(12);

// OUTPUT:
// Result: 1, 2 
__________________
- Iᴀɴ Zɪᴍᴍᴇʀᴍᴀɴ
Reply With Quote
  #10  
Old 02-06-2009, 04:42 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
I've had some problems where the params[] array is erased/non-existent if you assign variables to the parameters in the function.
__________________
Reply With Quote
  #11  
Old 02-06-2009, 04:52 PM
Admins Admins is offline
Graal Administration
Join Date: Jan 2000
Location: Admins
Posts: 11,693
Admins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud of
The params[] array is only created when the script is called (doesn't even need to be a function, GS1 style). That is either on an event, or when you call a function of another object. To get a variable number of parameters either use func(arg1, arg2, arg3, arg4 etc) and ignore the arguments that you don't want, or pass the parameters as array.

Currently accessing the parameters for each function as array is not possible because it would mean a big speed slowdown, although if it's really important to have it then we could add something like temp.functionparams[] which dynamically converts the function parameters to variables.
Reply With Quote
  #12  
Old 02-06-2009, 05:36 PM
Loriel Loriel is offline
Somewhat rusty
Loriel's Avatar
Join Date: Mar 2001
Posts: 5,059
Loriel is a name known to allLoriel is a name known to allLoriel is a name known to allLoriel is a name known to all
Quote:
Originally Posted by Stefan View Post
The params[] array is only created when the script is called (doesn't even need to be a function, GS1 style). That is either on an event, or when you call a function of another object. To get a variable number of parameters either use func(arg1, arg2, arg3, arg4 etc) and ignore the arguments that you don't want, or pass the parameters as array.
Thanks, that clears it up. Some experimentation suggested the FAQ thread was inaccurate about this and some dude on IRC said it only worked for triggers, so that makes sense.

Quote:
Currently accessing the parameters for each function as array is not possible because it would mean a big speed slowdown, although if it's really important to have it then we could add something like temp.functionparams[] which dynamically converts the function parameters to variables.
Not important, I really just wanted to build an error reporting function that would forward all its arguments to format() and print the resulting string with more error stuff, instead of having the user call format, and consequently got confused about how variadic functions even work.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 06:23 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.