Lua behavior tree editor implementation method and system based on Unity engine

By using the NodeGraphProcessor plug-in in the Unity engine to export the C# file of the Lua behavior tree node and introduce value nodes, the problem of difficulty in Lua behavior tree configuration editing in the existing technology is solved, and efficient and convenient behavior tree editing and configuration export are achieved.

CN119847505APending Publication Date: 2025-04-18XIAMEN WOOBEST INTERACTIVE NETWORK TECH CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202411938795.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-26
Publication Date
2025-04-18

AI Technical Summary

Technical Problem

The existing Lua behavior tree configuration lacks an intuitive editor, which makes it difficult to edit the Lua behavior tree configuration and edit it, which is difficult to integrate into the development workflow, and it is difficult to write manually.

Method used

The Lua behavior tree editor implementation method based on the Unity engine, exports the Lua implementation script of the Lua behavior tree node into a C# file through the NodeGraphProcessor plug-in, implements the editor to customize the editable node, and introduces value nodes to support more flexible parameter configuration.

Benefits of technology

It realizes intuitive, efficient and convenient editing of the parameters and connections of behavior tree nodes, supports direct editing of new node types in the editor, and supports hot code updates, improving the efficiency and convenience of configuration editing.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119847505A_ABST
    Figure CN119847505A_ABST
Patent Text Reader

Abstract

The invention discloses a Lua behavior tree editor implementation method and system based on a Unity engine, and the method comprises the steps: exporting a Lua implementation script of a Lua behavior tree node as a corresponding editor node definition class C # file, and achieving editor self-definition editable nodes for the editor node definition class C # file through the Unity engine; a value node is introduced into the Lua behavior tree, a value node in the Lua implementation script is exported as a corresponding editor C # file, and application of the editor value node is achieved through a Unity engine; creating and editing a Lua behavior tree node graph of the editor; and exporting a Lua behavior tree configuration file of the editor. The problem that Lua behavior tree configuration is difficult to edit is solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the technical field of game development, and in particular to a method and system for implementing a Lua behavior tree editor based on a Unity engine. Background Art

[0002] Behavior tree is a programming paradigm widely used in game development and artificial intelligence. It can generate complex and controllable non-player character (NPC) behaviors and can be used to organize complex logic implementations. In game development, behavior tree enables planners or artists to independently design and implement the expected complex logic without writing program code.

[0003] In current mobile game development, Lua is widely used in game business development, and therefore is also often used to implement behavior trees. However, the existing behavior tree configuration lacks an intuitive editor and cannot directly support the configuration editing and use of Lua behavior trees, which makes the configuration of behavior trees very difficult and difficult to integrate into the development workflow, and it is difficult to manually write them.

[0004] A Chinese patent with publication number CN116932535A discloses a method and system for generating an agent behavior tree based on reinforcement learning and a genetic algorithm, wherein the method includes: adding the offspring behavior tree generated by the genetic algorithm to the action space of the reinforcement learning. The behavior tree generated by reinforcement learning is used to drive the agent, and the value estimate of the behavior tree by reinforcement learning is used as a reference for measuring fitness. The genetic algorithm screens, reorganizes and mutates the offspring of the behavior tree according to the fitness, and repeatedly iterates the process until a behavior tree that can drive the agent to complete the task goal is obtained. In this way, a behavior tree that can drive the agent to complete the task goal can be generated in an experimental environment that only provides delayed rewards. The invention generates a behavior tree through an automated mechanism, but the reinforcement learning and genetic algorithm used therein have high computational overhead, slow convergence speed, and may generate complex behavior trees that are difficult to control. Summary of the invention

[0005] In order to solve the problems existing in the above-mentioned prior art, the present invention provides a method and system for implementing a Lua behavior tree editor based on the Unity engine, aiming to solve the problem of difficulty in configuring and editing Lua behavior trees.

[0006] The technical solution of the present invention is as follows:

[0007] On the one hand, the present invention provides a method for implementing a Lua behavior tree editor based on the Unity engine, comprising the following steps:

[0008] Export the Lua implementation script of the Lua behavior tree node to the corresponding editor node definition class C# file, and use the NodeGraphProcessor plug-in of the Unity engine to implement the editor's custom editable nodes in the editor node definition class C# file.

