playMaker

Author Topic: Custom action that operates on components?  (Read 487 times)

HiPhish

  • Playmaker Newbie
  • *
  • Posts: 8
Custom action that operates on components?
« on: September 11, 2021, 10:48:06 AM »
Hello,

I am trying to write a custom action which operates on a particular type of component. The main question is: how do I get a handle on the component instance?

The easiest solution would be to pass an "owner" GameObject either as a value or as a variable and use the GetComponent method on it from within my script. However, if an object can have multiple instance of the component, then that approach won't work.

Alternatively I would like to specify the component itself, perhaps as a variable. There is no variable type for components, but there is FsmObject for objects and I can use the ObjectType attribute to specify the exact type. That sounds like what I need. However, how can I get the component instance? There is an AddComponent action and it has a "Store component" field, but I cannot select any variable to store the result in. All I see when I open the dropdown menu is "None". I have created an object variable, is there anything else I need to do?

Here is how I imagine it would work:

Code: [Select]
public class SetFooProperties: FsmStateAction {
[ObjectType(typeof(Foo))]
public FsmObject foo;

public FsmFloat bar;

public override void OnEnter() {
foo.Value.bar = bar.value;
}
}

HiPhish

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Custom action that operates on components?
« Reply #1 on: September 12, 2021, 04:37:39 PM »
Never mind, I have figured it out, but it's quite unintuitive. First I need to create an FSM variable of variable type Object and assign the actual target type in the object type field. Then I can choose the variable in the drop-down menu of the AddComponent action. The action which consumes the component must expose a corresponding field and set the type to the concrete type in its annotation:

Code: [Select]
public class SetFoo: FsmStateAction {
[Tooltip("The foo")]
[ObjectType(typeof(Foo))]
[UIHint(UIHint.Variable)]  // In my case only a variable makes sense
[RequiredField]            // Ensure that the variable is never None
public FsmObject _foo;

public FsmFloat _val;

public override void OnEnter() {
var foo = _foo.Value as Foo;
if (!_val.IsNone)
foo.val = _val.Value;
}
}

This works fine, but I wish there was a "New variable" entry in the drop-down menu of the AddComponent action.