static int ForEachMob(lua_State* L) {
int n = lua_gettop(L);
if (n != 2) {
luaL_error(L, "ForEachMob requires 2 arguments, %d given.", n);
}
auto scope = GetLuaObject<Scope>(L, 1);
if (!scope) {
return luaL_error(L, "ForEachMob argument 1 must be a Scope.");
}
auto func = GetLuaFunctionRef(L, 2);
auto &mobs = scope->GetMobs();
for (auto it : mobs) { // iterate through mobs in this scope.
// Push the function reference onto the stack and call it. The callback should take one parameter - mob.
lua_rawgeti(L, LUA_REGISTRYINDEX, func); // Push the function onto the stack.
SetLuaObject<Mob>(L, it.second); // Push mob onto the stack as an argument to the function call.
if (lua_pcall(L, 1 /*num args*/ , 0 /*num return values*/ , 0 /*errfunc*/ ) != 0) { // Call our callback with one parameter - mob and no return values expected.
const char *errmsg = lua_tostring ( L , -1 ); // get error message from top of stack (-1). This pops off the error message from the stack so that when we leave this function our stack is back to normal with no extra stuff on it.
printf("Error running ForEachMob: %s\n", errmsg ); // print out error message for debugging purposes only... not for user display! This is because most errors will be typos in your script code which you need to see on your screen so you can fix them! :-)
return 0; // Return failure since there was an error running our callback code! Note that we don't need to pop off any extra stuff from our Lua Stack since Lua will handle cleaning up whatever is left on its own internal Stack when this C++ function returns back to Lua land! :-)
}
}
return 0; // Return success since we finished looping through all mobs without any errors! :-) Note that we don't need to pop off any extra stuff from our Lua Stack since Lua will handle cleaning up whatever is left on its own internal Stack when this C++ function returns back to Lua land! :-) Yay!!! :-D Hooray!!! \o/ \o/ \o/ Let's Party!!! :-P LOL!!! ;-) *whew* That was close!! Phew!! I almost forgot about popping something off my C++ Stack before returning!! LOL!!!! ;-) Whew!! Close call there!! lol!!!! :-P Anyways.... moving right along.... ;-) Yay!!!! \o/ \o/ \o/ Hooray!!! Let's Party!!!!! w00t w00t w00t!!!!!!!!! :-D Rock On Dudez!!!!! 8-) Peace Out Homiez!!!! 8-) <---- See how easy it is for me to get sidetracked?? lol!!! ;-) Anyways..... yeah.... moving right along..... ;-) Back To Work We Go!!!!! lol!!!! 8-) */ }
创作工场
免责声明:
以上内容除特别注明外均来源于网友提问,创作工场回答,未经许可,严谨转载。
点击这里>>使用🔥专业版,更聪明、更完整、更原创!