[0009] Introduce value nodes into Lua behavior trees, export value nodes in Lua implementation scripts to corresponding editor C# files, and implement the application of editor value nodes through the NodeGraphProcessor plug-in of the Unity engine.

[0010] Create and edit Lua behavior tree node graphs for the editor based on the NodeGraphProcessor plugin.

[0011] Export the Lua behavior tree configuration file of the editor based on the NodeGraphProcessor plugin.

[0012] Preferably, the step of exporting the Lua behavior tree node implementation script to an editor node definition class C# file used by the editor is specifically as follows:

[0013] The Lua file defines the Lua behavior tree node definition information required by the Lua behavior tree node implementation script according to the preset writing standard style, including field name, field type, and field Chinese comment.

[0014] By triggering the parsing command of the Unity editor, the Lua file content is read line by line. When the node field description is read, the Lua behavior tree node definition information is obtained through regular expression parsing. After the Lua file is parsed, the parsing result is obtained.

[0015] The parsing result is stored in a Lua behavior tree node field configuration data structure, wherein the Lua behavior tree node field configuration data structure is included in a Lua behavior tree node configuration information data structure.

[0016] Generate the corresponding editor node definition class C# file based on the data object obtained from the parsing result.

[0017] Generate the implementation function required to export the Lua configuration script for subsequent export of the Lua configuration script.

[0018] Preferably, the editor custom editable node is implemented by using the NodeGraphProcessor plug-in of the Unity engine to define the editor node class C# file. Specifically, the NodeGraphProcessor plug-in adds a subclass of BTreeNode to implement the custom editable node. The writing style specification of the subclass is:

[0019] The definition of the subclass adds two types of properties: System.Serializable and NodeMenuItem. The System.Serializable property is used to serialize the instance of the class to save and load the state of the node in the NodeGraphProcessor plug-in. The NodeMenuItem property is used to define the directory path when the user creates the node in the NodeGraphProcessor plug-in.

[0020] The subclass overrides the acquisition of the Name field to specify the node display name.

[0021] The subclass adds a node field name attribute to the member field, and the node field name attribute is used to display the editable node field in the editor.

[0022] Preferably, the steps of introducing a value node into the Lua behavior tree, exporting the value node in the Lua implementation script as a corresponding editor C# file, and implementing the application of the editor value node through the NodeGraphProcessor plug-in of the Unity engine are specifically as follows:

[0023] Value nodes are configuration items that can be reused in multiple Lua behavior tree nodes. They are used to represent constants or calculation results shared in Lua behavior trees. Each value node has a unique ID, which also defines the node type and input parameter configuration.

[0024] The final result of the field value of the value node is defined by the node field value acquisition function. According to different input parameter configurations, the node field value acquisition function performs corresponding processing to obtain the final result of the field value.

[0025] Based on the NodeGraphProcessor plug-in, when the value node in the Lua implementation script is exported to the corresponding editor C# file, the OutPut attribute is added to the value node field. The OutPut attribute is used to define the field as an output domain and can be connected to other input domains of the same data type during editing to realize the application of the editor value node.

[0026] Preferably, according to different input parameter configurations, the node field value acquisition function performs corresponding processing to obtain the field value, and the final result is specifically:

[0027] Input parameter configurations for Value nodes include: direct constant values, references to other Value nodes, and Value node configurations that contain calls or parameter settings.

[0028] For direct constant values, the node field value acquisition function directly returns the final result of the field value.

[0029] For references to other value nodes, the node field value acquisition function obtains the corresponding value node configuration content through the ID index, and makes a recursive call to return the final result of the field value.

[0030] For a value node configuration including a call or parameter setting, the GetOutputVal function is called, and the GetOutputVal function is used to return the final result of the field value by recursively calling the node field value acquisition function.

[0031] Preferably, the step of exporting the Lua behavior tree configuration file of the editor based on the NodeGraphProcessor plug-in is specifically as follows:

[0032] The node information and node connection relationship data of the node graph are obtained through the API of the NodeGraphProcessor plug-in. The node information is the serialized node object.

