Passing by reference

can someone help me get my head around passing by reference in squirrel?!

I am trying to increment and store the value of an integer when a function is called, but it is not working. I have written a short piece of code to try and understand the pointer/address system in squirrel but any time I run this, I get the error: expected ‘IDENTIFIER’

Help much appreciated.

`function swapnum(local *i, local *j) {
local temp = i;
i = j;
j = temp;
return(0);
}

function main() {
local a = 10;
local b = 20;

swapnum(&a, &b);
server.log("A is "+a+"and B is "+b);
}

imp.configure(“pass by ref”, [], []);
main;`

Squirrel is like C, but it isn’t C, and your syntax with “*” and “&” plain doesn’t exist. Scalars (int, float, bool) are always passed by value, and tables, arrays, classes and suchlike are always passed by reference.

Peter

ahh ok - so to do something like this, would I have to first create an array to hold the values?

Thanks for the reply

Yes, an array or perhaps a table. Then it ought to work.

Peter

thanks very much peter,

ill give that a go and post my progress

Thanks for the help peter - that all worked fine with an array. For anyone making the same mistakes I did, here is some example code:

`//example code showing how to pass by reference
//in squirrel you can do this with arrays, tables and classes

local score = [10, 20];

function swapnum() {
local temp = score[0];
score[0] = score[1];
score[1] = temp;
return(0);
}

function main() {
server.log("A is “+score[0]+” and B is "+score[1]);
swapnum();
server.log("A is now “+score[0]+” and B is now "+score[1]);
imp.wakeup(1, swapnum);
}

imp.configure(“pass by ref”, [], []);
main();
`