make Custom Code as Custom Node

Forums General Discussion make Custom Code as Custom Node

This topic contains 2 replies, has 2 voices, and was last updated by  Gavalakis 1 month, 4 weeks ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #1973

    purga
    Participant

    Is it possible to add some custom code and make them a custom node/macro/function to be used in FlowCanvas?
    The following is the code I would like to add as a custom node:

    
    Vector3[] CastToGameObjectPositions( List<GameObject> gameobjects )
    {
         List<Vector3> positions = new List<Vector3>();
         foreach(GameObject go in gameobjects)
         {
             positions.Add(go.transform.position);
         }
    
         return positions.ToArray();
    
    }
    

    Thanks!

    #1974

    purga
    Participant

    I guess something like this should do it:

    
    public class CastToGameObjectPositions : PureFunctionNode<Vector3[], List<GameObject>>
    {
    	public override Vector3[] Invoke(List<GameObject> gameobjects)
    	{
    		List<Vector3> positions = new List<Vector3>();
    
    		foreach(GameObject go in gameobjects)
    		{
    			positions.Add(go.transform.position);
    		}
    
    		return positions.ToArray();
    	}
    }
    
    #1980

    Gavalakis
    Keymaster

    Hello,

    Your second post is exactly how you would make a custom coded node. It is pretty easy as you can tell as well 🙂
    There are also a few other node types you can derive from too depending on the type of node you are after.
    In your node posted above, if for example you change from PureFunctionNode, to CallableFunctionNode, the node will also have a Flow input and a Flow output as well.

    Another completely different way for custom coded nodes, which can also be useful if you want to use your functions outside of FlowCanvas too, would be to create a static class like “Utilities” and create any number of static functions within it.
    Then, open up the [Preferred Types Editor] and add your static “Utilities” class into the list.
    You will now be able to access your Utilities static functions via reflection as normal, through the menu “Functions/Reflected/Utilities/…”.

    Let me know if the above work for you.
    Thanks!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.