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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by vik.vega · Jun 21, 2013 at 03:20 PM · staticmethod

writing a utility (static) function properly

Hi, there's something i couldn't figure out, after reading about static functions and methods.

I have written a pathfinding utility, which i want to call from the most different contexts within my game (level generation, AI, and so on) so i'd like to call that class likewise Mathf.somefunction. and it has come to my understanding that i should use a static function, which requires all it's variables to be static.

And here's the problem.

the FindShortestPath() function, returning a List of Vector3, has some internal Lists, (declared outside the methods, so they can be accessed by all the functions, i wrote for pathfinding) and making them static, is just a nonsense.

So, since I may be wrong on any of my assumptions, my real question is, is there a way for calling a "utility" function from anywhere, with no need to call it from within an object, and let that function have it's own internal instances ?

 var newPath_ : List.<Vector3> = new PathfindClass.FindShortestPath(startingpoint, endpoint);

Thank you for your time.

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

1 Reply

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

Answer by Coderdood · Jun 21, 2013 at 05:21 PM

You might look at the singleton pattern - an example for unity is here. However this could be viewed as the same thing as making all your members static - since by definition of a singleton there can only be one instance.

It sounds to me that you have a design problem. I would separate the actual logic of finding paths out into a non static class. Say "PathPlanner" or something. If the PathPlanner needs access to common info, for example the world navgrid, that should be separated out into a separate class say "NavGrid" and passed as an argument. So in this case to find a path you might use:

 PathPlanner newPlanner = new PathPlanner();
 newPlanner.FindShortestPath(startPoint, endPoint, navGrid);

 

Having stuff like the NavGrid separated is good idea anyway - as is seperating the path planner because otherwise two objects can't pathfind at the same time.

Once the design is properly separated like this it is very simple to write a utility class like so:

 public static class PathFinding
 {
     private static NavGrid mNavGrid; // The global nav grid
     public static List<Vector3> FindShortestPath(startPoint, endPoint)
     {
         PathPlanner newPlanner = new PathPlanner();
         return newPlanner.FindShortestPath(startPoint, endPoint, mNavGrid);
     }
 }


Comment
Add comment · Show 2 · 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 vik.vega · Jun 21, 2013 at 05:42 PM 0
Share

Thanks ! I think i need to give your answer a few more reads, and then test it since i'm still confused about dealing with non static variables inside my class. I'll let you know, thanks again !

avatar image vik.vega · Jun 21, 2013 at 05:47 PM 0
Share

Well actually it IS a design problem, since i'm clueless about what goes where, and if i need to make Lists static.

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

15 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

Related Questions

Unable to access a static method from another class 2 Answers

Why is my kill method not running the score system method call? 1 Answer

How to call class object from static function? 0 Answers

Trying to run 5 instances of a static method at once to do an action, probably a dumb thing to do? 0 Answers

How is this function parameter magically disappearing? 0 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