我有一个类型列表.我想创建一个元组,其结果是在该列表中的每个类型上调用一个函数,然后将其用作另一个仿函数的参数.所以像这样:
template<typename F>
struct function_traits;
template<typename T, typename R, typename... Args>
struct function_traits<R(T::*)(Args...) const> {
using return_type = R;
using param_types = std::tuple<Args...>;
};
template<typename T> struct function_traits : public
function_traits<decltype(&T::operator())> {};
template <typename T>
T* get_arg(int id)
{
// Actual implementation omitted. Uses the id parameter to
// do a lookup into a table and return an existing instance
// of type T.
return new T();
}
template <typename Func>
void call_func(Func&& func, int id)
{
using param_types = …Run Code Online (Sandbox Code Playgroud) 我希望能够有一大块 Lua 代码(一个“脚本”),可以在游戏中的敌人类型之间共享,但是脚本的每个实例都有一个独特的执行环境。为了说明我的问题,这是我第一次尝试脚本的外观:
time_since_last_shoot = 0
tick = function(entity_id, dt)
time_since_last_shoot = time_since_last_shoot + dt
if time_since_last_shoot > 10 then
enemy = find_closest_enemy(entity_id)
shoot(entity_id, enemy)
time_since_last_shoot = 0
end
end
Run Code Online (Sandbox Code Playgroud)
但这失败了,因为我将在所有敌人之间共享全局 time_since_last_shoot 变量。然后我尝试了这个:
spawn = function(entity)
entity.time_since_last_shoot = 0;
end
tick = function(entity, dt)
entity.time_since_last_shoot = entity.time_since_last_shoot + dt
if entity.time_since_last_shoot > 10 then
enemy = find_closest_enemy(entity)
shoot(entity, enemy)
entity.time_since_last_shoot = 0
end
end
Run Code Online (Sandbox Code Playgroud)
然后为每个实体创建一个唯一的表,然后在调用 spawn 和 tick 函数时将其作为第一个参数传递。然后在运行时以某种方式将该表映射回 id。这可以工作,但我有几个问题。
首先,它很容易出错。脚本仍然可能意外地创建全局状态,这可能导致以后在同一脚本甚至其他脚本中难以调试问题。
其次,由于 update 和 tick 函数本身是全局的,当我去创建尝试使用相同界面的第二种类型的敌人时,我仍然会遇到问题。我想我可以通过某种命名约定来解决这个问题,但肯定有更好的方法来处理它。
我确实发现这个问题似乎在问同样的事情,但接受的答案是关于具体细节的,并且指的是 Lua …
我正在编写一个解析一些命令的应用程序.命令以下列形式给出:
A {B}
我只想要A和B.A是可选的,但这很容易处理.我遇到的问题是A和B几乎都可以包含任何字符,包括空格和'{'和'}'.括号也不需要平衡.这可以用正则表达式解析吗?如果没有,您认为可以做的最简单的事情是什么?
例如,给定:
"parsme {foo {"hello"} {"goodbye"} {{{} {bar {"up"} {"down"}}"
然后:
A ="parseme {foo {"hello"} {"goodbye"} {{{}}和B ="bar {"up"} {"down"}"