青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

牽著老婆滿街逛

嚴以律己,寬以待人. 三思而后行.
GMail/GTalk: yanglinbo#google.com;
MSN/Email: tx7do#yahoo.com.cn;
QQ: 3 0 3 3 9 6 9 2 0 .

Scripting with LuaPlus and Cpp

轉載自:http://gpwiki.org/index.php/Scripting_with_LuaPlus_and_Cpp

A small guide to Lua scripting

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:

simulated final function RandSpin(float spinRate)
  
{
      DesiredRotation 
= RotRand();
      RotationRate.Yaw 
= spinRate * 2 *FRand() - spinRate;
      RotationRate.Pitch 
= spinRate * 2 *FRand() - spinRate;
      RotationRate.Roll 
= spinRate * 2 *FRand() - spinRate;    
  }

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:

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

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.

health = 100;

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

PrintNumber(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.

#pragma once
  #include 
"LuaPlus.h"
  
using namespace LuaPlus;
  
/* Lua test code
  
  
*/

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

      
// No return values.
      return 0;
  }

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

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)

//Create state
      LuaStateOwner state;
      
      
//With this the script can access our own C++ functions:
      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.

//Open test file:
      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:

E:\LuaPlus\Bin>luaplusc "E:\src\test.lua"
  luaplusc: E:\src\test.lua:
