Python scripts solve difficulties in Game Development

Source: Internet
Author: User

Python scripts are a communication language widely used in game development. Many problems have plagued developers in practical application, the following is a solution to the specific problem of the Python script in practical application. I hope to read the following article to help you.

In some early games, most of the game logic was directly written into the game code, such as calculation formulas and game processes. However, with the continuous development of the game industry, game development itself has become more and more complex. Game planning requires more time to adjust the game. If the game logic is still written in the code, it is planned that every modification to the game should be carried out through the program, and the program needs to be re-compiled and restarted, which greatly reduces the work efficiency.

  • Introduction to the Python script language
  • Program editing using the Python script language
  • Comparison between Python script programs and Per
  • Brief introduction to the basic application method of calling Python scripts
  • Python scripts solve difficulties in Game Development

Using the Python script in the game can solve the above problems, from the calculation formula to the control process of the game to the Python script, in addition, most of the current script systems are interpreted and executed, so they can be dynamically modified at runtime, so that you can immediately see the modification results, which is very convenient.

How to use scripts

There are two main script methods. One way is that the main program uses a high-level language, such as C ++, to write it, and then embed a script interpreter to dynamically execute some script functions at runtime; another way is to write the entire program using scripts. For example, some mud games are directly written using LPC scripts.

This article mainly studies the use of embedded Python scripts, because most of the scripts currently do not provide a convenient debugging environment such as VC ++. If all the programs are written in scripts, debugging becomes very painful when the script contains tens of thousands or even tens of thousands of lines. In addition, some very time-consuming code can be written in C ++ for Embedded use to ensure better running efficiency.

Starts from the main () function of C ++, and then enters the main loop. In some C ++ functions, the script function is called directly. during the running of the script function, it is also possible to call C ++ extension functions. C ++ has two main functions: one is to add functions that cannot be directly written by Python scripts, and the other is to replace functions that are too slow to run in scripts.

The key aspect of the above process is how C ++ and scripts call functions and how parameters and results are transmitted. The general solution is to register C ++ extension functions with the script API when the program starts, and pass the function pointer to the script system for future calls, when a script function is called, the API of the script system is used to compress the call parameters into the stack and obtain the running result through the API.

Python script Introduction

Currently, many third-party scripting languages are available for direct use, such as Tcl and Lua. This article describes Python scripts. Python has more than a decade of history. It is an explanatory and object-oriented scripting language. Python interpreters can run on most operating systems, such as Windows, Linux, Solaris, and Mac.

1. installation and configuration

After the installation is complete, the Python Graphical Editor (IDLE, but Chinese characters are not supported in the current version), Python command line interpreter, and user manual will be displayed in the Start Menu.

To call the Python API function in a C ++ program, you need to add the header file and lib path to the search directory of VC ++, the header file path is the include directory under the Local Python installation directory, and the lib path is the libs directory under the Local Python installation directory. Note that the installation package only provides the lib and dll of the release version. If debugging is required, you must download the Python source code to compile the lib and dll of the debug version.

2. Syntax Introduction

For detailed syntax instructions, refer to the documentation that comes with the Python installation package. Here I will only introduce some common keywords and precautions.

The Python script does not contain {And} in C ++. It is replaced by indentation. Variables do not need to be declared separately, but cannot reference unassigned variables.

The Python script introduces the concept of a module, similar to the concept of Lipary in C ++. A module can contain functions, variables, and classes. A script file is a module that needs to be imported before use.

Python does not have a switch. if is used instead.

 
 
  1. if ( num==1 ):  
  2. print "1"  
  3. elif ( num==2 ):  
  4. print "2"  
  5. else:  
  6. print "unknown  

"While is a loop Statement of Python. In the while LOOP, you can use continue to jump to the next loop, and use peak to jump out of the entire loop.

 
 
  1. Cnt=5 
  2. While (cnt>0 ):
  3. Print cnt
  4. Cnt-=1 
  5. For Loop:
  6. List= ["Test1", "test2", "test3"]
  7. For str in list:
  8. Print str

A dictionary is a type of ing data for Python scripts. It can be mapped from a key to the actual content (value ):

 
 
  1. accounts = {'tom':'123456', 'mike':'654321'}  
  2. print accounts['tom']  
  3. print accounts['mike'] 

3. API Introduction

The Pyhon script provides a large number of C APIs. The interaction between C ++ and Python is carried out through these Apis. The following describes several important API functions: void Py_Initialize () must be initialized using Py_Initialize before using the Python system. It loads Python built-in modules and adds the system path to the module search path.

This function has no return value. You need to use Py_IsInitialized to check whether the system initialization is successful. Int PyRun_SimpleString (char * command) runs the input string directly as Python code. If the return value is 0, the input string is successful. If the return value is-1, the input string is successful. Most errors are caused by syntax errors in strings. PyObject * Py_BuildValue (char * format,...) converts the C ++ variable into a Python object.

This function is used when a variable needs to be passed from C ++ to Python. This function is a bit similar to C's printf, but the format is different. The commonly used formats include "s", "I", and "f", which indicate a string. "O" indicates a Python object. PyObject * PyObject_CallObject (PyObject * callable_object, PyObject * args) calls a Python function pointed to by callable_object. args is the call parameter.

Before using this function, you can use PyCallable_Check to check whether callable_object is a callable Python object. PyObject * PyImport_Import (PyObject * name) loads a module specified by n a m e. You can use PyString_FromString to convert the module name to a Python object, and then use PyImport_Import to load the module. Void Py_Finalize () disables the Python system. This function is generally called when the program exits.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.