Reply To: How to access PlayerController component

NodeCanvas Forums Custom Nodes & Tasks How to access PlayerController component Reply To: How to access PlayerController component

#13940
Gavalakis
Keymaster

Hey,

There are definetely easier ways to go about this 🙂
Probably the best way (and an important feature of NodeCanvas Custom Tasks), would be to derive your Move ActionTask from the generic version of ActionTask<T>, instead of just ActionTask, with T being the Component which the task requires to work with, thus in this case:
public class Move : ActionTask<PlayerController>.

Doing so, and on initialization of the task, the PlayerController component will be fetched (with GetComponent) from the “agent” (The FSMOwner gameobject) and automatically be cached.

To access the PlayerController within the task’s code, you will then need to use the .agent property, which will already be of type PlayerController since you derived the generic ActionTask version. Thus your custom task code will end up like this in the end:

Another benefit of deriving from the generic version of ActionTask as you will see, is that within the inspector of your Move ActionTask, you will (optionally) be able to override (with the checkbox) the “target” agent, or in other words, the PlayerController in this case which you want the task to use. Most of the times though, leaving the option to the default “Use Self” is what you will need.

Thus, deriving from the generic version of ActionTasks where T would be the the Component type you want the action to use, allows you to “not care” about caching or assigning and just use the component directly without worries, through the .agent property.

Please let me know if the above helps and if that is indeed what your question was about.
Thanks!

Join us on Discord: https://discord.gg/97q2Rjh