6: unexpected symbol near `/'
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.)

 

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

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:

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

"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.

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

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 :).

 

External Links

posted on 2010-06-07 10:04 楊粼波 閱讀(1028) 評論(0)  編輯 收藏 引用


只有注冊用戶登錄后才能發(fā)表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美婷婷久久| 久久天天躁狠狠躁夜夜av| 久久av一区二区三区漫画| 久久精品国产99| 欧美精品18+| 久久激情久久| 在线观看成人小视频| 欧美福利网址| 一二三区精品| 一本色道久久综合狠狠躁篇怎么玩| 欧美影院一区| 亚洲第一黄色网| 99re成人精品视频| 国产精品视频网| 久久综合中文色婷婷| 日韩亚洲综合在线| 久久免费高清视频| 亚洲美女免费视频| 国产精品综合不卡av| 农村妇女精品| 亚洲专区一区| 亚洲国产精品免费| 亚洲国产高清一区二区三区| 欧美日本一区二区高清播放视频| 91久久极品少妇xxxxⅹ软件| 午夜国产不卡在线观看视频| 国产精品国产三级国产aⅴ无密码| 亚洲国产日韩在线一区模特| 国产一区二区三区成人欧美日韩在线观看 | 亚洲欧美激情精品一区二区| 韩国福利一区| 久久综合伊人77777蜜臀| 一二美女精品欧洲| 欧美黄色一区| 久久激五月天综合精品| 一本久久a久久精品亚洲| 欧美金8天国| 99精品国产高清一区二区| 亚洲一区二区三区四区视频| 精品电影在线观看| 欧美成人资源| 欧美一区亚洲二区| 亚洲视频二区| 亚洲精品一级| 亚洲黄色毛片| 蜜桃av久久久亚洲精品| 欧美一区二区在线免费观看| 国内精品免费在线观看| 毛片一区二区三区| 欧美呦呦网站| 亚洲欧美日韩在线| 在线视频日韩| 日韩亚洲精品在线| 亚洲精品视频免费在线观看| 欧美不卡视频一区| 一区二区日本视频| 91久久精品一区| 在线看片第一页欧美| 国产亚洲精品7777| 国产精品日韩在线| 国产精品v日韩精品| 欧美视频网址| 欧美小视频在线观看| 欧美日韩国产精品成人| 亚欧美中日韩视频| 亚洲欧美韩国| 亚洲激情另类| 亚洲国产婷婷香蕉久久久久久99 | 中文国产亚洲喷潮| 亚洲国产精品第一区二区三区| 欧美日韩调教| 欧美日韩国产成人| 欧美日韩国产欧美日美国产精品| 欧美亚洲尤物久久| 亚洲欧美日韩精品久久久| 夜夜嗨一区二区三区| 一区二区三区四区五区精品| 一本一本久久a久久精品综合麻豆| 老色批av在线精品| 欧美ed2k| 亚洲人成在线免费观看| 久久亚洲精品网站| 欧美a级片网站| 亚洲人线精品午夜| 日韩视频免费看| 欧美福利影院| 亚洲欧洲综合| 亚洲一区视频| 欧美怡红院视频一区二区三区| 日韩网站在线| 亚洲一区二区三区在线观看视频| 亚洲国产精品久久久久婷婷884| 久久久国产成人精品| 久久先锋影音av| 欧美激情网友自拍| 一区二区日韩| 久久成人免费网| 欧美精品性视频| 国产精品日产欧美久久久久| 韩国v欧美v日本v亚洲v| 亚洲精品三级| 欧美一级视频| 欧美在线视频免费| 免费亚洲电影在线| 99精品欧美一区| 欧美综合国产精品久久丁香| 欧美激情一区二区三区在线视频观看 | 亚洲综合首页| 美女国内精品自产拍在线播放| 久久久www免费人成黑人精品| 香蕉免费一区二区三区在线观看 | 麻豆av一区二区三区| 亚洲国产精品久久91精品| 在线亚洲+欧美+日本专区| 久久久久一区二区| 欧美午夜精品理论片a级按摩| 欧美日韩在线影院| 精品av久久707| 在线观看日韩精品| 一区二区三区高清| 亚洲一区二区三区精品动漫| 玖玖国产精品视频| 亚洲午夜精品网| 欧美freesex8一10精品| 欧美日韩成人在线播放| 韩日精品视频一区| 亚洲午夜精品17c| 亚洲欧美在线免费观看| 欧美一级片久久久久久久| 亚洲福利在线观看| 久久精品国产第一区二区三区最新章节| 性做久久久久久久久| 欧美专区在线观看一区| 久久精品夜色噜噜亚洲a∨| 欧美体内谢she精2性欧美| 亚洲国产mv| 久久综合999| 欧美国产日韩xxxxx| 亚洲欧美国产77777| 欧美日韩视频一区二区| 亚洲精品女av网站| 欧美aaa级| 久久久久国产精品厨房| 国产欧美日韩在线观看| 午夜视频一区二区| 日韩亚洲一区二区| 欧美日韩四区| 在线亚洲国产精品网站| 亚洲电影免费观看高清完整版在线观看 | 亚洲一区二区三区乱码aⅴ蜜桃女| 亚洲欧美日韩一区二区| 日韩视频一区二区| 欧美精品久久久久久久久老牛影院| 国产精品久久一区二区三区| 在线一区免费观看| 亚洲狼人精品一区二区三区| 欧美韩国日本综合| 亚洲最黄网站| 亚洲美女区一区| 欧美日韩精品在线观看| 日韩一区二区精品葵司在线| 亚洲国产精品国自产拍av秋霞| 亚洲一区国产精品| 久久一区激情| 亚洲激情在线激情| 午夜亚洲伦理| 亚洲欧美清纯在线制服| 国产欧美亚洲视频| 久久免费高清视频| 久久免费一区| 亚洲三级影片| 一区二区三区精品国产| 国产精品久久久久免费a∨大胸| 亚洲人成7777| 99re视频这里只有精品| 免费观看30秒视频久久| 91久久极品少妇xxxxⅹ软件| 亚洲国产综合91精品麻豆| 欧美精品在线观看播放| 亚洲一区二区免费视频| 亚洲制服欧美中文字幕中文字幕| 免费毛片一区二区三区久久久| 国产精品美女一区二区| 久久久国产91| 免费av成人在线| 亚洲午夜电影在线观看| 欧美一区永久视频免费观看| 在线看日韩欧美| 亚洲区一区二区三区| 国产精品尤物| 欧美成年人视频| 欧美日韩亚洲免费| 久久精品国产久精国产思思| 免费不卡在线视频| 亚洲一区二区动漫| 久久精品国产2020观看福利| 亚洲人成毛片在线播放| 欧美激情国产精品| 国产精品九九| 午夜精品www|