Squirrel: pitfalls & how2s

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

Post Reply
griffin71
Traffic Manager
Traffic Manager
Posts: 142
Joined: 31 Mar 2007 13:11
Location: Amsterdam

Squirrel: pitfalls & how2s

Post by griffin71 »

I wonder if any of us programmers knows if or how it is possible to pass a variable argument list as an argument. The SQ ref. man. sais nothing explicit about that. It does say, however, that

Code: Select all

Myfunc1(arg1, arg2, ...) {
    Myfunc2(arg1, vargv)
    .
    .
    .
}

MyFunc2(arg1, ...) {
    .
    .
    .
}
won't work.

Any suggestions?

Whenever you make the variable arguments explicit, things do work, as in

Code: Select all

Myfunc1(arg1, arg2, ...) {
    Myfunc2(arg1, vargv[0])
    .
    .
    .
}
I suppose this does work, because vargv[0] copies the first vararg, which then no longer is a pseudo argument.
A game worth playing is a game worth modding :-)
DaleStan
TTDPatch Developer
TTDPatch Developer
Posts: 10285
Joined: 18 Feb 2004 03:06
Contact:

Post by DaleStan »

Does squirrel provide a va_list[0] type? If so, pass it as a single argument. If not, you'll have to muster up some other trick, which probably won't be the easiest thing in the world. For hints as to the presence of this type, look for equivalents to the vprintf class[1] of functions:

Code: Select all

printf(const char*, ...);
vprintf(const char*, va_list);
[0] (not an array index)
Akin to the C:

Code: Select all

void Func2(va_list ap) {
 //...
}

void Func1(int arg1, ...) {
  va_list ap;
  va_start(arg1, ap);
  Func2(ap);
  //...
}
[1] vprintf, vsprintf, vfprintf, vscanf, vsscanf, and vfscanf are the six in C that come to mind.
To get a good answer, ask a Smart Question. Similarly, if you want a bug fixed, write a Useful Bug Report. No TTDPatch crashlog? Then follow directions.
Projects: NFORenum (download) | PlaneSet (Website) | grfcodec (download) | grfdebug.log parser
User avatar
benc
Engineer
Engineer
Posts: 62
Joined: 30 Apr 2007 01:57

Post by benc »

AFAIK, there is no syntax in Squirrel to easily pass vargv from one function to the next. I'm no Squirrel guru though; you might try your question/feature request at http://squirrel-lang.org/forums/

Ugly, stupid hacks are always possible, though:

Code: Select all

function call(f, v)
{
  switch (v.len()) {
    case 0: return f();
    case 1: return f(v[0]);
    case 2: return f(v[0], v[1]);
    case 3: return f(v[0], v[1], v[2]);
    case 4: return f(v[0], v[1], v[2], v[3]);
    case 5: return f(v[0], v[1], v[2], v[3], v[4]);
    case 6: return f(v[0], v[1], v[2], v[3], v[4], v[5]);
    case 7: return f(v[0], v[1], v[2], v[3], v[4], v[5], v[6]);
    case 8: return f(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
    case 9: return f(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8]);
  }
  throw "too many arguments";
}

function sum(...)
{
  local total = 0;
  for (local i = 0; i < vargc; i++)
    total += vargv[i];
  return total;
}

function my_sum(dummy, ...)
{
  local v = [];
  for (local i = 0; i < vargc; i++)
    v.append(vargv[i]);
  return call(sum, v);
}

print(my_sum("blah", 1, 2, 3, 4));
// output: 10
TheJosh
Engineer
Engineer
Posts: 75
Joined: 17 Apr 2007 12:19
Contact:

Post by TheJosh »

just make the function take a single argument: an array

then your 'arguments' can be whatever length you want
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: Ahrefs [Bot] and 7 guests