4.1-4.4

清明假期, 到处公园转了一转, 日行2W步

4.5

准备着手了解一些c++游戏服务器,

RollerCoaster Tycoon 2
过山车达人,经营社区, 赚钱建造游乐园
otclient
cuberite
Minecraft
NoahGameFrame
服务端框架, 包含角色库, 网络库, 可以用于多人实时网络游戏引擎, 适配多种客户端引擎。
ArkGameFrame
基于[NoahGameFrame]的引擎
Poseidon
(C++98/11 + boost + OpenSSL + MySQL + MongoDB) 没有过多介绍
InfiniteRasa
游戏地址有墙, 看不了

4.8

运行时动态执行lua代码

需要程序运行时,能读取控制台输入代码,并在lua层执行。

代码:

c++ 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void watchThread(){
std::thread([]() {
std::string code;
while (true) {
std::getline(std::cin, code);
trim(code);
if (code.size() > 0) {
code = "gm('" + code + "')";
Director::getInstance()->getScheduler()->performFunctionInCocosThread([code]{
LuaEngine::getInstance()->executeString(code.c_str());
});
}
}
}).detach();
}

lua 代码

1
2
3
4
5
6
7
8
9
10
11
12
function gm(str)
-- local str = string.match(str, "^#echo (.*)$")
if(str) then
str = string.format("print(%s)", str)
local func, error = loadstring(str)
if(func) then
func()
else
print(error)
end
end
end

分析:

创建一个子线程, 每帧查询控制台输入,如果有的话,则调用lua层的`gm(str)`函数, 使用`loadstring(str)`,然后执行

问题:

不能直接在子线程执行lua代码,否则遇到render操作的时候,便会闪退。

方案:

使用cocos自带的`performFunctionInCocosThread`, 可以让一个函数在主线程执行。
因为该函数不能带参数, 所以需要用到`lamada表达式`构建一个匿名的函数, 使用闭包将参数传过去。

参考资料:


https://leafflow.top/2023/02/07/daily/2017-4/
作者
vollen
发布于
2023年2月7日
许可协议