lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Ok, I think I have the varargs/tabke thing working -but- how do I do the

Say I have a table t = {6,7,8} and this is what I want to pass to a
function. This func will pass t on to another func and so on (ie a number of

How do I get one of the funcs to modify its passed-in vararg tables so that
it sets arg[2] from (what was) 7 to (say) 9. Having done this when it passes
the arg table onto another func - the called func should get a table with
{ 6,9,8 } as a param.

In C/C++ Speak (which might be easier to explain) I have an array or

struct MyInfo
	int x;
	int y;
	int z;
		x = 6;
		y = 7;
		z = 8;

void MyFunc( void* anyArg )
	MyInfo* pArg = (MyInfo*)anyArg;

	if( pArg->y == 7 )
		printf("change arg\n');
		pArg->y = 9;
		return MyFunc( anyArg );  // <== Pass on arg with its changed value
						  // syntax for how to accomplish this in lua im not sure about
						  // also syntax for passing varargs between chains of calling
						  // funcs.
		printf("arg already changed\n');
	return 1;

use like:

MyInfo info;
MyFunc( (void*)(&info) );

All help appreciated.


-----Original Message-----
From: Andy Bushnell []
Sent: Thursday, October 09, 2003 10:17 PM
Subject: Tables, Functions & varargs


Another (newbie) question here (and I did RTFM first - but it isnt sinking
in yet!:-)


I have a function to print the name of a node (a tolua'd data structure)

function printName( node, ... )

	print( node:getName() )


I want to be able to call printName with some optional arguments. In my
print example I want to call it with an "indent level" number which the
function can key-off & print appropriate tabs before writing the name as

Another function is supposed to get passed the exact same argument &
increment/decrement the value which will be used in printName.

so at the top level I have code like:

level = 1
printAll(root,level)  where printAll is defined as printAll(node,...)

I believe my level number value is put into a table & passed to varargs
functions as a table. Hence in my vararg functions Ive tried...

	indent_level = arg[1]
	indent_level = unpack(arg) // not sure what/when or why i'd use the unpack
	indent_level = tonumber(arg[1])

	my intention being that I could then...

	arg[1] = indent_level + 1 --or minus 1

when I try to use any of my indent_level assignments above I get an error in
the for loop (to print the tabs) about indent_level not being a number!!!

Im a bit confused and I couldnt find an example which seems to do what i'm
doing - so any help appreciated!:-)



Lua is very cool though!