开发者

Lua: require() not working on iPhone

I am working on a shooting game on iPhone and I need lua for scripting levels, enemies, and etc. So I wrote a bullet script like this:

-- circular_bullet.lua
local time_between_bullets = 0.2;

...

function InitializeCircularBullet(objectName)
  ...
end

and an enemy script:

-- level1_D2.lua
require("circular_bullet.lua");

...

But it turned out that the enemy script can't "require" the bullet script. I tried to look into lua library, and found out that in loadlib.c :

static int ll_require (lua_State *L) {
  ...
    if (lua_isfunction(L, -1))  /* did it find module? */
      break;  /* module loaded sucessfully */
    else if (lua_isstring(L, -1))  /* loader returned error message? */
      lua_concat(L, 2);  /* accumulate it */
    else
      lua_pop(L, 1);
  ...
}

It would enter the "else if" branch, which means some error happened, but 开发者_如何学JAVAI have no idea how to read that error message.

If I comment out the "require" line, the enemy "level1_D2" would work as intend without shooting bullet. I also did try copy the whole circular_bullet.lua into level1_D2.lua, and it worked, so the problem must be the require statement.

Those two files are under root directory of the package. (I don't know how to make them in different directory, thus I had found out that Diner Dash kept its scripts in different directory.) However the two files are not in the same group in my Xcode project. I tried putting them in same group but nothing happened.

Anyone knows what the problem is? Thanks a lot!


Finally I got the answer!!!

the lua require function searches "./scrips" directory for require files, so I got to put those script in the directory!

Yet I still don't know how to change that searching path, but it did work.


I've got a snippet here which might help you to change that path:

// Initialize library path
lua_pushstring(L,"package");
lua_gettable(L, LUA_GLOBALSINDEX);

string path = string(Globals::GetPathPrefix())+"?.lua";

lua_pushstring(L, "path");
lua_pushstring(L, path.c_str());
lua_settable(L, -3);

lua_pop(L,1);   


I also had this when I was new to Lua. I don't know this also appears on iPhone, but on Windows I had to remove the '.lua' So just remove the extension.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