Newbie question on referencing scripts and non-standard types/properties on instantiated objects

Michael Hutton

New member
Hi all,

Completely new here so please be patient.

I *thought* I needed behaviour Trees in my project but am coming up against some issues. The project is trying to simulate a hospital and the behaviours of patients etc. (don't ask me why). I have versions which do what I want but want to convert the project to use behaviour trees. I am at the very first stages.

1. I am instantiating gameObjects at runtime. So far I have attached a behaviour tree which references an external behaviour tree. This seems to work. I can set a float on the object and change it's colour. Woo!.

But I am getting confused with variables and the documentation goes a bit deep too quickly. Let's say I instantiate my Patient prefab which successfully runs the behaviour and all the floats logged are different meaning I know the myFloat is diffierent for each tree. Great. If I have a script attached to the object I have a field (can make it a property) called Gender but uses the using UnityEngine.Analytics; which is not a standard 'component'

using UnityEngine;
using UnityEngine.Analytics;

public class PatientProperties : MonoBehaviour
{

public Gender Gender { get; set; }

}

In script I can assign the gender etc but I can't work out how to get/set it in the behaviour tree for that object. Do I have to make this a sharedVariable? I am going round in circles at the moment and need some guidance how to reference scripts on my runtime instantiated gameObjects. If you see what I mean.

I'd be very grateful for any pointers or pointers to threads which discuss this. I am also having difficulty know whether to place a script on the object with all the (often non-standard class types) or put them in the behaviour tree and make custom nodes (which is I suspect what I have to do?

Sorry if this is garbled!

Michael
 

Attachments

  • 1725022236833.png
    1725022236833.png
    148.9 KB · Views: 4
In script I can assign the gender etc but I can't work out how to get/set it in the behaviour tree for that object. Do I have to make this a sharedVariable? I am going round in circles at the moment and need some guidance how to reference scripts on my runtime instantiated gameObjects. If you see what I mean.
It looks like you want to use the property mapping feature in order to get the Gender, correct? So you can get and set the Gender within PatientProperties instead of separately on the behavior tree? The absolute easiest would be to make a Gender Shared Variable, but I'll assume that you do want to use the property mapping feature (which is definitely a valid use case).

With that said, since Gender is an enum (I'm assuming?) you will still need to create a new SharedGender variable type. This will allow you to create a new variable called Gender, and then use the Property Mapping feature to map it to the Gender property within your own component. You will want to assign the Property Mapping on the Variables inspector rather than within the Task inspector.

I am also having difficulty know whether to place a script on the object with all the (often non-standard class types) or put them in the behaviour tree and make custom nodes (which is I suspect what I have to do?
There's really no one right answer for this, and I think that as you get more familiar with behavior trees you'll have a better idea of what makes the most sense for your project structure. For example, within the Ultimate Character Controller integration we have many tasks that simply call other components, but with the Deathmatch AI Kit the new tasks contain a log of logic themselves without requiring other components.
 
OK, thanks. I'll dig a bit deeper. I think I have opened pandora's can of worms to mix my metaphors. I'll have to think about how to structure/rewrite a lot of code to implement the behaviour tree.
 
To reference scripts at runtime, can’t you use something like

MyGenderScript myScript = GameObject.FindObjectOfType<MyGenderScript>();

That’s the usual way I go to grab references .

In your gender script maybe you have properties like
int male = 1;
Int female = 2;

In your behavior tree, you can then use a ‘Compare Shared Int’ against your script’s public SharedInt property.

You can also try to use tags on your patients GameObjects and tag them with a gender string. Then use a ‘Compare Tag’ node.

This may not be exactly what you’re looking for but hopefully you get some fresh ideas
 
To be honest I find the shared variables concept quite cumbersome. It adds another layer of abstraction onto simpler objects. I have also used the KiwiCoder's behaviour tree (https://github.com/thekiwicoder0/UnityBehaviourTreeEditor) which let's you add a context, which is basically all the components added to a gameobject the Behaviour tree is on and I find it more intuitive to work with. I suspect Opsive is just using a Dictionary<key, value> to register variables but if I want to access fields within scripts attach to various custom components it gets very tedious and difficult to keep track if you have a few. I feel Opsive is more geared to integrating to the playmaker etc and am not sure it totally fits with my project. But it's good. No drama there!
 
Once you understand how to use shared variables, you will see it's actually quite simple and makes communicating between trees very flexible and powerful. I also appreciate how easy it is to integrate my custom scripts to work with my AI logic. I'm not sure how much development time the kiwi one has, but BD has around 10 years' development. I recommend being patient and learning it, at least if not for your current project then for future ones.

However, use whatever works best for your situation. Good luck and share an update how your project goes.
 
Back
Top