0

I have encountered an AS3 function that is declared as a null variable, as in:

public var edgeWeights:Function = null;

I am not sure how to use this function to change null to another value (e.g., a number like 2 or 3). I thought something like cs.edgeWeights = 2 might work, but that creates a compile error, as does cs.edgeWeights(2);

I believe these are anonymous functions in AS3 and I did do some research on them, but could not find a resolution to this situation.

2
  • Could you post the code you have written thus far? Also why are you trying to declare a Function variable, initialize it to null, and then try to set it equal to an int? Commented Nov 20, 2013 at 15:58
  • A Function cannot be set to a number value. It can return a number value, but without seeing how edgeWeights is utilized in the code, not sure how anyone can help you. Commented Nov 20, 2013 at 19:17

3 Answers 3

3

public var edgeWeights:Function = null;

This notation means declaring variable edgeWeights of type Function. In Actionscript Function is an object and can be set to null.

To use it you need to set this variable to some function. For example: edgeWeights = function(a:int,b:int):int { return a+b } or edgeWeights = Math.sin.

What function you should set there depends on your particular case.

Sign up to request clarification or add additional context in comments.

3 Comments

Hmm, thanks - I am still not getting the result I am looking for. Just to add a bit of clarity, I cannot change the "public var edgeWeights:Function = null;" statement - that is given to me by the as3 class I am calling. So the task is how to turn that 'null' into a '3' in the program I am writing. Thanks!
Sounds like you don't understand how to use the class. Your perceived task is likely a step in the wrong direction ? This answer explained a concept that you need to understand if you want to use this class you speak of.
@tomish public var edgeWeights:Function = function():Number {return 3; }
0

If you assume that the Class that declares edgeWeights is Widget:

protected var widget:Widget;

protected function createWidget():void {
    widget = new Widget();
    widget.edgeWeights = widgetCallback;
}

//signature would need to match what the Widget
//actually expects this callback to do
protected function widgetCallback():void {
    trace('hi from widget callback');
}

Note that it's probably bad practice to have a public callback variable and not provide a default implementation, so if you have access to the source code, you should probably fix that.

1 Comment

Hi Amy, thanks for this. I am puzzled why this edgeWeights function was implemented the way it was in the source code. Seems awkward. I'd rather try to work around it instead of recompiling the source but I may end up having to do that.
0

Given any function:

public function someFunction()
{
    ...
}

You can create a "pointer" with this: this.edgeWeights = someFunction; (yes, without ())

Later you just use: this.edgeWeights(); and you'll be calling someFunction().

1 Comment

Thanks, this appears to work - at least it no longer crashes the program: function myFunction() { } cs.edgeWeights = myFunction;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.