Document English
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.
The pseudocode system here is process-oriented, and the syntax is very simple.
PseudoCode was born to solve the flexibility of game matching tables, and can fill in the code anywhere you can fill in the string, and the syntax is simple and easy to use. Support mouse programming, native asynchronous execution, perfect integration with multi-threading, compatible with synchronous execution.
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 writing such a process code, how do you make this code work? How do you make pseudocode real code?
The answer is: all "true" methods corresponding to "pseudo" methods need to be completed so that the pseudocode can be run
Pseudo-method: Character release projectile-like spells (spell missile entity object, attacked object, damage value, negative effect) --- Implement the 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 implement all the skills directly in C#? 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 recommended by medium and large studios: Planning first out some requirements, programmers according to these needs to split into pseudo-methods and implement in C#, and then provide pseudo-methods and pseudo-method documents to the planning, plan 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 can play more and more space, The effects that can be written are also becoming more and more flexible. If the programmer implements the pseudo-method according to the PseudeCode specification, the documentation for the use of the pseudo-method can be automatically generated, and the document can be viewed with Unity open.
PseudoCode is especially suitable for individual game developers or small studios, often juggling multiple jobs by one person For example, the brother who writes the code may also have to undertake the work of gameplay design in addition to the work of writing the code itself, and then use PseudoCode to communicate with people too much about the purpose and use of the new pseudo-method. When it is necessary to add new personnel, pseudo-methods written using the PseudoCode specification can also easily present the purpose and use of pseudo-methods in the GUI document in Unity
Pseudocode is not only used in the skill system, for RPG games, the plot and tasks of the game are almost essential gameplay, and the plot and tasks, especially the tasks, will have a large number of conditional judgment and execution processes that need to be configured, and the conditional judgment and execution process can be implemented using this pseudocode system.
If you implement a user editing interface in the runtime for PseudoCode, you can even let players program in real time in the game, or make mods for the game, and make workshop works.
In game development, any place where flexible configuration of conditional judgment and execution processes is required, pseudocode systems can be used.
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 you want to configure in Excel sheets, you can only fill in pseudocode in the form of strings.
What is a PseudoCode object?
When a variable of type PseudoCode is defined in the MonoBehaviour or ScriptableObject subclass, then the variable is a PseudoCode object.
PseudoCode objects can be edited directly in the UnityEditor Inspector, and the document window is easier to write with pseudocode methods:
In addition, PseudoCode objects can be edited in the pseudocode editor window, which is recommended for writing, and sometimes it is necessary to cooperate with each other to write the two methods.
Click the + sign, or click the gear button on the right to pop up the menu, and select [Change To] from the pop-up menu. After the item, an interface similar to the one shown below will open, and clicking the pseudo-method button in the interface can be added or replaced with a pseudo-method in the pseudo-code.
Pseudocode Method Documentation Window:
To implement the pseudo-method:
For pseudomethods to be executed, the programmer needs to implement its execution in C#. For example:
All pseudo-method implementations are in this format:
[pseudo-method related features] public static void pseudo-method name (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. It is important to note that the concept of "public domain parameters" is important, two pseudo-methods with different requirements for public domain parameters often cannot coexist in a pseudocode, and users often have unexpected operation errors if they are not used correctly!!!
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, providing pseudo-methods.
PFC_Ignore (same as :PseudocodeFuncClass_Ignore ) Flagged methods are ignored by the Pseudocode system and do not provide pseudo-methods.
PFC_FindingPath Specify the seek path for a pseudomethod or class in the Select Method Name window, and if both the pseudomethod and the class in which it resides are marked with PFC_FindingPath the attribute marked by the pseudo-method takes precedence.
PFC_CommonParameterField specify 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 attribute of the pseudo-method token takes precedence.
PFC_HelpInfo Specify help information for pseudo-methods: comments for pseudo-methods, return information, information about individual parameters.
Note: All of the above features can be disused, but can effectively help pseudocode editors if used well
How pseudocode is called in C#:
The pseudocode is not designed to replace C#, but to complement C#. To get it to run, you need to make a call in C#.
According to the classification of the usage method, it can be divided into two types, one is to obtain or create the PseudoCode object, the method of using the PseudoCode object'. RunXXX' to call; The other is by using the static methods of the PseudoCodeHelper static class'. RunXXX' to call. The latter is essentially called internally, and in order to facilitate subsequent maintenance, it may no longer be maintained for the PseudoCodeHelper's calling method in the future, or even marked as obsolete.
The call mode is divided into four categories: callback mode call, coroutine mode call, asynchronous (multithreaded) mode call, synchronous call. (See: PseudocodeDemo.cs),
In the following introduction, the first letter of PseudoCode indicates the pseudocode class or pseudocode plugin, and the first letter lowercase represents the instance object of the PseudoCode class.
Callback mode
When called in callback mode, use 'pseudoCode.Run(... Action<object> callback...) ', this way does not enter other threads when it starts running, the call is in the main thread, it is executed on the main thread; If the call is in a child thread, it is executed in the child thread. This call method can get the final value returned by the pseudocode after the pseudocode runs in the callback. If the pseudocode is run without deferred processing, the last value returned is the value returned by synchronous execution.
Coroutine mode
PseudoCode supports calling the execution of pseudocode in a coroutine and waiting for it to finish executing. The call statement is as follows: 'yield return pseudoCode; ' or 'yield return pseudoCode.FindOrCreateChild(); ' , the difference between these two statements is that when the same pseudocode needs to be run at the same time, in order for them not to conflict, you need to create a child pseudocode object, using its FindOrCreateChild method to find or create a free child pseudocode object. When you need to get the return value of the pseudocode in the coroutine, you need to first stage the child pseudocode object as a variable, and after waiting for it to execute, use the GetRunResult method of the child pseudocode object to get it. If it is never possible to run multiple instances of this pseudocode in the game at the same time, you can directly use the 'yield return pseudoCode; ' statement.
Multithread mode
This way uses the statement 'await pseudoCode.RunTask<... > (...). ' to call, when calling pseudocode in this way, a Task will be immediately opened to run pseudocode, as for whether a new thread is opened is determined by the system class Task itself, most of the test process is opened a new thread, it often does not execute Task tasks in the busy main thread. This way you can get the pseudocode running result in the same way as synchronously: 'var result = await pseudoCode.RunTask ... ’
Synchronization mode
This way uses the statement 'var result = pseudoCode.RunSync.... ' to get the running result. This method does not need to wait, so it cannot get the results that need to wait. Therefore, this method is usually used to deal with pseudocode written by pseudo-methods that do not need to wait, and if pseudocode that needs to wait is used, then the desired running result is often not obtained
Waiting method
The usage of this method is the same as that of the multi-thread method. You can use await to wait for the execution of the pseudocode, and use the statement 'await pseudoCode.RunAwait<...>(...)' to call. This method is internally executed and called back Similarly, it will not actively open a new Task.
Usage of PseudocodeHelper:
PseudocodeHelper.Init Initialize pseudocode.
PseudocodeHelper.AddPseudocodeFuncs Pseudocode classes are usually added to pseudocode systems using the PFClass feature, and pseudo-methods in pseudocode classes marked by PFClass are automatically added by the pseudocode system at runtime. If you do not use PFClass, you can also manually add pseudo-method classes for the pseudocode system through this method.
PseudocodeHelper.SetPseudocodeFuncs You can also use this method to add pseudo-methods to the pseudocode system one by one.
PseudocodeHelper.Run Pseudocode exists in two forms, one as a string and one 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, this method can be used: yield return PseudocodeHelper.RunCode(...)
PseudocodeHelper.RunAsync Open a new Task to run pseudocode.
Note: Starting from version 1.3, it is necessary to initialize the PseudocodeHelper static class, because in order to improve the efficiency of pseudocode, a pseudocode delegate will be generated during its initialization process, which is relatively time-consuming, so it is recommended to put it uniformly in the initialization process. PseudocodeHelper's initialization method Init() is an asynchronous method, if you need to wait for it, you can use the await keyword. Using the PseudocodeHelper class for the first time will also trigger initialization, but if you run the pseudocode immediately after triggering, you will usually get an error. In addition, if you want to reduce the initialization time as much as possible, you can change the runtime mode of the pseudocode runMode = RunMode.Reflection or RunMode.Expression_Realtime before initialization. This means running the pseudomethod using reflection mode or also using expression mode, but it will generate a corresponding delegate for a pseudo-method the first time it is used
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 by the last run will be saved, and the pseudocode of the PseudoCode type will have an extra [Debug Run] button, which can be run again using the common parameter field used by the last run after clicking this button, and the running result will be printed in the Console, and 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