Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Sendatsu_Yoshimitsu · Jul 04, 2014 at 07:46 AM · c#programmingfunction

Access one function in multiple classes

I'm using a FSM for my AI, with each state being its own class, as in:

 public class PatrolState: FSMState
 {
     public PatrolState()
     {
         stateID = StateID.Patrol;
     }
     
     public override void Reason(GameObject player, GameObject npc)
     {
     //Check all transition conditions
     }
     
     public override void Act(GameObject player, GameObject npc)
     {
     //Walk patrol route
     }
     
 }

Now I notice that I'm using the exact same logic in each state's Act section: a) find the nearest object with some tag, b) pathfinding to that object, c) run a function on that object.

Obviously it would much cleaner (and easier to debug) if I were to encapsulate this logic in a single function that I call in each Act(), but since each state is its own class, it can't see functions that I declare elsewhere. Is there any easy, computationally efficient way to stick this logic in a single place and call it from each Act state?

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 Taschenschieber · Jul 04, 2014 at 09:33 AM

Define your Act method in a class "GenericState" which inherits from FSMState. Then, PatrolState and all the other states you use should inherit from GenericState and use the Act method written in GenericState.

You can give some states different behaviours by simply overriding the Act method in these states.

(I'm not sure I understand exactly what you mean - maybe you are simply searching for static functions?)

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 Sendatsu_Yoshimitsu · Jul 04, 2014 at 10:07 AM 0
Share

Ooh that does the trick nicely, thank you! :)

avatar image
0

Answer by Kiwasi · Jul 04, 2014 at 11:19 AM

Inheritance works best for "is a" type relationships. However there is another way that is worth mentioning, that is to encapsulate you methods in a separate class and call them from there. If your method does not need to store any data you can create a static class. Otherwise you can create a class that you call with new. I'm thinking something like this:

 public static class HelperMethods {

     public bool Act(SomeParamater someParamater){
         //Do some cool stuff
     }

     // Some other methods
 
 }


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

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Do you know the solution of this probelm? (Problem is in the details) 0 Answers

NullReferenceException: Object reference not set to an instance of an object 1 Answer

C# Null Reference Exception in Custom Function 1 Answer

Distribute terrain in zones 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