[0033] Recursively traverse and export from the root node of the editor node graph, read the node connection relationship data on the editor, and finally export the structure description of the entire Lua behavior tree to generate the corresponding Lua behavior tree configuration file.

[0034] On the other hand, the present invention provides a Lua behavior tree editor implementation system based on the Unity engine, including an editor custom editable node construction module, an editor value node construction module, a Lua behavior tree node graph creation and editing module, and a Lua behavior tree configuration file export module.

[0035] The editor custom editable node building module is used to export the Lua implementation script of the Lua behavior tree node to the corresponding editor node definition class C# file, and implement the editor custom editable node through the editor node definition class C# file of the Unity engine's NodeGraphProcessor plug-in.

[0036] The editor value node building module is used to introduce value nodes into Lua behavior trees, export value nodes in Lua implementation scripts to corresponding editor C# files, and implement the application of editor value nodes through the NodeGraphProcessor plug-in of the Unity engine.

[0037] Lua behavior tree node graph creation and editing module, used to create and edit the editor's Lua behavior tree node graph based on the NodeGraphProcessor plugin.

[0038] Lua behavior tree configuration file export module, used to export the editor's Lua behavior tree configuration file based on the NodeGraphProcessor plugin.

[0039] On the other hand, the present invention further provides an electronic device, comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the method for implementing a Lua behavior tree editor based on the Unity engine as described in any embodiment of the present invention is implemented.

[0040] On the other hand, the present invention further provides a computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed by a processor, the method for implementing a Lua behavior tree editor based on a Unity engine as described in any embodiment of the present invention is implemented.

[0041] Compared with the prior art, the present invention has the following technical effects:

[0042] The method proposed in the present invention can support Lua behavior tree editing and exporting, and can edit the parameters and connections of behavior tree nodes intuitively, efficiently and conveniently; and when a new node type is added on the Lua side, it can directly support editing in the editor; and at the same time, it supports the demand for hot code updates. BRIEF DESCRIPTION OF THE DRAWINGS

[0043] Figure 1 It is an overall flow chart of the method for implementing the Lua behavior tree editor based on the Unity engine described in the present invention;

[0044] Figure 2 Edit the schematic for the coordinate offset value node;

[0045] Figure 3 Get the function processing flow chart for the node field value;

[0046] Figure 4 Export algorithm flow graph for Lua configuration file of value node. DETAILED DESCRIPTION

[0047] In order to make the objectives, technical solutions and advantages of the present invention more clear, the technical solutions of the present invention will be clearly and completely described below in combination with specific embodiments of the present application and with reference to the accompanying drawings.

[0048] Embodiment 1

[0049] This embodiment provides a method for implementing a Lua behavior tree editor based on the Unity engine to obtain an editor tool for efficiently and intuitively editing Lua behavior trees. Combined with the NodeGraphProcessor plug-in of the Unity engine, the Lua behavior tree node implementation script is exported as a C# node implementation script that can be recognized by the plug-in through an algorithm, so that the Lua behavior tree nodes can be edited and created in the editor. After the Lua behavior tree is edited, the node information of the editor is exported as a Lua behavior tree configuration script through the export algorithm. At the same time, the editor's persistent configuration asset file (node ​​graph configuration file) will save the node graph information in real time, and can be opened for editing and export at any time. See Figure 1 As shown, the following steps are included:

[0050] Export the Lua implementation script of the Lua behavior tree node to the corresponding editor node definition class C# file, and use the NodeGraphProcessor plug-in of the Unity engine to implement the editor's custom editable nodes in the editor node definition class C# file.

[0051] As a preferred implementation of this embodiment, the steps of exporting the Lua behavior tree node implementation script to an editor node definition class C# file used by the editor are as follows:

[0052] The Lua file defines the Lua behavior tree node definition information required by the Lua behavior tree node implementation script according to the preset writing standard style, including field name, field type, and field Chinese comment.

[0053] By triggering the parsing command of the Unity editor, the Lua file content is read line by line. When the node field description is read (such as the content starting with ---@field), the Lua behavior tree node definition information is obtained through regular expression parsing. After the Lua file is parsed, the parsing result is obtained.

[0054] The parsing result is stored in the Lua behavior tree node field configuration data structure (which may be named BTreeNodeParamFieldCfgOfLua), which is included in the Lua behavior tree node configuration information data structure (which may be named BTreeNodeCfgInfoOfLua).

