NodeCanvas Forums › General Discussion › Access properties & fields of Dynamic Vars
Hi,
In my behaviour tree I make the AI search for a type called ‘lamp’ via a custom GetClosestOfType<T> action, which stores the closest lamp as a Dynamic Variable, so far so good. Now, this lamp class has an ‘active’ bool property which controls the sprite replacment, light shading, etc..
What I would need to do, is to access this property to check inside NC if the lamp is currently active or not, so that I can then collect a list of lamps that are active and one list with those that are not, but I have no clue how this is done. Accessing the properties and fields of Dynamic Variables would be a huge help. Is this possible? Or can I filter it directly inside GetClosestOfType<T> via Linq and expose it visually in the node?
This is the class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
public class GetClosestOfType<T> : ActionTask<Agent> where T : Component { [BlackboardOnly] public BBParameter<Vector3> savePosition; [BlackboardOnly] public BBParameter<Transform> saveTransform; [BlackboardOnly] public BBParameter<GameObject> saveGameObject; [BlackboardOnly] public BBParameter<T> saveComponent; protected override void OnExecute() { float closestDistance = Mathf.Infinity; var objects = Object.FindObjectsOfType<T>(); T closestObj = null; if(objects != null && objects.Length != 0) { foreach(var obj in objects) { var dist = Vector3.Distance(agent.transform.position, obj.transform.position); if(dist < closestDistance) { closestDistance = dist; closestObj = obj; } } savePosition.value = closestObj.transform.position; saveTransform.value = closestObj.transform; saveGameObject.value = closestObj.gameObject; saveComponent.value = closestObj; EndAction(true); return; } EndAction(false); } } |
Hey,
If you want to manually access a variable value (dynamic or not), from within your custom task, you can do so by calling:
blackboard.GetValue<T>(string name)
. So, for example blackboard.GetValue<Lamp>("myLamp")
You can also use a public BBParameter<Lamp> lamp
variable in your task which will expose a parameter of type Lamp in the inspector, which you can set to also be dynamic and type in the name of the variable that should get it’s value from. In this case, if a dynamic variable has not yet been generated by your GetClosestOfType
Please let me know if this is what you ment, or maybe I completely misunderstood the question which I think is quite possible in this case 🙂
Thanks.
Join us on Discord: https://discord.gg/97q2Rjh
Hello,
sorry for being not clear. So, I find the closest type “Lamp” in NC with the GetClosestOfType action and cache it as a dynamic var (Of which implementation was a brilliant idea by the way!) Now I need to access a public bool property inside that class “Lamp”, but since “Lamp” is a dynamic variable, I don’t know how to best accomplish this.
I know, I could work around this by scripting certain methods and special actions, but I just wanted to make sure, that I don’t overlook an easier approach.
A pseudo version of the class would be:
1 2 3 4 5 6 7 |
public class Lamp : Device (: MonoBehaviour) { public bool active // Which I need to check inside NC. { get; set; } } |
Thanks!
Hey,
Don’t worry about and thanks! 🙂
So, if you are refering to accesing the property with the ‘GetProperty’ script control task, that would be impossible right now, because the ‘GetProperty’ action can’t work with dynamic variables since the option for “(DynamicVar)” does not exist in the variable dropdown selection for when selecting a variable for the target agent (replacing “Self”).
If the above is what you need, I can certainly allow dynamic vars here. I just haven’t done this yet to avoid possible confusions and errors by wrong typed variables.
The only alternative solution right now would be to create a custom task to access that property like for example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class GetIsActive : ActionTask{ public BBParameter<Lamp> lamp; [BlackboardOnly] public BBParameter<bool> isActive; protected override void OnExecute(){ isActive.value = lamp.value.active; EndAction(); } } |
In a future version, I plan on allowing a property of a variable to be selected directly within the dropdown parameter selection. So that if for example you have a BBParameter
This feature is comming rather soon.
Thanks!
Join us on Discord: https://discord.gg/97q2Rjh
Hi,
if you could make dynamic variables available for the GetProperty action, that would be very helpful. Until then I’ll work around with custom classes like yours above. 🙂
Thanks alot, Gavalakis!
You are welcome 🙂
I will allow dynamic variables for overriding agent parameter (thus possible to use GetProperty with them) in the version after the one already submited and pending review in the asset store.
Thanks!
Join us on Discord: https://discord.gg/97q2Rjh
Oh, know I understand, what you mean by replacing it with ‘Self’ a.k.a. agent parameter. Hell, that’s a wonderful idea!
Hehe 🙂
Glad to know you like it 🙂
Thanks!
Join us on Discord: https://discord.gg/97q2Rjh