A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Can you set a variables in a functions parent function (doable in C)?

  1. #1
    Member
    Join Date
    Feb 2007
    Location
    Sydney, Australia
    Posts
    57

    Can you set a variables in a functions parent function (doable in C)?

    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:
    PHP Code:
    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:
    PHP Code:
    #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!

  2. #2
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    PHP Code:
    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. 

  3. #3
    Member
    Join Date
    Feb 2007
    Location
    Sydney, Australia
    Posts
    57
    That just creates another variable on the timeline (or whatever), see...
    PHP Code:
    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!

  4. #4
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    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:
    Code:
    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.

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    There is no direct correlation, but you can box and unbox your variable by putting it in an array.
    Code:
    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.

  6. #6
    Member
    Join Date
    Feb 2007
    Location
    Sydney, Australia
    Posts
    57
    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!

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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:
    Code:
    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

  8. #8
    Member
    Join Date
    Feb 2007
    Location
    Sydney, Australia
    Posts
    57
    Cool. I wasn't able to do it the way I wanted to do, but I learnt something new. Thanks very much guys!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center