Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Codelyy · Aug 23, 2016 at 04:08 AM · aidelegatesfinite-state-machine

Using coroutines with a Finite State Machine help

I'm trying to create a simple Finite State Machine for the bosses within my game as I want them to have different attacks and randomly switch between them.

This is the script I've used for the Finite State Machine:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class FSM : MonoBehaviour {
 
     public delegate void State();
     private List<State> availableActions = new List<State>();
     private State action;
 
     public FSM(State state)
     {
         action = state;
     }
 
     public void AddState(State state)
     {
         availableActions.Add(state);
     }
 
     public void RandomState()
     {
         SetState(availableActions[Random.Range(0, availableActions.Count)]);
     }
 
     public void SetState(State state)
     {
         action = state;    
     }
 
     void Update () 
     {
         action();
     }
 }
 
   

I simply add a method using the AddState() method within a boss class and it will be added to the availableActions list which will be looped through when calling RandomState(). Or I can just use SetState(). Currently it works fine but I'm having to remove one of the actions that my boss has because it's a coroutine and of course I want to be able to use both types of methods (Voids and Corutines).

This is the error I get: http://i.imgur.com/MWkHeMO.jpg

I know it's because of the coroutine but is there a way I can make my Finite State Machine work with coroutines also?

Thanks

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by DiegoSLTS · Aug 25, 2016 at 08:32 PM

Yes, the error message is pretty clear, you defined the delegate to return void, and your Fire method is a coroutine so it returns IEnumerator.

I guess you can do something like this:

 public void Fire() {
     StartCoroutine(FireCoroutine());
 }
 
 IEnumerator FireCoroutine() {
     //your coroutine code
 }

That way you have a method that matches the delegate and starts a coroutine, but you have to be carefull since the object with the Fire script attached will actually run the coroutine, so it must be an active object and the Fire method should be inside a MonoBehaviour derived class.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by ericbegue · Aug 25, 2016 at 06:43 PM

Instead of a solution, I would like to suggest an alternative approach. Using coroutines or FSMs can quickly become cumbersome for handling complex AI. Using a Behaviour Tree is more appropriate defining such AI. Have a look at Panda BT, which is a scripting framework based on Behaviour Tree.

To give you a preview of how simple it is to define AIs, the script for your Boss AI to randomly select and execute an action from a given set, would look something like this:

 tree("DoRandomAction")
     random
         tree("Action1")
         tree("Action2")
         tree("Action3")
 

Of course, you can do much more that with Behaviour Tree. In fact, it is an handy tool to write any logic that runs over several frames.

I'm the author of Panda BT, so if you have any question about this tool, I'd be glad to help.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Codelyy · Aug 27, 2016 at 12:45 AM 0
Share

Seem quite useful but I'm not really doing any advanced or complicated AI for my game, pretty basic AI. Just switching between different attacks

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

82 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Getting my object to rotate and move at random 1 Answer

Unknown Identifier in Finite State Machine 2 Answers

Basic AI finite state machine: where to put decision logic? 2 Answers

Tennis game like Grand slam tennis 1 Answer

How do I stop an Enemy NavMeshAgent from intersecting the player model? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges