c#

I am decoupling AI, character control and character animation into 3 types of scripts
I want AI to provide character control a vector and to character animation certain variable values,
character control to provide character animation with a speed and direction etc…
There will be many AI scripts and there are already multiple character animation scripts, I want them inter-operational.
I wanted to do
public notype aiscript;
but since c# is strongly typed, it’s not possible and I can’t expose a no type generic bucket in the inspector to drag and drop my scripts (which class I don’t know yet).
so how can I do this ?

(there will be a lot of chatter between these scripts and lots of critters which will chat between them so I’d like to avoid sendmessage() if possible.)

thanks!

does “public MonoBehaviour aiscript” work for you?

[edit]

Thanks I figured out class inheritance so scripts of same category would all have the same input/output.

It’s easy to manage in VS but I’m wondering if it’s overkill now :slight_smile:

You could have a script that is always on all your objects that stores whatever variables you need to pass between them. Each AI script would save its vector to this script, and each character controller would read its vector from it.

The trick would be to making sure your scripts get run in the right order each frame.

I don’t think inheritance is overkill - it’s just good software-design :wink: … if done right, of course :wink:

You would naturally have a superclass for the AI scripts in which you have everything that is common to all your AI scripts, and then you would have specialized classes for AI. For instance, you could have “AIBase” (as the superclass, or base class), and then DumbEnemyAI, CoolEnemyAI, FriendAI etc. You even may make use of multiple levels of inheritance (some classes sharing code that’s specific to those classes).

Another thing you might consider is the use of interfaces. If you have classes that don’t share any code or any member variables, you might provide the signature of one or several methods in an Interface that multiple classes implement. The nice thing about interfaces is that one class can implement multiple interfaces (which is quite often very useful). Interfaces can also sometimes be useful for “tagging” classes, but I don’t have any good examples for this at hand, at the moment :wink:

Jashan

Have you tried multiple inheritence?

I saw on the c# doc that you can have somethind like

class AI_AggressiveAmphibian: AI_PathFinderGround, AI_PathFinderUnderwater, AI_SeekAndBite {}

Although from what I understand it doesn’t allow same method from two parents (can’t tell which is which), but I think you can go around this with some namespace (I know unity doesn’t allow full namespacing) so you could have AI_PathFinderGround.FindPath() and AI_PathFinderUnderwater.FindPath().

Nope, multiple inheritance does not work in C#. What you’ve seen is probably one class inheriting from another class, and implementing several interfaces. In Java, this is clearly distinguishable via the keywords “extends” and “implements” - in C#, unfortunately, it is not. Since the language doesn’t do it, I would recommend using a naming convention to be able to easily distinguish between classes and interfaces, like it’s done in the whole .NET API. Simply prefix interfaces with an I, like IEnumerable :wink:

I just googled a little info on multiple inheritance in c#: read this… I think this explains it nicely…

Jashan