Pseudocode documentation
What is pseudocode? What is a pseudocode system?
Broadly speaking, pseudocode is fake code, but it is often used to describe the workflow of a certain job, so often pseudocode is process-oriented. Of course, everyone's pseudocode habits/abilities are different, and they may also be object-oriented.
But the pseudocode system here is procedure-oriented; It is also aimed at planners, so the syntax of this pseudocode system is very simple: basically it consists of fixed-format XXX method names (method parameter lists). (Please see the documentation for specific syntax details).
The significance of the existence of this pseudocode is: to prepare for the planning to flexibly configure a conditional judgment and process
For example, a character's skill release process goes like this: unleash a fireball in front of the character and fly towards the enemy, dealing M point damage to the enemy, and apply a negative effect to the enemy ----- burn N seconds, dealing P point damage per second.
Then the pseudocode of the planning configuration can be filled in the Excel table like this:
"Character unleashes projectile-type spells (create fireballs (), get current enemies (), calculate skill damage (), create burning negative effects (N, P))"
Thereinto:
Character unleashes projectile-like spells / creates fireballs / acquires current enemies / calculates skill damage / creates burning negative effects These are all methods in pseudocode, abbreviated: pseudo-methods.
N, P may be constant, so fill in the numbers directly.
M is not a constant, but is determined according to the character attribute of the released skill and the skill itself, so it needs to be calculated, and the pseudocode called: Calculate skill damage() method to obtain the calculated value.
Create Fireball () Similar methods can be: Create Ice Crystal () / Create Water Ball () / Create Energy Ball () etc
Get the current enemy () Similar methods can be: randomly surrounding an enemy () / random a friend () / self () etc
Create a Burn Negative Effect () There are many similar methods: Create Curse Negative Effect () / Create Blood Return Positive Effect () etc
The more similar approaches, the richer the combination and the more flexibility!
So after planning to write such a process code, how to make this code work? How do you make pseudocode real code?
The answer is: the programmer needs to complete all the "true" methods corresponding to the "pseudo" methods so that the pseudocode can be run
Pseudo-method: Character release projectile-like spells (spell missile entity object, attacked object, damage value, negative effect) --- Programmers implement a function corresponding to the method name in C#, this function receives parameters as: spell missile entity object, attacked object, damage value, negative effect. The function of this function is to call the received parameters to complete: [Spell Missile Entity Object] Fly to [Attacked Object] animation, when the missile hits the attacked object, it causes [Damage Value] points to [Attacked Object], and applies [Negative Effect].
Pseudo-method: Create Fireball() --- Programmers implement a function in C# corresponding to the method name, this function is used to complete: Call resources to generate fireball objects in the game.
Why not let the programmer implement all the skills directly according to the planning requirements? Isn't it redundant to still need to plan to write pseudocode? When the number of skills is large, such as more than a hundred, the advantages of using pseudocode are revealed. Programmers do not need to implement them one by one according to the requirements of these more than 100 skills, but skill planning is flexibly designed according to the interface (that is, pseudo-method) provided by the programmer. Skill planning is similar to playing a jigsaw puzzle to complete different skills.
Workflow reference process: planning first out of some requirements, programmers according to these requirements to split into pseudo-methods and implement in C#, and then provide pseudo-methods and pseudo-method documents to the planning, planning and then use these pseudo-methods according to the pseudo-code syntax to write and combine into a new process, during which programmers may need to constantly supplement new pseudo-methods, with more and more pseudo-methods, pseudo-code The space to play is getting bigger and bigger, and the effects that can be written are getting more and more flexible. Pseudo-method documentation can be generated automatically
Pseudocode is not only used in the skill system, for RPG games, the game's plot and tasks are almost essential, and there are a large number of conditional judgment and execution processes that need to be configured in the story and tasks, and the conditional judgment and execution process can be implemented using this pseudocode system. As long as the game needs to flexibly configure the conditional judgment and execution process, you can use the pseudocode system to achieve it
Pseudocode syntax:
Statement: with '; 'Spaced. Each statement consists of a pseudo-method that is the "root"
Pseudo-method: "Method Name (Method Parameter 0, Method Parameter 1, ...)", any spaces or line breaks or tabs before and after the method name or before and after the parameter will have no effect on the pseudo method execution
Method parameters: Pass in the parameters required for the method to run so that the method can be executed correctly. Each method parameter can be returned by a pseudo-method after it is run
Basic type: integer/float/boolean/string, corresponding to the int/float/bool/string type in C#. Integers in pseudocode will be parsed to int type by pseudocode parser, and decimals will be parsed to float type
Empty object: [NULL], used to indicate that an object of type is null
Code block methods: NewAction (pseudocode statement) and NewFunc (a pseudocode statement with a return value), the parameters of these two special methods are pseudocode statements, which encapsulate a pseudocode statement for a code block to provide other pseudo-methods that require code block parameters as parameters. For example, pseudo-methods that use logic control or loop control such as IF() or While().
Pseudocode editor:
Pseudocode can be filled in as a string or as a pseudocode object (PseudoCode object). PseudoCode objects are easier to edit in UnityEditor. If it is to be configured in an Excel table, it can only be a pseudocode filled with strings.
PseudoCode objects can be edited directly in the UnityEditor Inspector, which is easier to write with the pseudocode method document window:
In addition, PseudoCode objects can be edited in the pseudocode editor window, which is recommended for writing, and sometimes it is necessary to write in two ways together
Pseudocode method documentation window for planning:
Programmers implement pseudo-methods:
For pseudomethods to be executed, the programmer needs to implement its execution in C#. For example:
[PFC_HelpInfo("获取变量的值\n" + "Get the value of the variable.", typeof(object), "变量的值 (Value of variable)", // Return Info typeof(string), "变量的名字 (Name of variable)" )] [PFC_FindingPath("变量(Variable)")] public static void GetVar(PseudoCode pseudoCode, string funcName, Action<object> callback, object[] parameters, object[] commonParameters) { string varName = (string)parameters[0];
if (pseudoCode.vars.ContainsKey(varName)) callback?.Invoke(pseudoCode.vars[varName]); else { Debug.LogError($"不存在变量:{varName} ---- 调用方法:{funcName}"); } }
[PFC_HelpInfo("删除变量, 并返回变量的值\n" + "Delete a variable and return its value.", typeof(object), "变量的值 (Value of variable)", // Return Info typeof(string), "变量的名字 (Name of variable)" )] [PFC_FindingPath("变量(Variable)")] public static void RemoveVar(PseudoCode pseudoCode, string funcName, Action<object> callback, object[] parameters, object[] commonParameters) { string varName = (string)parameters[0];
var res = pseudoCode.vars[varName]; pseudoCode.vars.Remove(varName); callback?.Invoke(res); }
All pseudo-method implementations are in this format:
Public static void PseudoCode pseudoCode, string funcName, Action<object > callback, object[] parameters, object[] commonParameters) {
Pseudo-method gets parameters
Pseudo-method procedure code
Pseudo-method return
}
Pseudo-method gets parameters:
Pseudo-methods usually have two sources of parameters, one is whether the value specified in the pseudocode is a base type or returned by other pseudocode; The other is to pass in parameters from outside the pseudocode, which are called public domain parameters, because all pseudo-methods in the same pseudocode share the same common domain parameters. Note that the concept of "public domain parameters" is important, two pseudo-methods that require different public domain parameters often cannot coexist in a pseudocode. If the user uses it incorrectly, Unexpected operational error !!! often occur
In a pseudo-method, the incoming public domain parameters are the same series, but because different pseudo-methods may use public domain parameters in different ways: some pseudo-methods do not need any public domain parameters, and some only use a part of the passed public parameters, which means that as long as the pseudo-methods that can not obtain or correctly obtain the parameters they need from the public domain parameters can coexist in the same pseudo-code. Therefore, in order to improve the commonality of pseudo-methods, it is often necessary to clearly specify which values can be obtained in public domain parameters before implementing pseudo-methods
The pseudo-method parameters are passed in by object[] parameters , and what parameters are passed in depends on how the pseudocode is written; The public domain parameter is passed in by object[] commonParameters, and the content of the public domain parameter is passed in by calling pseudocode
PseudoCode pseudoCode, string funcName These two parameters will not be used by most pseudo-methods and can be ignored for the time being
Pseudo-method procedure code
The process code is the embodiment of what the pseudo-method does.
Pseudo-method return
All pseudo-methods need to be returned by calling callback after their own affairs are processed, and callback must be called whether this pseudo-method has a return value or not.
Pseudo-method-related attributes
PFClass (same as: PseudocodeFuncClass) is marked as a class that indicates that static methods in its class can be added to the Pseudocode system Provide pseudo-methods
PFC_Ignore (same:PseudocodeFuncClass_Ignore ) Flagged methods are ignored by the Pseudocode system and do not provide pseudo-methods
PFC_FindingPath specifies the seek path for a pseudomethod or class in the Select Method Name window, and if both the pseudomethod and the class it is in are marked with PFC_FindingPath the attribute marked by the pseudo-method takes precedence
PFC_CommonParameterField specifies a public domain parameter string for a pseudo-method or class, and if both the pseudo-method and the class in which it resides are marked with PFC_CommonParameterField, the attributes marked by the pseudo-method take precedence
PFC_HelpInfo Specify help information for pseudo-methods: comments for pseudo-methods, return information, information for individual parameters
Note: All of the above features can be disused, but can effectively help pseudocode editors if used well
APIs for programmers:
PseudocodeHelper. AddPseudocodeFuncs usually uses the PFClass feature to add pseudocode classes to the pseudocode system, and pseudo-methods in pseudocode classes marked by PFClass are automatically added by the pseudocode system at runtime. If PFClass is not used You can also manually add pseudo-method classes for the pseudocode system through this method
PseudocodeHelper. SetPseudocodeFuncs can also use this method to add pseudo-methods to the pseudocode system one by one
PseudocodeHelper. Run pseudocode exists in two forms, one exists as a string and the other exists as a PseudoCode object. To run pseudocode as a string, you need to use this method to run pseudocode. Pseudocode in the form of a PseudoCode object can be run directly by calling the PseudoCode.Run method
PseudocodeHelper. RunCode If the pseudocode is asynchronous and can wait for its execution in the coroutine, you can use this method: yield return PseudocodeHelper. RunCode(...)
Packaging settings:
Pseudocode implementation code is often not referenced in C#, and its calls are called through reflection by pseudocode runners. Unity's Managed Stripping Level should be as low as possible, and the code should not be clipped when packaging as much as possible.
Debugging:
When the pseudocode of the PseudoCode type is run, the public parameter field used for the last run will be saved, and the pseudocode of the PseudoCode type at this time will have an extra [Debug Run] button, after clicking this button, you can use the common parameter field used by the last run to run again, and print the running result in the Console, if an error is reported, it will also be displayed in the Console
Note: The [Debug Run] button appears only after the pseudocode is run.
Last updated