views:

66

answers:

2

Hi guys, 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 I 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!

A: 

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.

Chengyang
and it seems depend on version, very strange...
Chengyang
A: 

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);   
spurserh