[0055] Generate the corresponding editor node definition class C# file based on the data object obtained from the parsing result. Specifically, generate the class name definition code text and field attribute domain text (including input domain and output domain). At the same time, in order to support table field auxiliary input, generate the corresponding data map so that the table name corresponding to the field can be obtained according to the field name during subsequent editing.

[0056] Generate the implementation function required to export the Lua configuration script, which is used to export the node content to the Lua configuration script later.

[0057] Through the above conversion method, the node data and algorithm implementation functions are organized, and the specific operations of the C# data object are encapsulated in the C# class file. The outside world does not need to care about the specific implementation and only needs to call it.

[0058] As a preferred implementation of this embodiment, the editor custom editable node is implemented by using the NodeGraphProcessor plug-in of the Unity engine to define the editor node class C# file. Specifically, the NodeGraphProcessor plug-in adds a subclass of BTreeNode to implement the custom editable node. The writing style specification of the subclass is:

[0059] The definition of the subclass adds two types of properties: System.Serializable and NodeMenuItem. The System.Serializable property is used to serialize the instance of the class to save and load the state of the node in the NodeGraphProcessor plug-in. The NodeMenuItem property is used to define the directory path when the user creates the node in the NodeGraphProcessor plug-in.

[0060] The subclass overrides the acquisition of the Name field to specify the node display name.

[0061] The subclass adds a node field name attribute to the member field, and the node field name attribute is used to display the editable node field in the editor.

[0062] Introduce value nodes into Lua behavior trees, export value nodes in Lua implementation scripts to corresponding editor C# files, and use the NodeGraphProcessor plug-in of the Unity engine to apply editor value nodes. Figure 2 As shown, this embodiment introduces the concept of value node type based on the traditional Lua behavior tree concept. Value node represents a node with output value, which is used to obtain the input value of the Lua behavior tree node field. Through the connection and organization of value nodes, parameter configuration with multiple complex relationships can be more flexibly supported. Each value node has input parameters and outputs. It accepts a series of parameter inputs and returns an output result at runtime. At runtime, the value node directly obtains the final input value of a parameter through an interface. The value node file can be recognized as a value node by the editor by the standard file name starting with BTVal.

[0063] As a preferred implementation of this embodiment, the steps of introducing a value node into a Lua behavior tree, exporting the value node in the Lua implementation script as a corresponding editor C# file, and implementing the application of the editor value node through the NodeGraphProcessor plug-in of the Unity engine are as follows:

[0064] Value nodes are configuration items that can be reused in multiple Lua behavior tree nodes. They are used to represent constants or calculation results shared in Lua behavior trees. Because value nodes can be reused, all value nodes of Lua behavior trees are flattened and collected into a data container in the configuration file. Each value node has a unique ID, which also defines the node type and input parameter configuration. For example, coordinate offset requires defining two receiving parameters: original coordinates and offset coordinates.

[0065] The final result of the field value of the value node is defined by the node field value acquisition function. According to different input parameter configurations, the node field value acquisition function performs corresponding processing to obtain the final result of the field value.

[0066] Based on the NodeGraphProcessor plug-in, when the value node in the Lua implementation script is exported to the corresponding editor C# file, the OutPut attribute is added to the value node field. The OutPut attribute is used to define the field as an output domain and can be connected to other input domains of the same data type during editing to realize the application of the editor value node.

[0067] As a preferred implementation of this embodiment, Figure 3 As shown in the figure, according to different input parameter configurations, the node field value acquisition function performs corresponding processing to obtain the field value. The final result is:

[0068] The input parameter configuration of the value node includes:

[0069] Direct constant value, such as V3(8.72,9.08,0).

[0070] References to other value nodes (can be specified as field contents using {valIdx=value node ID} or similar style specifications). For example, for a Lua behavior tree node, if a field domain uses a value node as input pos={valIdx=1}, it means that the value of the pos field uses the output value of the value node ID 1.

[0071] Contains the value node configuration for the call or parameter settings.

[0072] For the above three forms, the node field value acquisition function performs corresponding processing:

