Global variables of type GameObject

leissler

Member
As a developer, I was fiddling around with BD and the source package installed, trying to grasp the possibilities this product opens up.
At one point I saw that the Global Variables live in a serialized asset file. Nevertheless, it seems possible to have global variables of reference types into the scene graph (e.g. GameObject reference).
Does that even make sense? I tried around a bit and it seems to work while the scene is loaded. As soon as I load another scene the reference obviously gets destroyed. I would think it makes sense to simply remove global reference types, as I don't see a use-case for it. Scene reference types in Behavior-Tree parameters (variables) make sense because they are overridden in the scene for each instance of an (external) behavior tree.

Also: How is memory management for instanced behavior trees, like when I attach the same external BT to different GameObjects in a scene. Do they get unloaded when I change the scene and re-loaded when the scene is loaded again? Are they intelligently cached somehow?

Thanks in advance for any insights from this developer perspective.
 
Did this question make sense, or did I misformulate it? Maybe expressed in a too complicated way.

I'd just like to know two things regarding BD:
- What is an exemplary use case for having global variables of type GameObject, since they reference a scene that is potentially not loaded?
- How are (external) Behavior Trees treated memory-wise? When are they loaded and unloaded from memory?

Hope that makes my points more clear.
 
Looks like I missed this one.

You are not able to reference scene-objects from an asset file. You can reference prefabs or other asset files, but not scene files. As an alternative you can reference the GameObject at runtime:


When are they loaded and unloaded from memory?
They follow the C# lifestyle so they are removed from memory as soon as you completely destroy all of the behavior trees referencing those trees.
 
Thanks for explaining and pointing me to that documentation page.
So if it isn't possible to reference Scene Objects from the global variables file, maybe those data types shouldn't be choosable in the first place when creating globals. Doesn't that only add to confusion for developers, if there is no use case for that anyways?

Regarding memory management:
So when I have multiple Bahavior Tree components attached to GameObjects in my scene, which reference external BT-Assets, and then the scene is unloaded, those Behavior-Tress also get unloaded from memory?
 
So if it isn't possible to reference Scene Objects from the global variables file, maybe those data types shouldn't be choosable in the first place when creating globals. Doesn't that only add to confusion for developers, if there is no use case for that anyways?
You can use SharedGameObjects for prefabs so you don't want to restrict the type.

So when I have multiple Bahavior Tree components attached to GameObjects in my scene, which reference external BT-Assets, and then the scene is unloaded, those Behavior-Tress also get unloaded from memory?
Yes, it should as long as the Behavior Tree component is destroyed.
 
Alright, so it's up to the Unity component lifecycle if a BT is destroyed in memory when the scene with the GameObject is unloaded. Got that.

So what you mean with "SharedGameObjects for prefabs" that you can reference prefabs from, e.g. the global variables file?
Like so:
Screen Shot 2021-09-29 at 12.27.05.png

That would make sense, so you can store certain prefabs you want to instantiate during the game within global variables.
And as far as I remember it's not possible to check if it's a reference to a GameObject in the hierarchy or a prefab when dragging something into that slot, right?
[Edit]: Oh, maybe it is possible. You could use the PrefabUlitily class from Unity to check for the prefab vs. gameobject case. Wouldn't that help the user experience if you warn the user if he tries to reference a scene object?
 
Last edited:
Top