In the Lua language, the require
statement search module has a built-in order and can be used package.path
to maintain the module's search strategy.
But in Cocos2d-x, that 's not true!
The cocos2d-x overloaded the original LUA require loading mode. (See COCOS2DXLUALOADER.CPP)
The Cocos2dxlualoader logic is in effect before Package.path, and Package.path on Android does not handle the problem of loading the PKG package internal files very well.
So in actual use, we only use cocos2d-x overloaded method is possible.
How do you do it?
Cocos2dxlualoader internal is used CCFileUtils::getFileData()
to load LUA code.
So if we want to add our own Lua script search path, we can just call CCFileUtils::addSearchPath()
it.
The following C + + code is implemented on iOS and Android platforms, the program looks for LUA files from the Scripts folder under the download path, and then looks for the Lua file target from the Scripts folder under the program's built-in resource path.
#if(Cc_target_platform = = Cc_platform_ios)std::stringDownloadedscriptpath = Ccfileutils::sharedfileutils ()->getwritablepath () +"Scripts"; Ccfileutils::sharedfileutils ()-Addsearchpath (Downloadedscriptpath.c_str ()); STD::stringScriptPath = Ccfileutils::sharedfileutils ()->fullpathforfilename ("Scripts"); Ccfileutils::sharedfileutils ()-Addsearchpath (Scriptpath.c_str ());#elif(Cc_target_platform = = cc_platform_android)std::stringDownloadedscriptpath = Ccfileutils::sharedfileutils ()->getwritablepath () +"/scripts"; Ccfileutils::sharedfileutils ()-Addsearchpath (Downloadedscriptpath.c_str ()); Ccfileutils::sharedfileutils ()->addsearchpath ("assets/scripts");#elif(Cc_target_platform = = Cc_platform_win32)//TODO#else_error_#endif
Android and iOS read Lua's search path (just copy others, not tested)