Apply Lua in your game: Run the interpreter in your game code

xiaoxiao2021-03-06  41

Often, you want to read some information at your game to configure your game, which usually put it in a text file, when you start, you need to open this file, then resolve String, find the information you want.

Yes, maybe you think this is enough, why should I use Lua?

Applications to "Configuration", Lua provides you more powerful, more flexible expression, in the previous way, you can't configure your game according to certain conditions, Lua provides you with flexible expression Way, you can configure your game similar to this:

IF player: is_dead () thendo_something () Elsedo_ELSE () end

More importantly, after you have made some modifications, you don't need to recompile your game code at all.

Usually, you don't need a separate interpreter in the game, you need to run the interpreter in the game, let's take a look, how to run the interpreter in your code:

// This is the three header files required for Lua //. Of course, you need to link to the correct lib # clude "lua.h" #include "lautelib.h" #include "lualib.h"

INT Main (int Argc, char * argv []) {Lua_State * L = Lua_Open (); Luaopen_Base (L); LuaOpen_IO (L);

Const char * buf = "Print ('Hello, World!');

Lua_Dostring (BUF);

Lua_Close (L);

Return 0;}

Program output: Hello, World!

Sometimes you need to perform a string, sometimes you may need to execute a file, when you need a file, you can do this: Lua_Dofile (L, "Test.lua");

Look, very simple.

转载请注明原文地址:https://www.9cbs.com/read-68762.html

New Post(0)