Hi fellows, I have one script which must communicate with many others, to put this in other perspective: I have an instance of A(“a”) which must communicate with B(“b”), C(“c”), D(“d”), and more.
“b” will call an “a” method, I do that easily, I reference “a” from “b” from the Inspector. Now the problem arise when “a” must call a “b” method. I need to have a variable defined in “a” which will receive as value “b”, so then I can do b.methodOne. Being that variable one of dynamic type I can’t initialize it, for example I can’t do:
var receiver : ClassB;
…because “receiver” will not be always of ClassB, can be of ClassC, ClassD, so HOW CAN I INITIALIZE THAT VARIABLE SO CAN ACCEPT DYNAMIC TYPES? I’ve seen that in C# you can use the keyword “dynamic”, but I didn’t see that functionality in UnityScript. This is a very simple question and I think that most of us have rolled with it, also is very easy to make some implementtation in any other language, I’m not new in programming, yes to UnityScript. Until now UnityScript made me most of my life headaches. There’s so few documentation for programmers, the API is so poor documented. I appreciate Unity(specially for the multiplatform feature) but the programming part…can be a lot better. I’ll thank your help.
If you think you need the dynamic keyword - which is not available in .NET 3.5 and under - is that you have no idea what the dynamic keyword actually means.
And why you cannot do “var receiver : ClassB”?
Sounds to me you need bidirectional communication. Ever heard of event?
“b” have a reference to “a” because of the inspector, there I drag “a”, thats awesome. Now “b” call a method on “a”, awesome! “a”, after some process and logic will call a method on “b”. “b” have those methods defined, that’s awesome! All could work really good, but ey! it’s UnityScript!(sorry I can’t hide my hate): I need to define which type of variable will get the value which can occupy “b”, or perhaps an instance of C, or D, and so on. The type is DYNAMIC, IT VARIES. So how can I initialize a dynamic variable in UnityScript? This: “var source : ClassB;” will not work for me, that must be dynamic.
The type is not dynamic. Let me be clearer; you have no clue what a dynamic type means, so drop it.
Now, if multiple type has a method named the same way, you could have a base class or an interface.
I come from Ruby and JavaScript and I know what dynamic is. What I mean is that the VARIABLE will have DYNAMIC TYPE VALUES, I don’t know how to explain my self better, it’s very clear my issue. I’ll put you the same issue I have, by another guy, the fact is that he’s programming in C#. This is exactly what’s happening to me: Dynamic type for accessing "script objects/components..." - Unity Engine - Unity Discussions .
A Base Class can cut the cake but is that really needed? Is there another way to go around? I feel that I’m putting bandaids to my app.
And the guy in that URL also had no clue what he truly wanted in regards to a strict language like C#. You come from Ruby/JavaScript, two language who are - if I remember well - not type-enforced and are both interpreted. C#/UnityScript/Boo are not like that, they are strict and are byte-coded, so give it up. People live in C++/C# environment for years without any need for dynamic types or dynamic values, so start thinking differently.
Using dynamic would be the bandaids, which you cannot use because it doesn’t exist in .NET 3.5. Having an interface or a base class is the real good code design.
You do know what an interface is, right?
I know what you mean by base class, I can make those B, C, D inherit from a base class. That base class must have the methods defined, they can do nothing, but must be defined. Then in each subclass(B, C, D) I define the same methods as the base class. Meaby that can work, I don’t know but I feel that yes. I have to do in A: “var source : SomeBaseClass;”, and that might work. This is a kind of polymorphism.
By Interface I don’t know exactly what you mean, since has different meaning in different languages.
I’m talking about C# interface - which also exist in UnityScript.
They are method contract between unrelated classes. They are a huge part of the polymorphism in C#. Usually you would make a base class when your classes share a logical relationship. When they do not, but they do share similar methods or functionality, they can implement an interface.
Let’s say I have a door, a car and a player. They have nothing in common and would probably not share any kind of hierarchical relationship.
However, if I throw a grenade and I want to damage everything in its radius, the door, car and player can all have a method named “ReceiveDamage”, so they could implement a common interface. The grenade would not need to know that it’s a door or a car, only that it’s a “IDamageable” interface, so that it has a method named “ReceiveDamage”.
Example;
public interface IDamageable
{
void ReceiveDamage(Vector3 source, float damage);
}
And an implementation;
public class Player : MonoBehaviour, IDamageable
{
public void ReceiveDamage(Vector3 source, float damage)
{
// do stuff related to the player receiving damage
}
}
Okay, nice. I’m confused about how to implement this in UnityScript. Since the class comes already opened(there’s no “public class Player : MonoBehaviour”), how do I do to include that behaviour in the class? is there any documentation about this? I see no “interface” keyword in here => http://wiki.unity3d.com/index.php?title=UnityScript_Keywords
UnityScript example of base class (extends) and interface implementation (implements)
public class AIExampleJS_Running extends MonoBehaviour implements IInspectorRunning { }
Interface declaration;
interface IInspectorRunning
{
function myFunction();
}
Then I initialize the variable in A like this:
var source : IInspectorRunning;
right?
Yes. You can now put any object in that variable that implement that interface.
Thank you very much LightStriker
, I’ll get around this.
I should take a book of some of these languages, have this “secrets” stuff for me :S . Can you be kind and advice me which of this languages should I master? Which do you recommend me?
I could only recommend C#, because both UnityScript and Boo lacks the documentation and support C# has. Just the MSDN has a huge documentation about C# and the .NET framework.
Also, US and Boo exist only in Unity, while C# allow you to write application, library and pretty much anything C++ can. I’m a visual studio user, and it works great with C#.
On top C# got a few features US/Boo lacks. Sure, you can build a full game in US/Boo too, so it often boils down to personal preference.
I should start to learn C#. Thanks LightStriker.