[0073] For non-table direct constant values, the node field value acquisition function directly returns the final result of the field value.

[0074] For references to other value nodes, the node field value acquisition function obtains the corresponding value node configuration content through the ID index, and makes a recursive call to return the final result of the field value.

[0075] For a value node configuration including a call or parameter setting, the GetOutputVal function is called, and the GetOutputVal function is used to return the final result of the field value by recursively calling the node field value acquisition function.

[0076] Create and edit the Lua behavior tree node graph of the editor based on the NodeGraphProcessor plug-in. This step mainly utilizes the function of the NodeGraphProcessor third-party plug-in. For each Lua behavior tree, the user can directly create a node graph in the Unity engine and perform operations such as opening and editing. This embodiment does not use its runtime function, but only uses its node editing function.

[0077] Export the Lua behavior tree configuration file of the editor based on the NodeGraphProcessor plug-in. Specifically, the user can operate in the node graph editing interface and click the customized export button to export the node information of the node graph to the Lua behavior tree configuration file. Among them, each node graph asset file corresponds to a Lua behavior tree configuration file.

[0078] As a preferred implementation of this embodiment, the steps of exporting the Lua behavior tree configuration file of the editor based on the NodeGraphProcessor plug-in are specifically as follows:

[0079] The node information and node connection relationship data of the node graph are obtained through the API of the NodeGraphProcessor plug-in. The node information is the serialized node object.

[0080] Recursively traverse and export from the root node of the editor node graph, read the node connection relationship data on the editor, and finally export the structure description of the entire Lua behavior tree to generate the corresponding Lua behavior tree configuration file.

[0081] Furthermore, the export algorithm is as follows: detect whether the Lua behavior tree has multiple root nodes; if there are multiple root nodes, throw an exception; traverse all nodes, and consider the node without a parent node as the root node; Figure 4 As shown, all value nodes are collected and traversed to generate the corresponding value node configuration Lua code text; for each collected value node, its ToLuaString interface is called to generate the corresponding value node Lua code and perform text splicing.

[0082] Furthermore, the implementation of the ToLuaString interface is divided into two parts, including the generation of the type name (which can be defined as NodeTypeToLua) and the generation of the parameter list (which can be defined as ParamToLua). The generation of the type name can be directly read from the NodeType field on the node. The generation of the parameter list is implemented by a specific value node, and its generation behavior is a fixed template. When the C# node is generated, it will automatically organize the generation method according to the parameter definition domain of the Lua node script. Each different node type has its own pre-generated ParamToLua interface.

[0083] Furthermore, the generation logic of the ParamToLua interface is: for each field, its template can be set to {field name} = {domain resolution interface name corresponding to the field data type} ({field name}, "{field name}", this) and other similar formats; by traversing all fields, the ParamToLua interface implementation can be spliced ​​and generated.

[0084] The domain parsing interface name corresponding to the field data type is responsible for converting the specific C# value type into Lua description, such as converting Vector3 into V3 in Lua, converting Bool value into true or false, etc. They can be set as: ParseVector3FieldToLua, ParseIntFieldToLua, ParseStringFieldToLua, ParseBoolFieldToLua, ParseFloatFieldToLua, etc.

[0085] The above interfaces will first search for other value nodes they reference. If there are any, the corresponding value node association description will be directly exported: {valIdx = other value node ID}. Otherwise, the Lua code text of the corresponding type will be directly exported.

[0086] After the value node is exported, the Lua configuration description of each node and its child nodes will be exported from the root node of the behavior tree by traversal. The algorithm for exporting the Lua configuration file of non-value nodes is roughly the same as that of value nodes, so I will not go into details here.

[0087] Embodiment 2

[0088] Accordingly, this embodiment provides a Lua behavior tree editor implementation system based on the Unity engine, and the system is used to implement the Lua behavior tree editor implementation method based on the Unity engine as described in any embodiment of the present invention, including an editor custom editable node construction module, an editor value node construction module, a Lua behavior tree node graph creation and editing module, and a Lua behavior tree configuration file export module.

[0089] The editor custom editable node building module is used to export the Lua implementation script of the Lua behavior tree node to the corresponding editor node definition class C# file, and implement the editor custom editable node through the editor node definition class C# file of the Unity engine's NodeGraphProcessor plug-in.

