I am learning AI type stuff and wanted to do my various states as different classes and swap them out as the state needs to change. Do I need to use C# or Boo for this in Unity along with the virtual/override combo?
There is a zillion way to design it, I guess. One of them is adding/removing (or enabling/disabling) components when significant states change. E.g. you write an AttackingBehaviour script and an IdleBehaviour script, attach both to the object. Then from somewhere, enable/disable what’s needed.
Thanks for the reply. I will look into doing something like that. Seems easy yet flexible.
Or write an FSM system.
-Jeremy
Actually, that is what I am sort of doing, or at least attempting to do.
Well, you can have a script file that contains all of your states, and set up instances for them so they are easy to call. You then have a base entity class that handles state changing. That way you don’t actually have to drag anything to anything, and each state can change the state if needed as well.
-base entity class handles keeping track of current state and changing states
-states class contains all of the states as seperate singleton classes (with instance vars)
Your main agent control code then creates an instance of your base entity class, and uses it to control the states:
myTankFSM.ChangeState(TankState_Idle.Instance);
It takes a bit of code, but it can work really well.
Here is a site that explains many ways do code an FSM as well:
http://www.ai-junkie.com/architecture/state_driven/tut_state1.html
HTH,
-Jeremy
I actually have one of Buckland’s books which I am working from as I get time.
Regarding the many classes in one file, can I do that in Unity, without using a plugin to access C++? Am I incorrect in my understanding of the one class per file in Unity with the name of the file being the same as the class name?
You can have as many classes in one file as you want.
However if you want to attach it to a game object, there needs to be one class in the file that derives from MonoBehaviour (When using C#)
Often you have utility classes and of course you can put them into any file and as many as you like.
Thanks Joachim, I did not know that. Can I also do that with the JS implementation in Unity, or am I limited to C# and Boo? I think someone once posted the Class Declaration for JS on the boards.
I am sure you can do it in JS, but I wouldn’t recommend it for this…but that’s just me
In C# there are also fun things called interfaces which is a special kind of class that you must derive from to use, but the fun part is you can derive from as many interfaces as you like. Can come in handy.
Here is a very quick hack of the general States.cs (csharp of course) structure just to give you an idea:
using UnityEngine;
using System;
public class States
{
}
public class TankState_Idle
{
//set up the singleton Instance as per freyr's code on the wiki
//do whatever. This can get as complex as you like, as shown in buckland's book
}
public class TankState_Evade
{
//blah blah
}
this does not need to be put on any game object. Because you set up the singleton instance variables, it will automatically be set up when needed. Because of this, you also don’t need to call: TankState_Idle tank_idle as new TankState_Idle(); as you normally would to use a class that is not on a GO.
HTH,
-Jeremy
jeremyace, thank you for the pointer. I was leaning towards using C#. I will use it first since it most closely resembles C++ If only it had something like Objective-C’s id.
np, I sent you a PM too btw.
-Jeremy