- Home /
Javascript / Unityscript 'with' keyword
So I'm trying to take advantage of Unity3's new JS feature, function types. My code is basically trying to do something like this:
class MyClass {
var myVar:int;
var func: function();
}
what I want to do is be able to change myVar
from inside whichever function I assign to func
. I need it to act just like it would when using this.member
from within a method, because my class has multiple such members and multiple such methods (function members, really).
I found this link which in the real JavaScript would likely work for this purpose.
But it doesn't work here though, the compiler keeps expecting colons and semicolons. Either the with
keyword works differently here or it just doesn't really work yet. I think I can technically do this in C# with using
, but I figure now that we have more JS functionality I would give it a try.
Hope I'm being clear enough in what I'm trying to do. I'll revise this if more info is needed.
"using" in C# only imports namespaces. The equivalent in JS is "import", but neither have anything to do with this question.
Good point. I don't script in C# very often, yet. $$anonymous$$ost of my experience is from C++ and it has been a while. I'm used to being able to declare methods inside a class and only later define them with the :: operator, and I was hoping I could use anonymous functions to do something like that on a per-object (vs per class) basis. But I think I've got another way that will get this done.
Answer by TheDemiurge · Jan 02, 2011 at 06:21 AM
Sigh. nice response typed up and IE got all screwed up. Here goes: answering question now that I can and don't remember why I needed this in the first place, for the sake of it not being unanswered anymore.
Using C# instead of JS.
public delegate int MyDelegate(); public class MyClass { int m_myVar; public MyDelegate Del;
public void SetMyVar() { if (Del != null) m_myVar = Del(); else m_myVar = 0; }
}
// in another script: MyClass mc = new MyClass(); mc.Del += new MyDelegate(DoSomething); mc.SetMyVar();
public int DoSomething() { return 5; }