Be a Supporter!

Can't pass function as argument

  • 415 Views
  • 9 Replies
New Topic Respond to this Topic
bonkanailios
bonkanailios
  • Member since: Jan. 3, 2002
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Can't pass function as argument Feb. 8th, 2012 @ 07:21 PM Reply

For some reason this works:

function tick(e:Event) {
				testMC.tick();
		}

But when testMC is Badguys[0] this doesn't work:

function tick(e:Event) {
				arrayfunction(Badguys, tick);
		}
		function arrayfunction(a1:Array, f1:Function) {
				var arraylength1 = a1.length;
				for (var i = arraylength1 - 1; i >= 0; i--) {
					trace (i)
					var a1target = a1[i];
					a1target.f1();
				}
			
		}

Thankyou for your expert time. I'm sure this is some sort of stupid mistake.

bonkanailios
bonkanailios
  • Member since: Jan. 3, 2002
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to Can't pass function as argument Feb. 8th, 2012 @ 07:24 PM Reply

I should say that I keep getting this in my output box:
ReferenceError: Error #1069: Property f1 not found on Enemies.treeElemental and there is no default value.
at Level/arrayfunction()
at Level/tick()

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Can't pass function as argument Feb. 8th, 2012 @ 07:42 PM Reply

You are passing the function as a parameter with no problem.
Your problem is here:

var a1target = a1[i];
a1target.f1();

Just as the error says, there is no "f1" property in "a1target".
You passed "f1" in as a parameter, so you call it simply by:

f1();

However, I have to ask.
What are you trying to do here:

function tick(e:Event) {
	arrayfunction(Badguys, tick);
}

I think you are misunderstanding how function references work.

Are you intentionally ignoring the event object from your event listener?
Why are you passing the callback of an event listener as a parameter to another function?

Kirk-Cocaine
Kirk-Cocaine
  • Member since: Aug. 17, 2003
  • Offline.
Forum Stats
Moderator
Level 38
Programmer
Response to Can't pass function as argument Feb. 8th, 2012 @ 07:52 PM Reply

Where is a1target defined? Also your function is also either trying to be recursive (why?), or your object (a1target) is trying to call a function that doesn't exist.

Also why are you looping backwards?

If you explain what you're trying to do you might get more help.


The water in Majorca don't taste like what it oughta.

| AS3: Main | AS2: Main | Flash Tutorials |

BBS Signature
bonkanailios
bonkanailios
  • Member since: Jan. 3, 2002
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to Can't pass function as argument Feb. 8th, 2012 @ 08:08 PM Reply

At 10 minutes ago, Diki wrote: However, I have to ask.
What are you trying to do here:

function tick(e:Event) {
arrayfunction(Badguys, tick);
}

I think you are misunderstanding how function references work.

Are you intentionally ignoring the event object from your event listener?
Why are you passing the callback of an event listener as a parameter to another function?

Firstly: THANKYOU SO MUCH for the quick response!

It's sometimes hard for me to express these things accurately as I'm quite new to all this, but as best as I can describe it:
The code above is from a Level class. TestMC is a child of the Level class.
I'm trying to run a function within TestMC named "tick" Which in turn manages all of TestMC's behavior.

This is the actual function inside TestMC:

public function tick() {
			priority();
		}

(all priority currently does is moves TestMC to the left.)

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Can't pass function as argument Feb. 8th, 2012 @ 09:24 PM Reply

If your tick() is a member function of each of your TestMC objects then why can't you just put all the code inside that function?
I don't see a need for you to be passing a function reference to another function.

Perhaps I'm misunderstanding what it is that you're trying to do.
Are you essentially just trying to create an update function for your TestMC class?

bonkanailios
bonkanailios
  • Member since: Jan. 3, 2002
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to Can't pass function as argument Feb. 8th, 2012 @ 10:35 PM Reply

At 52 minutes ago, Diki wrote: If your tick() is a member function of each of your TestMC objects then why can't you just put all the code inside that function?
I don't see a need for you to be passing a function reference to another function.