[0090] The editor value node building module is used to introduce value nodes into Lua behavior trees, export value nodes in Lua implementation scripts to corresponding editor C# files, and implement the application of editor value nodes through the NodeGraphProcessor plug-in of the Unity engine.

[0091] Lua behavior tree node graph creation and editing module, used to create and edit the editor's Lua behavior tree node graph based on the NodeGraphProcessor plugin.

[0092] Lua behavior tree configuration file export module, used to export the editor's Lua behavior tree configuration file based on the NodeGraphProcessor plugin.

[0093] Embodiment 3

[0094] This embodiment provides an electronic device, comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the method for implementing a Lua behavior tree editor based on the Unity engine as described in any embodiment of the present invention is implemented.

[0095] Embodiment 4

[0096] This embodiment provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor, the method for implementing a Lua behavior tree editor based on a Unity engine as described in any embodiment of the present invention is implemented.

[0097] In the embodiments of the present application, "at least one" refers to one or more, and "more than one" refers to two or more. "And / or" describes the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B can represent the existence of A alone, the existence of A and B at the same time, and the existence of B alone. Among them, A and B can be singular or plural. The character " / " generally indicates that the previous and next associated objects are in an "or" relationship. "At least one of the following" and similar expressions refer to any combination of these items, including any combination of single or plural items. For example, at least one of a, b and c can be represented by: a, b, c, a and b, a and c, b and c, or a and b and c, where a, b, c can be single or multiple.

[0098] Those of ordinary skill in the art will appreciate that the various units and algorithm steps described in the embodiments disclosed herein can be implemented in a combination of electronic hardware, computer software, and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.

[0099] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.

[0100] In several embodiments provided in the present application, if any function is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (Read-Only Memory; hereinafter referred to as: ROM), random access memory (Random Access Memory; hereinafter referred to as: RAM), disk or optical disk, and other media that can store program codes.

[0101] The above descriptions are merely embodiments of the present invention and are not intended to limit the patent scope of the present invention. Any equivalent structure or equivalent process transformation made using the contents of the present invention specification and drawings, or directly or indirectly applied in other related technical fields, are also included in the patent protection scope of the present invention.

Claims

1. A method for implementing a Lua behavior tree editor based on the Unity engine, characterized in that: The following steps are involved: Export the Lua implementation script of the Lua behavior tree node to the corresponding editor node definition class C# file, and use the Unity engine's NodeGraphProcessor plug-in to implement the editor's custom editable node in the editor node definition class C# file; Introduce value nodes into Lua behavior trees, export value nodes in Lua implementation scripts to corresponding editor C# files, and use editor value nodes through the NodeGraphProcessor plug-in of the Unity engine; Create and edit the editor's Lua behavior tree node graph based on the NodeGraphProcessor plugin; Export the Lua behavior tree configuration file of the editor based on the NodeGraphProcessor plugin.

2. The method for implementing a Lua behavior tree editor based on the Unity engine according to claim 1, characterized in that: The steps of exporting the Lua behavior tree node implementation script to an editor node definition class C# file used by the editor are as follows: The Lua file defines the Lua behavior tree node definition information required by the Lua behavior tree node implementation script according to the preset writing specification style, including field name, field type, and field Chinese comment; By triggering the parsing command of the Unity editor, the Lua file content is read line by line. When the node field description is read, the Lua behavior tree node definition information is obtained through regular expression parsing. After the Lua file is parsed, the parsing result is obtained; The parsing result is stored in a Lua behavior tree node field configuration data structure, wherein the Lua behavior tree node field configuration data structure is included in a Lua behavior tree node configuration information data structure; Generate the corresponding editor node definition class C# file based on the data object obtained from the parsing result; Generate the implementation function required to export the Lua configuration script for subsequent export of the Lua configuration script.

