alxcyl
alxcyl

Reputation: 2722

Require Lua files from writeable path

I'm currently stuck with this problem for about 3 days now.

I'm using cocos2d-x 2.1.3 and Lua scripting for the game. Using AssetsManager as a base, I have now successfully fetched .zip files from server and load them in the app. However, there's a tiny catch.

In my project (which is based on AssetsManagerTest), it downloads a .zip file (called package.zip containing two files, a .png (called HelloWorld.png) and a .lua file (called game.lua). The .lua file simply creates a HelloWorld scene, just to test if it works, and it did.

Then I edited game.lua file to require another .lua file, called gem.lua. When I try to run it, it reports an error saying that it cannot find gem.lua. So I tried editing my C++ code to make use of CCLuaEngine::addSearchPath( const char * ), however, it still doesn't work.

I download package.zip folder and extract it into the path returned by CCFileUtils::getWritablePath(). The files are properly extracted and the game.lua is found. When I tried adding gem.lua into the package, I added this bit of code:

// Obtain the full script path. The value returned is:
//     CCFileUtils::getWritablePath() + "/games/game/scripts"
const std::string scriptPath = MyAssetManager::sharedManager() -> getScriptPath();

// Obtain the script file path
std::string gamePath = scriptPath + "/game.lua";

// Add the scriptPath to the LuaEngine search paths
CCLuaEngine::defaultEngine() -> addSearchPath( scriptPath.c_str() );

// Run the script
CCLuaEnging::defaultEngine() -> executeScriptFile( gamePath.c_str() );

Even though gem.lua is in the proper path (/games/game/scripts/), the engine can't somehow find it. After looking around, it turns out the base path for the search paths is the Resource folder.

Is there a way to make the CCLuaEngine look for the writable path? It can be done with resources, though.

This is the whole error message I get:

[LUA ERROR] ...D35-DB7A1B515ED3/Documents/games/game/scripts/game.lua:3: module 'gem.lua' not found:
    no field package.preload['gem.lua']gem.lua
    no file './gem/lua.lua'
    no file '/usr/local/share/luajit-2.0.1/gem/lua.lua'
    no file '/usr/local/share/lua/5.1/gem/lua.lua'
    no file '/usr/local/share/lua/5.1/gem/lua/init.lua'
    no file 'Scripts/gem/lua.lua'
    no file 'Scripts/Common/gem/lua.lua'
    no file 'Scripts/HomeScene/gem/lua.lua'
    no file 'Scripts/Games/gem/lua.lua'
    no file '/Users/xxxx/Library/Application Support/iPhone Simulator/6.1/Applications/E0CFD097-5DED-4E6D-8D35-DB7A1B515ED3/Documents/games/game/scripts/gem/lua.lua'
    no file './gem/lua.so'
    no file '/usr/local/lib/lua/5.1/gem/lua.so'
    no file '/usr/local/lib/lua/5.1/loadall.so'
    no file './gem.so'
    no file '/usr/local/lib/lua/5.1/gem.so'
    no file '/usr/local/lib/lua/5.1/loadall.so'

Upvotes: 1

Views: 1389

Answers (1)

catwell
catwell

Reputation: 7020

Given the error message you probably wrote something like require "gem.lua" in your code. You should remove the extension: require "gem".

Upvotes: 2

Related Questions