切换到宽版
  • 1351阅读
  • 0回复

星期三的智慧-使用C++自定义FlowGraph [复制链接]

上一主题 下一主题
离线天の川
 

只看楼主 倒序阅读 使用道具 楼主  发表于: 2016-04-15
Wednesday Wisdom: Custom FlowGraph node in C++
星期三的智慧:使用C++自定义FlowGraph


Introducing our new section dedicated tofeaturing community created tutorials, tips and tricks, and code! Thisweek "ochounos" shows you how to create a custom FlowGraph node in C++!
社区创造教程,提示,技巧和代码!本周“ochounos”向你展示了如何创建C++自定义流程图节点!

Managing complex FlowGraph logic cansometimes be much easier if it is handled natively in C++ directly. Thistutorial will give you an introduction on how to create a customflowgraph node to simplify your workflow.
如果是原生在C++中直接处理,管理复杂的FlowGraph逻辑有事会容易的多。本教程将向大家介绍如何创建一个自定义FlowGraph节点来简化你的工作流程。

Let’s create a simple FlowGraph node andlook at its core functionality. The node should be able to sum threeintegers and only perform the calculation when a specific operationtriggers.
让我们创建一个简单的FlowGraph节点,并看看它的核心功能。这个节点应该能够求和三个整数,并只有在一个特定的操作之后才开始计算



class CFlowSumNode : public CFlowBaseNode<eNCT_Instanced>
{
  public:
  enum EInputs
  {

IN_TRIGGER = 0,

IN_INTEGER_1,
IN_INTEGER_2,
IN_INTEGER_3
  };

  enum EOutputs
  {
      OUT_SUM
  };

  void CFlowSumNode::GetConfiguration(SFlowNodeConfig& config)
  {
static const SInputPortConfig in_config[] = {
    InputPortConfig<SFlowSystemVoid>( "Trigger", _HELP("Activate the node") ),
    InputPortConfig<int>( "A", _HELP("Operator A") ),
    InputPortConfig<int>( "B", _HELP("Operator B") ),
    InputPortConfig<int>( "C", _HELP("Operator C") )            };
             static const SOutputPortConfig out_config[] = {
    OutputPortConfig<int>( "Sum", _HELP("Sum three integers when activated") )            };

            config.sDescription = "Sum three integers when activated";
            config.pInputPorts  = in_config;
            config.pOutputPorts = out_config;            config.SetCategory(EFLN_APPROVED);
            …
      }
}
REGISTER_FLOW_NODE( "MyCategory:Sum", CFlowSumNode );

The first thing we notice is that our node inherits from CFlowBaseNode<>. The base for any FlowNode.
It defines the basic behavior of our node. It can be constructed with the flowing settings:

  • eNCT_Singleton:  A single instance of the node will be created and referenced by the FlowGraph every time it is used.

  • eNCT_Instanced: A new instance will be created every time this node is added to the FlowGraph.
  • eNCT_Cloned: A newinstance, cloned from the existing instance, will be created every timethis node is added to the FlowGraph. (deprecated)

In this example we use eNCT_Instanced as we do not intent to share the internal node data.



The InputPortConfig<> allows the user to feed data into the flow node.
By default it can be any of the types:
默认情况下它可以填入任何类型:


·  SflowSystemVoid
·  int
·  float
·  bool
·  string
·  Vec3
·  EntityId

In our example this controls when to sumup the three integers as well as the value of each individual integer.The first InputPortConfig<> is of type SFlowSystemVoid. Since it is only used for triggering the calculation it does not require a special data type. SFlowSystemVoid will accept any data type.


The other three InputPortConfig<> target the three integers.
另外三个输入口配置<>目标的三个整数。


Outputs are pretty much defined the same as inputs except the create OutputPortConfig<> instances.



In the last step of defining our flow node, we need to bind the SinputPortConfig in_config[] and SoutputPortConfig out_config[]  information to the SFlowNodeConfig& config, passed into the function by reference.


Ignore the following line for now.
config.SetCategory(EFLN_APPROVED);

We are only missing the code to run the sum of the inputs and put the result in the output.
Every time an input port is modified, our node will receive an event. We need to handle that event.


void CFlowSumNode::ProcessEvent(EFlowEventevent, SActivationInfo* pActInfo)
{
    // Every time that one input is modified the node receives an event
    if (event == eFE_Activate)
    {  
        // Check if Trigger port has been activated
        if (IsPortActive(pActInfo, IN_TRIGGER))
        {
            // Get the value of all operators    
            const int (pActInfo, IN_INTEGER_1);
            const int (pActInfo, IN_INTEGER_2);
            const int (pActInfo, IN_INTEGER_3);

            // Output the sum of the three operators
            ActivateOutput(pActInfo, OUT_SUM, a + b + c);
            }
      }
}

In this simplified example we check the event type first. As we onlywant to act when our “Trigger” input port is activated. We also need tospecifically check if the “Trigger” input port is active specifically,as we do not want to act when any of the “Integer” input ports receivedand activation event.
It is advised to create a set of enums i.e. EInputs/EOutputs next to your input/output ports which map the order in which the ports slots are layout out in your in_config/out_config array. The first entry being assigned slot 0 counting upwards.
Finally you need to register your flow node, defining category and name:
REGISTER_FLOW_NODE( "MyCategory:Sum", CFlowSumNode );

Check the whole source code provided in the attached file. Play around with it to understand FlowGraph nodes even further.
检查所附文件中所提供的全部源代码。尝试它,更进一步理解流程图的节点。


For CRYENGINE V:    对于CRYENGINE V
Create your file at \Code\Game\GameDll\flownodes
在这个 \Code\Game\GameDll\flownodes位置创建你的文件


For CRYENGINE 3.8.6 and earlier:    对于CRYENGINE 3.8.6 以及更早期版本
Create your file at \Code\GameSDK\GameDll\Nodes
在这个\Code\GameSDK\GameDll\Nodes位置创建你的而文件


In both cases, you must add your file to your solution to be included in your generated dll.
这两正情况下,你都需要将你的文件放到您解决方案中,并包含在你生成的dll中。

Download the code sample here.
可以在下面下载样例代码。

- ochounos

代码下载:
附件出售:FlowGraphSumNode.zip
评价一下你浏览此帖子的感受

精彩

感动

搞笑

开心

愤怒

无聊

灌水
要让让这里成为动漫爱好者温馨的天堂~~
官方qq讨论群140439020  官方微信公众号galaxixv
关于论坛G币的获得方法,请参考论坛公告的帖子
http://www.galaxix.com/bbs/read.php?tid=3158
快速回复
限200 字节
如果您在写长篇帖子又不马上发表,建议存为草稿
 
上一个 下一个