3. The method for implementing a Lua behavior tree editor based on the Unity engine according to claim 2, characterized in that: The NodeGraphProcessor plug-in of the Unity engine is used to define the editor node class C# file to implement the editor's custom editable nodes. Specifically: the NodeGraphProcessor plug-in adds a subclass of BTreeNode to implement custom editable nodes. The writing style specification of the subclass is: The definition of the subclass adds two types of properties: System.Serializable and NodeMenuItem. The System.Serializable property is used to serialize the instance of the class to save and load the state of the node in the NodeGraphProcessor plug-in. The NodeMenuItem property is used to define the directory path when the user creates the node in the NodeGraphProcessor plug-in; The subclass overrides the acquisition of the Name field to specify the node display name; The subclass adds a node field name attribute to the member field, and the node field name attribute is used to display the editable node field in the editor.

4. The method for implementing a Lua behavior tree editor based on the Unity engine according to claim 1, characterized in that: The steps of introducing value nodes into Lua behavior tree, exporting value nodes in Lua implementation scripts into corresponding editor C# files, and implementing application of editor value nodes through NodeGraphProcessor plug-in of Unity engine are as follows: Value nodes are configuration items that can be reused in multiple Lua behavior tree nodes. They are used to represent constants or calculation results shared in Lua behavior trees. Each value node has a unique ID, which also defines the node type and input parameter configuration. The final result of the field value of the value node is defined by the node field value acquisition function. According to different input parameter configurations, the node field value acquisition function performs corresponding processing to obtain the final result of the field value; Based on the NodeGraphProcessor plug-in, when the value node in the Lua implementation script is exported to the corresponding editor C# file, the OutPut attribute is added to the value node field. The OutPut attribute is used to define the field as an output domain and can be connected to other input domains of the same data type during editing to realize the application of the editor value node.

5. The method for implementing a Lua behavior tree editor based on the Unity engine according to claim 4, characterized in that: According to different input parameter configurations, the node field value acquisition function performs corresponding processing to obtain the field value. The final result is: Input parameter configurations for value nodes include: direct constant values, references to other value nodes, and value node configurations containing calls or parameter settings; For direct constant values, the node field value acquisition function directly returns the final result of the field value; For references to other value nodes, the node field value acquisition function obtains the corresponding value node configuration content through the ID index, and makes a recursive call to return the final result of the field value; For a value node configuration including a call or parameter setting, the GetOutputVal function is called, and the GetOutputVal function is used to return the final result of the field value by recursively calling the node field value acquisition function.

6. The method for implementing a Lua behavior tree editor based on the Unity engine according to claim 1, characterized in that: The steps for exporting the Lua behavior tree configuration file of the editor based on the NodeGraphProcessor plug-in are as follows: Obtain node information and node connection relationship data of the node graph through the API of the NodeGraphProcessor plug-in, where the node information is the serialized node object; Recursively traverse and export from the root node of the editor node graph, read the node connection relationship data on the editor, and finally export the structure description of the entire Lua behavior tree to generate the corresponding Lua behavior tree configuration file.

7. A Lua behavior tree editor implementation system based on the Unity engine, characterized in that: The system is used to implement the Lua behavior tree editor implementation method based on the Unity engine as described in any one of claims 1 to 6, comprising an editor custom editable node construction module, an editor value node construction module, a Lua behavior tree node graph creation and editing module, and a Lua behavior tree configuration file export module; Editor custom editable node building module, used to export the Lua implementation script of the Lua behavior tree node to the corresponding editor node definition class C# file, and implement the editor custom editable node through the editor node definition class C# file of the Unity engine's NodeGraphProcessor plug-in; Editor value node building module, used to introduce value nodes into Lua behavior trees, export value nodes in Lua implementation scripts to corresponding editor C# files, and implement the application of editor value nodes through the NodeGraphProcessor plug-in of the Unity engine; Lua behavior tree node graph creation and editing module, used to create and edit the editor's Lua behavior tree node graph based on the NodeGraphProcessor plugin; Lua behavior tree configuration file export module, used to export the editor's Lua behavior tree configuration file based on the NodeGraphProcessor plugin.

8. An electronic device, comprising: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the method for implementing a Lua behavior tree editor based on the Unity engine as described in any one of claims 1 to 6 when executing the computer program.

9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method for implementing a Lua behavior tree editor based on the Unity engine as claimed in any one of claims 1 to 6 is implemented.

Citation Information

Patent Citations

  • Intelligent agent behavior tree generation method and system based on reinforcement learning and genetic algorithm

    CN116932535A