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

牽著老婆滿街逛

嚴(yán)以律己,寬以待人. 三思而后行.
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

轉(zhuǎn)載自: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 楊粼波 閱讀(1021) 評(píng)論(0)  編輯 收藏 引用


只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   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>
            欧美韩日精品| 亚洲国产中文字幕在线观看| 欧美一级视频精品观看| 久久久久久久波多野高潮日日 | 亚洲免费在线播放| 亚洲伊人久久综合| 午夜日韩视频| 久久久久综合| 欧美金8天国| 欧美日韩亚洲一区二区三区在线 | 久久久久免费| 麻豆成人综合网| 欧美激情精品久久久久久蜜臀| 欧美激情一二三区| 国产精品久久久久久久久久妞妞 | 亚洲黄色视屏| 99国产一区二区三精品乱码| 亚洲三级国产| 午夜精品久久久久久久蜜桃app | 欧美在线啊v一区| 美女脱光内衣内裤视频久久网站| 亚洲电影欧美电影有声小说| 中文精品视频| 久久综合亚州| 欧美日韩一区二区三区免费看| 国产日韩欧美视频在线| 日韩一级黄色片| 久久久久久久欧美精品| 99riav久久精品riav| 久久精品99国产精品酒店日本| 欧美日本韩国在线| 激情久久久久久久| 午夜精品视频| 亚洲人成啪啪网站| 欧美在线视频一区| 欧美日韩综合另类| 亚洲国产精品精华液网站| 午夜久久tv| 亚洲欧洲精品一区二区三区| 欧美在线观看网址综合| 国产精品盗摄一区二区三区| 亚洲精品视频二区| 老司机aⅴ在线精品导航| 亚洲一区二区三区精品视频 | 国产精品xxxxx| 亚洲激情不卡| 看片网站欧美日韩| 午夜日韩电影| 国产精品揄拍一区二区| 一二美女精品欧洲| 欧美国产日韩一区二区| 久久爱另类一区二区小说| 国产精品视频99| 一区二区电影免费在线观看| 欧美激情第六页| 国产精品伦子伦免费视频| 伊人天天综合| 久久精品理论片| 亚洲一区激情| 国产精品久久久久aaaa樱花| 日韩视频精品| 亚洲激情午夜| 欧美国产成人精品| 日韩一区二区福利| 国产精品永久免费观看| 久久成人精品电影| 亚洲尤物视频在线| 国产精品综合| 玖玖视频精品| 裸体女人亚洲精品一区| 亚洲精选视频免费看| 亚洲区一区二区三区| 欧美日韩国产精品一区| 99精品欧美一区二区三区| 一本色道婷婷久久欧美| 国产精品欧美日韩| 久久久免费观看视频| 久久综合综合久久综合| 99精品国产热久久91蜜凸| 亚洲人屁股眼子交8| 欧美香蕉视频| 欧美在线日韩在线| 久久激情五月婷婷| 亚洲精品欧美专区| 在线视频欧美精品| 国产中文一区二区| 亚洲国产精品毛片| 国产免费亚洲高清| 亚洲国产aⅴ天堂久久| 欧美午夜精品理论片a级按摩| 久久狠狠久久综合桃花| 免费永久网站黄欧美| 亚洲午夜国产成人av电影男同| 亚洲欧美日韩综合aⅴ视频| 在线观看久久av| 一区二区三区视频免费在线观看| 一区二区在线观看av| 在线亚洲美日韩| 亚洲国产欧美日韩另类综合| 亚洲夜间福利| 亚洲人成在线观看网站高清| 亚洲在线成人精品| 亚洲毛片在线| 欧美诱惑福利视频| 亚洲视频每日更新| 美女视频黄a大片欧美| 羞羞色国产精品| 欧美精品aa| 你懂的视频欧美| 国产免费亚洲高清| 亚洲最新中文字幕| 亚洲级视频在线观看免费1级| 亚洲欧美国产毛片在线| 一区二区激情| 欧美成人午夜激情在线| 欧美一级久久久久久久大片| 欧美高清视频在线播放| 久久综合伊人77777蜜臀| 欧美视频一区二区在线观看| 久久国产精品久久久久久| 亚洲日韩视频| 久久久精品999| 久久精品亚洲一区二区三区浴池| 欧美日韩伦理在线免费| 亚洲高清av在线| 黑人巨大精品欧美一区二区| 亚洲国产成人久久| 久久精品麻豆| 久久精品亚洲一区| 欧美日韩免费观看一区=区三区| 久久久久久午夜| 国内精品久久久久影院薰衣草| 亚洲在线视频| 亚洲欧美国内爽妇网| 欧美喷水视频| 91久久精品国产91久久| 亚洲精品欧美日韩| 欧美成人激情视频| 亚洲第一成人在线| 亚洲高清视频的网址| 久久久久一区二区三区| 欧美a级一区| 亚洲高清一二三区| 久久综合给合| 欧美国产一区二区| 亚洲另类春色国产| 欧美日韩一区二区在线视频| 亚洲精品老司机| 亚洲性人人天天夜夜摸| 国产精品v片在线观看不卡| 一二三区精品| 亚洲欧美日韩成人高清在线一区| 国产精品久久久久三级| 亚洲男女毛片无遮挡| 久久久亚洲成人| 精品动漫av| 久久久久久欧美| 亚洲国产精品久久精品怡红院| 久久精品成人一区二区三区| 久久久久久久97| 亚洲国产va精品久久久不卡综合| 久久亚洲春色中文字幕| 亚洲高清免费视频| 亚洲视频一二| 国产啪精品视频| 欧美在线看片| 亚洲精品小视频在线观看| 亚洲女人av| 好吊色欧美一区二区三区视频| 欧美高潮视频| 午夜免费日韩视频| 亚洲国产精选| 欧美一区精品| 一区二区视频免费完整版观看| 欧美成人有码| 香蕉久久一区二区不卡无毒影院 | 久久久久久香蕉网| 亚洲精品一区中文| 国产欧美一区二区三区在线老狼| 久久精品1区| 中文日韩在线| 亚洲大胆视频| 久久九九电影| 中文日韩在线视频| 亚洲国产精品一区二区www在线| 久久久久国产精品www| 久久精品亚洲乱码伦伦中文| 日韩一级精品视频在线观看| 国产一区二区三区在线观看网站| 欧美极品aⅴ影院| 欧美中文字幕在线视频| 亚洲毛片在线免费观看| 欧美成人免费网站| 久久精品一区二区三区中文字幕 | 男人插女人欧美| 销魂美女一区二区三区视频在线| 亚洲精品美女| 亚洲成人中文| 激情久久五月| 国产综合久久|