;

PDA

Click to See Complete Forum and Search --> : Can you set a variables in a functions parent function (doable in C)?


Pincus
02-09-2008, 11:05 PM
Hey,
I am not down with AS3. I know AS2. So I am wondering if it is possible to do something in AS3 which I cannot do in AS2, but it is possible in C.
I am wanting to basically do this:

function fu1():Void {
var myNum:Number = 32;
fu2();
trace(myNum);
}

function fu2():Void {
myNum = 64; // I want to change the myNum variable in fu1...
}

fu1(); // Output is 32, but I want to find a way for it to output 64.


And in C:

#include <iostream.h>

void doStuff(int *abc) {
*abc = 200;
}


int main() {
int x = 10;
doStuff(&x);
printf("%i\n", x); // Outputs 200, not 10.
system("pause");
return 0;
}


Hopefully you understand what I am asking.
Thanks in advance!

MyFriendIsATaco
02-09-2008, 11:19 PM
var myNum:Number;

function fu1():Void {
myNum = 32;
fu2();
trace(myNum);
}

function fu2():Void {
myNum = 64; // I want to change the myNum variable in fu1...
}

fu1(); // Output is 32, but I want to find a way for it to output 64.

Pincus
02-09-2008, 11:24 PM
That just creates another variable on the timeline (or whatever), see...

Variable _level0.myNum = 64


I want to set the variable in the fu1 function (the one which calls the fu2 function), and not another one.

Thanks though! :)

MyFriendIsATaco
02-10-2008, 01:55 AM
I see, you're passing the variable as a reference into the function. Kinda like how PHP can do it.

From what I know, there's no direct equivalent to that. I would do something like this:function doStuff($abc:int):int
{
$abc = 200;
return $abc;
}

function main():void
{
var x:int = 10;
x = doStuff(x);
trace(x);
}Other than that, there really is no other way besides assigning a class level variable.

5TonsOfFlax
02-10-2008, 03:28 AM
There is no direct correlation, but you can box and unbox your variable by putting it in an array.

function fu1():Void {
var box:Array = [32];
fu2(box);
trace(box[0]);
}

function fu2(input:Array):Void {
input[0] = 64; // changes box contents from fu1
}

fu1(); // Output should be 64


Basically, you're creating an object which you can pass by reference and change the properties/values in that object.

Pincus
02-10-2008, 05:04 AM
Okay thanks guy. The only problem with returning the value is that I am already using it. I could return an array though.

Funny how passing in an object acts differently than other datatypes, like Number/int. I think I will do it that way.

Many thanks to both of you! :)

5TonsOfFlax
02-10-2008, 03:01 PM
Passing an object doesn't really act differently than passing a primitive type. It's just that you no longer have to change the actual value of the parameter, you get to change it's properties. To show that it's not different, the following code should NOT alter the original value:

function fu1():Void {
var box:Array = [32];
fu2(box);
trace(box[0]);
}

function fu2(input:Array):Void {
input = [64]; // does NOT change box contents from fu1
}

fu1(); // Output should be 32

Pincus
02-19-2008, 02:31 AM
Cool. I wasn't able to do it the way I wanted to do, but I learnt something new. Thanks very much guys! :)