x


how can I use function pointer/delegate in #pragma strict javascript ???

I'm using javascript write our Game in unity3d. I use variable store the function and call it somewhere such as:

var foobar;
function test() {
   ... do something;
}

function Start () {
    foobar = test;
}

Then I can call foobar as a funciton directly.

But when I add #pragma strict at the top of this script. It comes error that foobar need to be strong type. So I don't how can I fullfil the job above in #pragma strict.

Thanks in advance.

more ▼

asked Mar 02 '11 at 09:47 AM

KarasAya gravatar image

KarasAya
56 3 5 10

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Well, i didn't use JS but it should work with the type Function:

var foobar : Function;
function test() {
   ... do something;
}

function Start () {
    foobar = test;
}

In C# you would create a delegate:

delegate void FooBarFunction();

FooBarFunction foobar;
[...]
more ▼

answered Mar 02 '11 at 10:33 AM

Bunny83 gravatar image

Bunny83
45.5k 11 49 207

HI, many thanks! It is the answer. I first try to use var foobar:function but failed. And your post just remind me it should be Function.

Mar 02 '11 at 11:57 AM KarasAya
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3464
x32

asked: Mar 02 '11 at 09:47 AM

Seen: 2063 times

Last Updated: Mar 02 '11 at 09:47 AM