【发布时间】:2015-08-24 09:45:53
【问题描述】:
我想创建一个为我生成此代码的宏:
if (myEntity.get(Attack) == null) myEntity.add(new Attack());
if (myEntity.get(Confused) == null) myEntity.add(new Confused());
if (myEntity.get(Defend) == null) myEntity.add(new Defend());
if (myEntity.get(Offense) == null) myEntity.add(new Offense());
在代码中我想这样声明/使用它:
EntityMacroUtils.addComponents(myEntity, Attack, Confused, Defend, Offense);
当前的宏函数如下所示:
macro public static function addComponents(entity:ExprOf<Entity>, components:Array<ExprOf<Class<Component>>>):Expr
{
var exprs:Array<Expr> = [];
for (componentClass in components)
{
var instance = macro $e { new $componentClass() }; // problem is here
var expr = macro if ($entity.get($componentClass) == null) $entity.add(instance);
exprs.push(expr);
}
return macro $b{ exprs };
}
这个宏函数不正确,我得到错误:
EntityMacroUtils.hx:17:字符 22-43:找不到类型:$componentClass
问题是我不知道如何定义new $componentClass()。我将如何解决这个问题?
我还想避免在输出代码中出现Type.createInstance。
【问题讨论】: