Scripting with LuaPlus and Cpp

简介:

At some point you would probably like to add some scripting to your game.

For example, think of particle emitters or characters. Everytime you change something, you have to recompile. With decent sized projects this could mean making a cup of tea everytime :). With some kind of scripting you could simply change your script and run your game immediately again. The script has changed, and therefore so has the character, particle emitter, AI, etc.

Scripting is a hot item these days. For example, a game I still like quite a lot -Unreal Tournament- consists for a large part of "UnrealScript." This script controls all kinds of objects throughout the game. Elevators, switches, AI, etc.

Here is a small excerpt from UnrealScript:

None.gifsimulated final function RandSpin( float spinRate)
ExpandedBlockStart.gif   {
InBlock.gif      DesiredRotation = RotRand();
InBlock.gif      RotationRate.Yaw = spinRate * 2 *FRand() - spinRate;
InBlock.gif      RotationRate.Pitch = spinRate * 2 *FRand() - spinRate;
InBlock.gif      RotationRate.Roll = spinRate * 2 *FRand() - spinRate;    
ExpandedBlockEnd.gif  }

None.gif

Looks quite like normal C++ code, doesn't it?


Lua

I was reading through some programming books and read about Lua. Lua is a completely free scripting language. Even for commercial use. Basically the only thing you have to do is to mention it in a readme, and if you want to be really cool, add the Lua logo to your game.

Lua is based on ANSI C. It should compile on any platform. This means that if you ever try to port your game to linux, you don't need to change scripts. Also, Lua can be compiled or just "plain text". If your code (Script) is plain text, Lua will compile it on the fly when you load it in your game.

If you like to see compile errors, and have a compiled script, simply use the command line utility. You do not have to change any code to load "text" or binary Lua files! So, if you're done developping: Compile the Lua files and replace the text versions, and it should work just fine.

The main site of Lua is www.lua.org. Check it out for some more details.


The Language

Lua looks a bit like all kinds of languages: A mix of VB/C++. Loosely typed, "and" instead of "&&", etc. For a good language reference you might find some tutorials online. Or maybe get yourself a nice book about it. Right now I have less than half a day of experience with Lua, so I don't know everything yet either :).

Here is a small example what I'm using as a test script, called test.lua:

None.gifhealth = 100;
None.gif 
None.gif  PrintNumber(30);
None.gif 
None.gif function Add(x, y) 
None.gif       return math.cos(x); 
None.gif end
None.gif 
None.gif -- function Update(x,y)
None.gif --       return x + y;
None.gif -- end 
None.gif

LuaPlus

There is one thing I forgot to mention. I'm using an alternative version of Lua, called LuaPlus. This version is modified for better support in (V)C++. It can be used in Managed C environments too. It also comes with a debugger and a "QuickWatch" viewer. I haven't tried those yet, though.

Download the source and the binaries. Include the binary ".lib" files in your project (where you add the DirectX or SDL .libs to normally). You also have to include some files from the LuaPlus source directory. Check out the samples that come with LuaPlus to get it working!

LuaPlus is quite a bit more convenient than Lua 'normal'. There is a tutorial about Lua on Gamedev.net.

None.gifhealth = 100;

This means that a global variable "health", with a value of 100 is created immediatly when the script is loaded.

None.gifPrintNumber(30);

PrintNumber is a function I have in my C++ program, the code actually calls my C++ function from the script. This opens many possibilites AddBot("John"), "CreateExplosion(10,20)", etc. This is Lua -> C++.

Next, I also wanted to try C++ -> Lua. This could be great for binding your script to events. Also notice the "math.cos". You can use math in your scripts as well. There's even IO stream support, etc.

I believe you can actually call objects too. I haven't tried that yet, but I probably will later.


The Interface between C++ And Lua(Plus)

C++ and Lua are two different environments. How are those ever going to talk to each other? Internally Lua works with stacks (pushes, pops, should be a bit familiar, I assume). I believe that you have to take care of all the stack push/pops yourself in normal Lua.

With LuaPlus it's a bit different. It's close to normal C++. We'll put the code above to use in our C++ program.

I made a seperate .h file to test around with LuaPlus. I will paste the code here first and then explain it step by step.

None.gif#pragma once
None.gif  #include "LuaPlus.h"
None.gif   using  namespace LuaPlus;
ExpandedBlockStart.gif   /* Lua test code
InBlock.gif  
ExpandedBlockEnd.gif  
*/

None.gif 
None.gif   static  int LS_PrintNumber(LuaState* state)
ExpandedBlockStart.gif   {
InBlock.gif      LuaStack args(state);
InBlock.gif  
InBlock.gif      // Verify it is a number and print it.
ExpandedSubBlockStart.gif
        if (args[1].IsNumber()) {
InBlock.gif          Log << args[1].GetNumber();
InBlock.gif          printf("%f\n", args[1].GetNumber());
ExpandedSubBlockEnd.gif        }

InBlock.gif      // No return values.
InBlock.gif
      return 0;
ExpandedBlockEnd.gif  }

