Comments (4)
Heyho @Tresky.
What you are observing is the following: If you register a node, you put that node into a global registry, if you then reuse that node in different tree, both trees use the exact copy of that node. So any change you make to this
within one tree, is of course immediately done on the other tree, since that is the same node.
Interestingly, I never thought about that case before, or encountered it. Guess I should write some specs for that. And have to think about a way to resolve that case, without impacting the performance. Sharing the instance way faster then having to create new ones for all trees, but on the other hand, how often are new trees created anyways…
from behaviortree.js.
Hey @Calamari! Thanks for the quick reply. I understand why you'd make it that way for lookup times.
It seems to me that you could copy the entire logic tree into the Tree whenever it is created, so you have independent instances of the nodes in each tree. You could potentially create an independent registry inside of each Tree instance that stores instances of the nodes it needs.
You could still get very fast lookup of the nodes in that tree, the performance of creating a new tree would just be impacted due to the copying of the nodes. As you said, though, new trees likely aren't created in large quantities during runtime, but I guess that would also depend on use case.
I realize, though, that it may be a big push to get to something like that because it would require changing a major implementation detail of the library. Not to mention it's risky anytime you change such a core implementation detail. Maybe you, as the author, have some better thoughts than me on what might work, given you understand the code much more than I do.
My current use case is in the context of game dev. I'm attempting to have each instance of Enemy
own an instance of BehaviorTree
that stores the logic for how the AI should act (wandering, chasing, idling, etc).
Could you think of a way to enable this sort of use-case using this library beyond the major changes I suggested above? Is it just to totally exclude the use of the registry altogether?
from behaviortree.js.
I just took another look at the library code. This might actually be a fairly simple change to make.
The library is using a global registry defined here:
BehaviorTree.js/src/BehaviorTree.js
Lines 4 to 8 in 856bb65
Given the constructor of the BehaviorTree class here:
BehaviorTree.js/src/BehaviorTree.js
Lines 21 to 26 in 856bb65
You could simply add the following to the constructor and you have an independent registry inside the tree.
this.registry = Object.assign({}, registry)
The only other detail would be to change all registry lookups to look inside the reigstry on the tree instead of the global one.
It's not necessarily 0 performance impact, but I can't imagine it would be very unless your regsitry grows to a large size. At that point, it wouldn't be hard to add some nice optimization where only the nodes needed are copied from the registry.
from behaviortree.js.
Hmm, I thought I posted some thoughts, this morning. Apparently I forgot to hit the comment button. Well, here is a more informed look:
I just checked and it is not as simple. The solution you suggest will copy the registry, but the instances will be still the same, since JavaScript is copying objects by reference. So you would gain nothing.
The best idea I came up with, until now is, for a maybe additional syntax for creating objects that are instantiated on demand, something like:
BehaviorTree.register('my-decorated-node', [MyDecorator, {
node: 'my-sequence',
config: { cooldown: 2 }
}])
It feels a bit cumbersome, with that array, but on the otherhand it is maybe nice to serialize, and since passing in a function as creator-function is out, since it is used as a short version to define tasks. (I wonder how many people really use that, and if that is really needed.)
On a slightly related topic:
Since a while, I have the idea in my head, that it might be a useful idea to split out the registry stuff into an own class, so you can attach different registries to different behavior trees. But I still don't see a real use case for that. Thought this might be a case to do this, but I think it will not yield benefits here sadly.
from behaviortree.js.
Related Issues (20)
- it doesn't work in browser HOT 3
- Need node break function HOT 3
- Task with no immediate return SUCCESS/FAILURE instantly returns SUCCESS HOT 6
- The first task of a nested sequence does not seem to inoke start HOT 14
- debug: true not working HOT 8
- Help understanding JSON trees HOT 2
- What is the correct way to import behaviortree in a webpack app? (You may need an appropriate loader to handle this file type) HOT 3
- Export tree to JSON HOT 2
- Programmatically pushing nodes to branchnodes requires update of numNodes property HOT 2
- Node.js constructor: use original user parameter instead of destructuring HOT 3
- Unneeded dependencies HOT 2
- Add TypeScript support HOT 2
- How to make parallel behavior? HOT 7
- QA: alternative names for node HOT 2
- Bad usage examples HOT 3
- Missing `start` call ? HOT 5
- Question: Is there a non-remember Selector? HOT 4
- LoopDecorator ignores RUNNING result HOT 4
- CooldownDecorator example
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from behaviortree.js.