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

牽著老婆滿街逛

嚴(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>
            欧美久久电影| 欧美a级一区| 国产精品主播| 国产精品色网| 国产综合视频| 在线观看的日韩av| 亚洲欧洲一区二区在线播放| 亚洲精选国产| 亚洲欧美日韩另类精品一区二区三区| 亚洲一区亚洲| 久久电影一区| 亚洲国产精品嫩草影院| 久久综合999| 亚洲欧洲视频在线| 午夜精品久久久久久久白皮肤| 午夜精品久久久久久久久久久久久| 亚洲欧美日韩精品久久久| 久久久久欧美精品| 欧美日韩美女| 国产亚洲视频在线| 亚洲精品国产拍免费91在线| 午夜精品免费在线| 亚洲电影在线| 久久国产88| 欧美丝袜一区二区三区| 国产在线视频欧美| 亚洲新中文字幕| 欧美aaaaaaaa牛牛影院| 亚洲一级电影| 欧美国产日韩在线| 国产一区二区高清不卡| 一区二区三区不卡视频在线观看| 久久国产日本精品| 99精品99久久久久久宅男| 久久天堂av综合合色| 国产精品第一区| 亚洲国产影院| 久久最新视频| 欧美一级黄色录像| 国产精品久久精品日日| 亚洲精品乱码久久久久久按摩观| 在线日韩成人| 国产精品视频九色porn| 亚洲大片免费看| 欧美一区二区三区四区视频| 亚洲第一在线| 久久精品欧美日韩| 国产精品一区二区久久久| 亚洲精品中文字幕在线观看| 久久久国产精品一区| 亚洲色图综合久久| 欧美日韩91| 亚洲免费av观看| 亚洲国产精品嫩草影院| 麻豆av福利av久久av| 红杏aⅴ成人免费视频| 午夜精品影院| 亚洲欧美激情四射在线日 | 激情欧美一区二区| 欧美在线观看网址综合| 亚洲一区免费视频| 国产精品成人一区二区网站软件 | 亚洲一区国产精品| 国产精品久久久久天堂| 亚洲免费一级电影| 亚洲一区二区三区中文字幕 | 久久久精品2019中文字幕神马| 国产一区二区三区的电影| 久久精品亚洲乱码伦伦中文| 性做久久久久久久久| 国产亚洲欧美一区| 美女视频黄a大片欧美| 老司机一区二区| 日韩视频不卡中文| 99在线精品免费视频九九视| 欧美视频免费在线观看| 亚洲欧美精品伊人久久| 亚洲小说欧美另类社区| 国产午夜精品全部视频播放| 久久久亚洲一区| 欧美a级片网| 亚洲一区二区在线播放| 亚洲欧美一区二区精品久久久| 狠狠干狠狠久久| 亚洲日本aⅴ片在线观看香蕉| 国产精品国产三级国产a| 久久亚洲色图| 欧美日韩一区成人| 久久亚洲私人国产精品va| 久久精品青青大伊人av| 一本色道久久综合亚洲91| 国产精品麻豆va在线播放| 久久久国产成人精品| 欧美精品免费在线| 欧美专区日韩专区| 美女久久网站| 午夜精品一区二区三区在线| 久久久噜噜噜久久中文字幕色伊伊| 亚洲人成在线播放| 午夜视频精品| 一区二区激情| 久久伊人一区二区| 亚洲综合国产激情另类一区| 久久国产免费| 亚洲欧美日本国产有色| 美女图片一区二区| 久久国产免费看| 欧美日本韩国一区| 久热这里只精品99re8久| 欧美三日本三级少妇三2023| 久久综合狠狠综合久久激情| 欧美午夜精品久久久久久超碰| 蜜桃av综合| 国产欧美日韩精品一区| 亚洲美女黄色| 亚洲黄一区二区| 久久久国产精品一区二区中文 | 久久尤物视频| 久久精品一区| 国产精品欧美久久久久无广告| 欧美韩国日本综合| 黄色成人av网| 久久久www成人免费无遮挡大片| 午夜伦理片一区| 国产精品wwwwww| 一区二区三区四区在线| 亚洲视频日本| 欧美精品一区二区三区四区| 免费亚洲电影在线观看| 国产亚洲激情在线| 欧美一区午夜精品| 久久久精品欧美丰满| 国产日本欧洲亚洲| 亚洲免费在线看| 欧美亚洲网站| 国产精品美女在线观看| 一片黄亚洲嫩模| 一区二区三区.www| 欧美日韩在线第一页| 一区二区三区四区国产精品| 一区二区三区日韩精品| 欧美日韩国产在线观看| 日韩视频免费观看高清在线视频| 91久久精品一区| 欧美精品国产精品日韩精品| 91久久精品视频| 中文av一区二区| 国产精品嫩草影院一区二区| 午夜精品网站| 蜜桃久久精品乱码一区二区| 91久久在线视频| 欧美日韩蜜桃| 午夜精品久久久久久99热软件| 久久久.com| 国产精品青草综合久久久久99 | 亚洲国产一区二区a毛片| 亚洲乱码国产乱码精品精98午夜| 免费视频最近日韩| 亚洲精品黄色| 亚洲免费在线观看| 国内精品久久久| 欧美激情精品久久久久久蜜臀 | 亚洲激情视频在线| 欧美视频官网| 久久久999精品免费| 亚洲国产成人久久| 西瓜成人精品人成网站| 在线欧美亚洲| 国产精品99一区二区| 久久www成人_看片免费不卡| 欧美黑人国产人伦爽爽爽| 亚洲一区二区三区免费在线观看| 国产日韩综合一区二区性色av| 乱码第一页成人| 午夜免费久久久久| 亚洲人成高清| 久久综合综合久久综合| 亚洲在线国产日韩欧美| 亚洲第一在线综合网站| 国产精品免费在线| 欧美成年人视频网站欧美| 亚洲欧美偷拍卡通变态| 亚洲日本电影在线| 男男成人高潮片免费网站| 性欧美xxxx大乳国产app| 亚洲日韩成人| 国产一区观看| 国产精品永久在线| 欧美色一级片| 欧美国产日本在线| 久热国产精品视频| 久久精品国产77777蜜臀| 亚洲视频在线播放| 亚洲精品专区| 亚洲国产精品一区制服丝袜| 久久综合九九| 久久久久女教师免费一区| 亚洲欧美日韩综合aⅴ视频| 日韩视频三区| 亚洲精品一品区二品区三品区|