什么是热更新
所谓的热更新,指的是客户端的更新。
大致的流程是,客户端在启动后访问更新的URL接口,根据更新接口的反馈,下载更新资源,然后使用新的资源启动客户端,或者直接使用新资源不重启客户端。
热更新代码使用到的场景
-
情人节快到了,你想要组织一个游戏内活动,错过时机肯定是你最不想要看到的结果。
-
当你发现一个严重的bug。
-
当你想要添加一些新的场景或者关卡来延长游戏的生命。
-
以及非常多其他的情况...
在Cocos2d-x引擎中的如何实现热更新
LuaEngine
LuaEngine是一个脚本能够实时运行Lua脚本的对象,也就是因为有了LuaEngine这个C++类对象,所以才有了实现热更新技术的基础
-
获取LuaEngine脚本引擎的对象。
1
2
3
4
|
//获取LuaEngine引擎脚本类的单例对象
LuaEngine*engine=LuaEngine::getInstance();
//设置该单例对象作为脚本引擎管理器对象的脚本引擎
ScriptEngineManager::getInstance()->setScriptEngine(engine);
|
-
使用LuaEngine执行Lua字符串脚本
//使用Lua脚本引擎执行Lua字符串脚本
engine->executeString(
"print(\"Hello蓝鸥\")"
);
|
-
使用LuaEngine执行Lua文件脚本
//获取储存的文件路径
std::stringpath=FileUtils::getInstance()->getWritablePath();
path+=
"hellolanou.lua"
;
//创建一个文件指针
//路径、模式
FILE
*file=
fopen
(path.c_str(),
"w"
);
if
(file){
fputs
(
"print(\"蓝鸥!!!\")"
,file);
fclose
(file);
}
else
CCLOG(
"savefileerror."
);
//使用Lua脚本引擎执行Lua文件脚本
engine->executeScriptFile(path.c_str());
|
lua_State
lua_State,可以认为是“脚本上下文”,主要包括当前Lua脚本环境的运行状态信息,还会有GC相关的信息。
在使用Cocos2d-x引擎开发时需要使用Lua,那么就需要连接到libcocos2d和libluacocos2d两个静态库。
也就是要在lua_State对象中注册对应的功能模块类,如果不想要使用里边相应的模块时,就可以在luamoduleregister.h中注释掉对应的一行代码。
int
lua_module_register(lua_State*L)
{
//注册cocosdenshion模块
register_cocosdenshion_module(L);
//注册network网络模块
register_network_module(L);
#ifCC_USE_CCBUILDER
//注册cocosbuilder模块
register_cocosbuilder_module(L);
#endif
#ifCC_USE_CCSTUDIO
//注册coccostudio模块
register_cocostudio_module(L);
#endif
//注册ui模块
register_ui_moudle(L);
//注册extension模块
register_extension_module(L);
#ifCC_USE_SPINE
//注册spine模块
register_spine_module(L);
#endif
#ifCC_USE_3D
//注册3d模块
register_cocos3d_module(L);
#endif
//注册音频audio模块
register_audioengine_module(L);
return
1;
}
|
在使用Cocos2d-x引擎时需要使用Quick框架时,同样需要在lua_State注册quick框架的对应模块。
static
void
quick_module_register(lua_State*L)
luaopen_lua_extensions_more(L);
lua_getglobal(L,monospace!important; font-size:1em!important; min-height:inherit!important; color:blue!important; background:none!important">"_G"
);
(lua_istable(L,-1))
//stack:...,_G,
{
register_all_quick_manual(L);
//extra
luaopen_cocos2dx_extra_luabinding(L);
register_all_cocos2dx_extension_filter(L);
luaopen_HelperFunc_luabinding(L);
#if(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
luaopen_cocos2dx_extra_ios_iap_luabinding(L);
#endif
}
lua_pop(L,1);
|