Starting from the game scripting language, this article analyzes the script basics set up by Mono and the script language mono.

Source: Internet
Author: User

Starting from the game scripting language, this article analyzes the script basics set up by Mono and the script language mono.
0x00 Preface

In my daily work, I occasionally encounter the following question: "Why have game scripts become indispensable in current game development ?". So this week I wrote an article about the game script and analyzed why the game script appeared, and what script base does mono provide. Finally, the Mono runtime will be embedded into an unmanaged (C/C ++) program by simulating the Script Function in the Unity3D game engine, separate the scripting language from the engine.

0x01 Why? Starting from why gaming scripts are required

First, let's talk about why game development requires the use of game scripts.

Why do we need a script system? Why does the script system appear? In fact, game scripts are not a new term or technology. As early as Blizzard's World of Warcraft began to get popular, people were familiar with a script language called Lua. At that time, many online games used Lua as a scripting language, such as Netease's westward journey series.
However, in the era of standalone games, we seldom hear about script technology. Why? Since the current hardware level was not high, we needed to use languages such as C/C ++ to squeeze the performance of hardware as much as possible. At the same time, the upgrading of standalone Games was not as fast as that of online games, therefore, the development time and version iteration speed are not the first factors to consider. Therefore, you can use a language with low development efficiency such as C/C ++ to develop games.

However, with the passage of time, the hardware level has increased year by year, and the demand for hardware performance is no longer urgent. On the contrary, the rise of online games has put forward higher requirements for development speed and version change. Therefore, C/C ++, Which is inefficient in development and highly risky in investment, will no longer meet market demands. The more practical problem is that ,. net, javascript, and other languages are popular. programmers can select more and more languages, which leads to a smaller proportion of excellent C/C ++ programmers. As the online gaming market continues to expand, this demand for talent is also growing, which leads to a large number of talent vacancies, in turn, it increases the cost of using C/C ++ to develop games. Since C/C ++ is a language that is easy to learn and difficult to learn, the high risk of its advanced features and high flexibility is also caused by the use of C/C ++ for development in each project, problems that have to be considered.

One way to solve this problem is to use scripts in the game. It can be said that the emergence of game scripts not only solves the development efficiency problems caused by the difficulty of C/C ++ proficiency, it also reduces the risk and cost of using C/C ++ for development. Since then, script and game development complement each other and promote each other, which has gradually become an indispensable part of game development.

In today's mobile games era, the market demand has become larger and more frequent. This requires a scripting language to improve project development efficiency and reduce project costs.
What are the specific advantages of game scripts?

Therefore, many game engines, including Unity3D, provide script interfaces, allowing developers to get rid of C/C ++ during project development (note: unity3D itself is written in C/C ++. In fact, it reduces the barrier of Game Development in disguise and attracts many independent developers and game producers.

0x02 What? Script mechanism provided by Mono

The first question is: what is Mono?

Mono is an open-source project sponsored by Xamarin. Based on the ECMA standard (Ecma-335, Ecam-334) of the Common Language architecture (CLI) and C #, it provides another Implementation of the Microsoft. Net Framework. And Microsoft's. the Net Framework is different that Mono has the cross-platform capability, that is, it can not only run on Windows systems, but also on Mac OSX, Linux, and even some game platforms.

Therefore, using it as a cross-platform solution is a good choice for developing game engines for cross-platform games such as Unity3D. But how does Mono provide this script function?

If you need to use Mono to provide the Script Function for application development, one of the prerequisites is to embed Mono runtime into the application, only in this way can managed code and scripts be used in native applications. Therefore, we can find out how important it is to embed Mono runtime into the application. However, before discussing how to embed Mono runtime into Native applications, we should first understand how Mono provides the Script Function and what script mechanism Mono provides.

Mono and script

This section will discuss how to use Mono to improve our development efficiency and scalability without having to re-write the written C/C ++ code with C, that is, how Mono provides the script function.

It is common to use a programming language to develop games. Therefore, game developers often need to choose between high-efficiency low-level languages and low-efficiency high-level languages. For example, the structure of an application developed using C/C ++ is as follows:

We can see that low-level languages and hardware are more direct, so they are more efficient.

It can be seen that the advanced language does not directly deal with hardware, so its efficiency is low.
If we use speed as the language standard, the general ranking of language from low level to advanced level is as follows:

  • Assembly Language
  • C/C ++, compiled static insecure Language
  • C #, Java, compiled static security Language
  • Python, Perl, Javascript, interpreted Dynamic Security Language

Developers are faced with many real-world problems when choosing the appropriate development language.

Advanced languages are more efficient and easy to master for developers. However, advanced languages do not have the same running speed as low-level languages, and even have higher hardware requirements, to some extent, this determines whether a project is successful or failed.

Therefore, how to balance the two, or how to integrate the advantages of the two, becomes very important and urgent. The script mechanism came into being at this time. Game engines are developed by experienced developers using C/C ++, while functions in some specific projects, such as UI and interaction, are developed in advanced languages.

By using advanced scripting language, developers can combine the advantages of low-level languages and advanced languages. At the same time, the development efficiency is improved. As mentioned in section 1, after the script mechanism is introduced, the development efficiency is improved and the prototype can be developed quickly, instead of wasting a lot of time on C/C ++.

The scripting language also provides a secure sandbox development mode. That is to say, developers do not have to worry about the specific implementation details in the C/C ++ development engine, you do not need to pay attention to the details of such things as resource management and memory management, which greatly simplifies the application development process.

Mono provides the possibility of implementing this script mechanism. This allows developers to use JIT-compiled code as a scripting language to expand their applications.

Currently, many scripting languages tend to be interpreted, such as javascript used by cocos2d-js. Therefore, the efficiency cannot be compared with that of the native code. Mono provides a way to compile the script language into native code through JIT, improving the efficiency of the script language. For example, Mono provides a native code generator to make your application run as efficiently as possible. At the same time, it provides many convenient interfaces for calling native code.

When providing a script mechanism for an application, it is often necessary to interact with low-level languages. Therefore, we have to mention the necessity to embed Mono runtime into the application. Next, I will discuss how to embed the Mono runtime into the application.

Mono runtime embedding

Since we have clarified the importance of embedding a Mono runtime application, how to embed it into the application becomes the next topic worth discussing.

In this section, I will analyze how the Mono Runtime is embedded into the application and how to call the hosting method in the native code, how to call native methods in managed code. As we all know, the Unity3D Game Engine itself is written in C/C ++, so this section takes the Unity3D game engine as an example, assume that we have an application written in C/C ++ (Unity3D ).

After you embed your Mono runtime into this application, our application gets a complete virtual machine runtime environment. In this step, you need to link "libmono" to the application. Once the link is complete, the address space of your C ++ application will look like the following:

In C/C ++ code, we need to initialize the Mono runtime. Once the Mono runtime Initialization is successful, the most important next step is to load the template/. NET code. The loaded address space is shown in:

The C/C ++ code is usually called unmanaged code, while the code generated by the pencil compiler is usually called managed code.
Therefore, embedding Mono runtime in our application can be divided into three steps:

Let's proceed step by step. First, we need to compile the C ++ program and link it to the Mono runtime. In this case, we will use the pkg-config tool. Use homebrew for installation on Mac, enter the command "brew install pkgconfig" in the terminal, you can see that the terminal will have the following output content:

==> Downloading https://homebrew.bintray.com/bottles/pkg-config-0.28.mavericks.bottle.2.tar.gz######################################################################## 100.0%==> Pouring pkg-config-0.28.mavericks.bottle.2.tar.gz

Related Article

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.