Perhaps I'm misunderstanding what it is that you're trying to do.
Are you essentially just trying to create an update function for your TestMC class?

(thanks again)
Yes, exactly. also, I wanted to make it so that I can satisfy all of my basic for-loop needs with this one function. So that I don't have a million different for-loops in the level class. In a sense, this does work for that, but it only allows me to use functions within the Level class. I just cant seem to get it to do anything to any of the Level class's children.

I did come up with a solution of looking at f1 as a string and then doing a kind of
if(f1=="tick")a[i].tick();

but I can't help but feel like this is a lazy, inefficient solution, created entirely by my misunderstanding of exactly how this stuff works.

as for my reason for not just using an Event.ENTER_FRAME listener: I had read that having many Event.ENTER_FRAME listeners can bog down a game. Again, I am very new at this so I just take what I read as gospel.

Kellexx
Kellexx
  • Member since: Feb. 22, 2011
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to Can't pass function as argument Feb. 9th, 2012 @ 12:25 AM Reply

Since I don't have or want your full code, I'll just give you this. It's everything you should ever need to do with using functions as variables and parameters. For the parameters, there is a default function, and it can pass an unlimited number of parameters along with it. You can adjust this in ways to suit your situation.

function showString(cString:String = "Default string"):void
{
	trace(cString);
}

function showSum(... nums):void
{
	var i:int;
	var cSum:Number = 0;
	
	if (String(Number(nums))=="NaN"
		&& nums.length==1)
	{
		/* nums is a 2D array, which occurs if the function was called by callFunctionWithParameters with 2 or more parameters
		   Would also occur if this happened to be called with an array as the parameter */
		for(i=0; i<nums[0].length; i++)
		{
			cSum += nums[0][i];
		}
	} else
	{
		for(i=0; i<nums.length; i++)
		{
			cSum += nums[i];
		}
	}
	
	trace(cSum);
}

function callFunctionWithParameters(cFunction:Function=null, ... args):void
{
	if (cFunction == null)
	{
		cFunction = showString;
	}
	
	if (args[0] == null)
	{
		cFunction();
	} else
	{
		cFunction(args);
	}
}

function callSimpleFunction(cFunction:Function=null):void
{
	if (cFunction == null)
	{
		cFunction = showString;
	}
	
	cFunction();
}

var tFunc:Function = showSum;

showSum(2);
showSum(2, 3);
tFunc();

trace("---")
callSimpleFunction();
callSimpleFunction(showString);
callFunctionWithParameters(showString, "nom nom");
trace("---");
callFunctionWithParameters();
callFunctionWithParameters(tFunc);
callFunctionWithParameters(tFunc, 19);
callFunctionWithParameters(tFunc, 19, 3);

That outputs the following:

2
5
0
---
Default string
Default string
nom nom
---
Default string
0
19
22

I trust you can figure it out. I can answer any specific questions about it.

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Can't pass function as argument Feb. 9th, 2012 @ 02:05 AM Reply

At 3 hours ago, bonkanailios wrote: I did come up with a solution of looking at f1 as a string and then doing a kind of
if(f1=="tick")a[i].tick();

forget about it.

as for my reason for not just using an Event.ENTER_FRAME listener: I had read that having many Event.ENTER_FRAME listeners can bog down a game. Again, I am very new at this so I just take what I read as gospel.

Nobody said that you should add more enter frame handlers. (it would be indeed bad for the performance)
But you can run the for loop directly in your tick function.

bonkanailios
bonkanailios
  • Member since: Jan. 3, 2002
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to Can't pass function as argument Feb. 9th, 2012 @ 03:55 AM Reply

At 3 hours ago, Kellexx wrote: Since I don't have or want your full code, I'll just give you this. It's everything you should ever need to do with using functions as variables and parameters. For the parameters, there is a default function, and it can pass an unlimited number of parameters along with it. You can adjust this in ways to suit your situation.
I trust you can figure it out. I can answer any specific questions about it.

HOLY RESPONSE! Thanks so much I'm going to comb through this and figure out if it's exactly what I needed.
It looks promising though.