None.gif  
None.gif  
None.gif   void test();
None.gif  
None.gif   void test() 
ExpandedBlockStart.gif   {
InBlock.gif      //Create state
InBlock.gif
      LuaStateOwner state;
InBlock.gif      
InBlock.gif      //With this the script can access our own C++ functions:
InBlock.gif
      state->GetGlobals().Register("PrintNumber", LS_PrintNumber);
InBlock.gif  
InBlock.gif      //Open test file:
InBlock.gif
      int iret = state->DoFile("test.lua");
InBlock.gif      
InBlock.gif      //Get a global variable:
InBlock.gif
      LuaObject sObj = state->GetGlobal("health");
InBlock.gif      int mytest = sObj.GetInteger();
InBlock.gif      
InBlock.gif      //Update the value:
InBlock.gif
      sObj.AssignInteger(state, 30);
InBlock.gif      //Get value again:
InBlock.gif
       mytest = sObj.GetInteger();
InBlock.gif  
InBlock.gif      //Call a function in lua:
InBlock.gif
      LuaFunction<float> Add =  state->GetGlobal("Add");
InBlock.gif      float myret = Add(3.14f,0.0f);
InBlock.gif      
ExpandedBlockEnd.gif  }

None.gif

I have the habit of simply setting breakpoints in my code, and check out the value. Hence I don't 'print' much.

At the top of my code you will see the required declarations needed for the Lua file. Then follows a custom Print function, found in one of the LuaPlus examples. Then the last "Test" function is my test setup.


States

States are individual scripts, or environments. Every seperate script runs in its own state. If you want to run four scripts, you can't do that with one state, unless you unload it every time. Loading takes time, so you would want to use multiple states. (That's how I understood it from the docs)

None.gif // Create state
None.gif
      LuaStateOwner state;
None.gif      
None.gif       // With this the script can access our own C++ functions:
None.gif
      state->GetGlobals().Register("PrintNumber", LS_PrintNumber);

The above code creates a state variable, and registers my earlier mentioned LS_PrintNumber function.

The name it's registered as is "PrintNumber". I do this before loading the acutal script, since PrintNumber is called immediately (not within a function) from the Lua script. As seen above in my Lua code.

None.gif // Open test file:
None.gif
       int iret = state->DoFile("test.lua");

This code simply opens (and if needed, compiles) the lua code. If it doesn't load because of errors, it will return an error code. 0 is OK. The rest is an error. You can lookup the table with errors from the Lua site. It's like 4-5 kinds of errors. The compiler returns way more descriptive errors, hence it's always useful to see why your code doesn't compile using this command line program:

None.gifE:\LuaPlus\Bin>luaplusc "E:\src\test.lua"
None.gif  luaplusc: E:\src\test.lua:6: unexpected symbol near `/'
None.gif
Note that there are multiple ways to run a script. You can also use DoString(string) to run a script from a string you loaded somewhere. (or generated using an interface, etc.)

 

None.gif // Get a global variable:
None.gif
      LuaObject sObj = state->GetGlobal("health");
None.gif       int mytest = sObj.GetInteger();
None.gif

Here we get our global health variable, which we declared at the first line, remember? LuaPlus has all kinds of functions: GetInteger, GetString, GetDouble, GetFloat, etc. to get the right type of variable.

Also notice the "LuaObject" we are using to get the value. We now have a reference to the "health" variable.

Which also means we can update it:

None.gif // Update the value:
None.gif
      sObj.AssignInteger(state, 30);
None.gif       // Get value again:
None.gif
       mytest = sObj.GetInteger();
None.gif

"Health" is now 30. Again, many functions: AssignFloat, AssignString, etc. The auto complete list (In VC++) that pops up when you type the '.' is quite straight forward.

None.gif // Call a function in lua:
None.gif
      LuaFunction< float> Add =  state->GetGlobal("Add");
None.gif       float myret = Add(3.14f,0.0f);
None.gif

This calls our "Add" function in Lua, from our C++ code. Pretty nice, first get a pointer to the function and then call the function. Notice my Add function does not do an add. It returns the cosinus, something I wanted to test.

Also notice the "float". This could be an integer, double, etc. Depending on your function in the Lua code. I think it's quite obvious.


Final words

I hope this simple introduction to Lua helps someone starting with Lua. I'm a complete starter right and are simply writing this to remember it myself after my Uni tests soon :).

目录
相关文章
|
C语言 Android开发 Windows
解决windows下eclipse创建project时没有include导致出现“unresolved inclusion: &lt;stdio.h&gt;”错误的方法
解决windows下eclipse创建project时没有include导致出现“unresolved inclusion: &lt;stdio.h&gt;”错误的方法
解决windows下eclipse创建project时没有include导致出现“unresolved inclusion: &lt;stdio.h&gt;”错误的方法
|
5月前
|
Shell Ruby Perl
don‘t have write permissions for the /System/Library/Frameworks/Ruby.framework
don‘t have write permissions for the /System/Library/Frameworks/Ruby.framework
227 0
|
C++
C++Qt | 无法打开源文件:“x64\Debug\moc_xxx.cpp”怎么办。
编译时报错: 1>c1xx : fatal error C1083: 无法打开源文件:“x64\Debug\Res\src\Vlc\include\moc_AVPlayer.cpp”: No such file or directory的解决办法。
446 0
|
C# C++
C#的new和CPP的new
CSharp的new语句: Bus myBus = new Bus(2, 3, (float)250000.0); CPP的new语句: Bus* myBus = new Bus(2,3,250000.0); 本质上这两条语句是一样的,都是使用new来申请分配内存。
942 0