Prefabs, instances and global variables - more beginner questions

JasonT

Member
Is there anything wrong with this structure?

- I have a prefab with behaviour tree component which is using an external behaviour tree file.
- In the scene I have multiple instances of this prefab.
- I have dragged INSTANCES of other SCENE GAMEOBJECTS into the global variables of the behaviour tree.

It is all working this way, but I'm wondering if I'm breaking any rules because the behaviour tree on a prefab is using an external behaviour tree file, is it ok to be referencing scene objects this way? Or should I should be using the Variable synchronizer component to do this?
 
Last edited:
I’m referencing them in the global variables tab of the Behavior Tree component which I have open in a Tab so I’m not sure where that lives as it’s common to all the trees in the scene.

I’ve noticed that when I switch scenes I do lose any references to scene objects in the global variables tab. So I had moved them to a Variable Syncronizer component which is on my Game Manager Object and they are being set into the global variables that way at the moment. Is it alright to do it that way?
 
Global variables are a project level asset so it's similar to external trees. I recommend using the approach from the docs because it will only run once versus the variable synchronizer running every tick.
 
Hi there. I have a related question as a complete BD beginner.

Having just purchased I was looking to make a small tree, which was working fine. I had some custom made tasks that required references to gameobjects and I could drop them into the inspector tab without trouble.

I then wondered what would happen if I wanted to reuse a behaviour tree for some other object. It seemed like external behaviours were the thing to use. However, as mentioned as soon as I changed scene the references to GO's that were in my tasks disappeared.

This lead me to the docs you posted above where I have to manually push in variables, and my tasks have to now configure themselves by manually pulling them out to use them. What this means is that it doesn't seem convenient to write a task that can work both as a prefab and as an instance. What is the typical solution to this? Is there one? Do I have to have a hack where if I can't find instance references in my public fields, I then fallback to variables?

It would seem that a solution could be that when I have a BT instance in my scene that uses a external behaviour, allow me set the public fields as normal and save those references, restoring them when the external tree is loaded. That is, duplicate the task-local fields into the instance of the tree.


Thanks.
 
It would seem that a solution could be that when I have a BT instance in my scene that uses a external behaviour, allow me set the public fields as normal and save those references, restoring them when the external tree is loaded. That is, duplicate the task-local fields into the instance of the tree.
That is a good solution :) On the behavior tree instance you can add variables that will be sent to the external tree when the game is run. You'll want to make sure those variable names/types match up between the instance and external tree but this is a good way to reference scene variables.
 
That is a good solution :) On the behavior tree instance you can add variables that will be sent to the external tree when the game is run. You'll want to make sure those variable names/types match up between the instance and external tree but this is a good way to reference scene variables.

Thanks for the reply. I was hoping I didn't have to use variables. If I do it means my custom actions need to support both variables AND references or go variables all the way which is less than ideal.

Would be really, really good if BT instances could work like prefab instances do!

Thanks
 
Top