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 /
  • Help Room /
avatar image
0
Question by kingcoyote · Jun 04, 2016 at 05:16 PM · c#pathfindingpooling

Trouble with object pooling

I'm trying to implement object pooling on A* nodes, but when I put in the object pooling code, the pathfinding is unable to find a path. From what I can figure out, the pooled nodes are not being fully cleaned before being pulled from the pool, and the pathfinding is suffering because of this.

Here is a trimmed down version of my class, with the pooling code.

 public class ShippingRouteNode : IAStarNode {
     // either "node" or "line" currently, but making it as a string to potentially support modding
     public string Type;
     // only non-null if type is transitline
     public TransitLineScript TransitLine;
     // machine readable name for the transit type in use
     public string TransitTypeKey;
     // only non-null if type is node
     public TransitNodeScript Node;

     private ShippingRouteNode() {
 
     }
 
     private static Queue<ShippingRouteNode> _nodePool;
 
     public static ShippingRouteNode FromPool() {
         ShippingRouteNode node;
 
         if (_nodePool == null) {
             _nodePool = new Queue<ShippingRouteNode>();
         }
 
         if (_nodePool.Count == 0) {
             node = new ShippingRouteNode();
         } else {
             node = _nodePool.Dequeue();
         }
 
         return node;
     }
 
     public static void ToPool(ShippingRouteNode node) {
         node.Type = "";
         node.TransitLine = null;
         node.TransitTypeKey = "";
         node.Node = null;
 
         _nodePool.Enqueue(node);
     }
 
     public static void ToPool(IEnumerable<ShippingRouteNode> nodes) {
         foreach (var node in nodes) {
             ToPool(node);
         }
     }
 
     public void Dispose() {
         ToPool(this);
     }
 
     public static int PoolSize() {
         if (_nodePool == null) return 0;
 
         return _nodePool.Count;
     }
 }

IAStarNode is an interface, so there is no lingering fields from there, and my ToPool function clears out the only 4 fields this class has. If I change the FromPool function to this, it all works:

     public static ShippingRouteNode FromPool() {
         return new ShippingRouteNode();
     }

This makes me sure that somehow the objects coming from the pool are not perfectly fresh. I just can't figure out why they would contain old data.

I've checked the entire solution for all instances of new ShippingRouteNode(), and the only time is in FromPool.

Does anyone have any idea why my pooling is causing the pathfinding to fail?

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 kingcoyote · Jun 04, 2016 at 07:03 PM

I found the answer. Sorta.

On some nodes, I'm calling ToPool repeatedly. This puts the node in queue more than once. When it is dequeued the first time and modified, the second reference still in queue is also modified. So when that second reference is dequeued, I now have a dirty node.

I need to find out why I'm putting them back in the pool repeatedly and stop that from happening.

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 Chris333 · Jun 04, 2016 at 06:27 PM

Hi,

are you reseting the neighbours and all other fields of each node which get pulled from the pool or are they stay the same. Idk how you implemenmted a* but maybe this could be a possible source of error.

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 kingcoyote · Jun 04, 2016 at 06:34 PM 0
Share

I added some code to clear out the node at the end of FromPool() and all that did was throw an exception in a really bizarre place. I added breakpoints in FromPool and ToPool to monitor the status of every node going into or out of the pool and I observed that nodes going into the pool are scrubbed, but while in the pool some will suddenly be dirty.

I wonder if there is a reference issue somewhere, allowing a node to be both in the pool and accessible elsewhere simultaneously. I think I need to look at every place I pull from the pool and see what I do with the node.

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

154 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 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

Particle Systems. Stick inside pooled objects or pool them separately. 1 Answer

A* all nodes within range 0 Answers

How do I make my A* script work with prefab enemies and make enemies move using the list created through A*? 0 Answers

Move a object along a fixed path. 2 Answers

How to I make a gameobject follow a list of nodes and to link an instantiated gameobject to a script. 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