Lua game development application tutorial

Source: Internet
Author: User

LuaInGamesIn-development applications are the content to be introduced in this article, mainly for understanding and learningLuaMediumGamesDevelopment, implementation of specific content, refer to the detailed description of this article.

Previous days have been busy developing BigTank projects (see poor design site: http://www.buaa-mstc.com, does not support IE), summed up someLuaThe Application Method in the C # project.

LuaIs a small scripting language. The author is Brazilian. The language is designed to be embedded into an application and provide flexible scalability and customization for the application. Its home page is www.lua.org.
The Lua script can be easily called by C/C ++ code, or the C/C ++ function can be called in turn, which makesLuaIt can be widely used in applications. Not only as an extension script, but also as a common configuration file, instead of XML, Ini and other file formats, and easier to understand and maintain.

Using Lua in C # is also very simple.

 
 
  1. LuaInterface is a library for integration between the Lua language and Microsoft   
  2. .NET platform’s Common Language Runtime (CLR). Lua scripts can use it to instantiate CLR objects,   
  3. access properties, call methods, and even handle events with Lua functions. 

You can download this library from the LuaInterface website. You can start by referencing LuaInterface. dll in your project.

The BigTank project is not yet determined whether it is open-source, so I use my own pet program to demonstrate that it also uses Lua, and you can find all its source code on the lab page ).

 
 
  1. C #:
  2.  
  3. //...
  4. /// <Summary>
  5. /// Lua Virtual Machine
  6. /// </Summary>
  7. Private static Lua luaVM = null;
  8. /// <Summary>
  9. /// Pet Constructor
  10. /// </Summary>
  11. Public Pet (PetForm _ petForm, string _ petName, string _ petPath)
  12. {
  13. PetState = new PetState ();
  14. PetForm = _ petForm;
  15. PetName = _ petName;
  16. // Construct a Lua Virtual Machine to parse pet AI
  17. LuaVM = new Lua ();
  18. // Register the API functions provided to pet AI
  19. Type tThis = this. GetType ();
  20. LuaVM. RegisterFunction ("PetDo", this, tThis. GetMethod ("LuaPetDo "));
  21. LuaVM. RegisterFunction ("PetDoFrame", this, tThis. GetMethod ("LuaPetDoFrame "));
  22. LuaVM. RegisterFunction ("Sleep", this, tThis. GetMethod ("LuaSleep "));
  23. // Load the aifile
  24. LuaVM. DoFile (System. AppDomain. CurrentDomain. BaseDirectory + _ petPath + "\ ai. lua ");
  25. }

RegisterFunction registers a public function in the C # code (the latest version of LuaInterface supports private) for the Lua script. You do not need to care about the number and type of parameters.

 
 
  1. Lua:  
  2. PetDo("Sleep"); 

After DoFile is executed, the Lua script is called, and the latter calls the PetDo function in C # to complete the specified action.

Summary:LuaInGamesThe content of the application tutorial in development is complete. I hope this article will help you!

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.