Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在我的游戏引擎中集成Lua来构建我的游戏实体?_C++_Lua_Components_Game Engine - Fatal编程技术网

C++ 在我的游戏引擎中集成Lua来构建我的游戏实体?

C++ 在我的游戏引擎中集成Lua来构建我的游戏实体?,c++,lua,components,game-engine,C++,Lua,Components,Game Engine,我真的很想把Lua脚本添加到我的游戏引擎中。我与Lua合作,并使用Luabink绑定到C++,但我需要帮助设计我使用Lua。 发动机信息: 面向组件,基本上每个GameEntity都是以增量T间隔更新的组件列表。基本上,游戏场景由游戏实体的集合组成 所以,这是一个两难的问题: 假设我有这个Lua文件来定义游戏实体及其组件: GameEntity = { -- Entity Name "ZombieFighter", -- All the components that make the

我真的很想把Lua脚本添加到我的游戏引擎中。我与Lua合作,并使用Luabink绑定到C++,但我需要帮助设计我使用Lua。 发动机信息:

面向组件,基本上每个
GameEntity
都是以增量T间隔更新的
组件列表。基本上,
游戏场景
由游戏实体的集合组成

所以,这是一个两难的问题:

假设我有这个Lua文件来定义游戏实体及其组件:

GameEntity = 
{
 -- Entity Name
 "ZombieFighter",

 -- All the components that make the entity.
 Components = 
 {
  -- Component to define the health of the entity
  health = 
  {
   "compHealth",  -- Component In-Engine Name
   100,    -- total health
   0.1,    -- regeneration rate
  },

  -- Component to define the Animations of the entity
  compAnimation =
  {
   "compAnimatedSprite",

   spritesheet = 
   {
    "spritesheet.gif", -- Spritesheet file name.
    80,     -- Spritesheet frame width.
    80,     -- Spritesheet frame height.
   },

   animations =
   {
    -- Animation name, Animation spritesheet coords, Animation frame duration.
    {"stand", {0,0,1,0,2,0,3,0}, 0.10},
    {"walk", {4,0,5,0,6,0,7,0}, 0.10},
    {"attack",{8,0,9,0,10,0}, 0.08},
   },
  },
 },
}
如您所见,这个游戏实体由两个组件定义,“
compHealth
”和“
compAnimatedSprite
”。这两个完全不同的组件需要完全不同的初始化参数。运行状况需要一个整数和一个浮点(总计和再生),另一方面,动画需要一个精灵表名称,并定义所有动画(帧、持续时间等)

我很想用一些虚拟初始化器方法创建一个抽象类,它可以被我所有需要Lua绑定的组件使用,以便于从Lua进行初始化,但这似乎很困难,因为虚拟类没有一个虚拟init方法。这是因为所有组件初始值设定项(或大多数)需要不同的初始参数(健康组件需要不同于动画精灵组件或AI组件的初始参数)

您建议如何使Lua绑定到此组件的构造函数更容易?或者你会怎么做

PS:我必须使用C++来完成这个项目。

< P>我建议用A代替游戏实体。在解析Lua配置表时,将从通用游戏实体组件继承的对象添加到每个游戏实体。这项任务是一个完美的候选人。请注意,以这种方式组合实体仍然需要在GameEntity类中实现所有方法,除非使用诸如消息传递之类的备用分派方法(请参阅)

在Lua方面,我发现在C/C++中使用带有单个表参数的回调函数比遍历复杂的表结构更方便。下面是一个纯粹的Lua示例,说明了我使用您的数据的意思

-- factory functions
function Health(t) return { total = t[1], regeneration = t[2] } end
function Animation(t) return { spritesheet = t[1], animations = t[2] } end
function Spritesheet(t)
    return { filename = t[1], width = t[2], height = t[3] }
end
function Animations(t)
    return { name = t[1], coords = t[2], duration = t[3] }
end

-- game entity class prototype and constructor
GameEntity = {}
setmetatable(GameEntity, {
    __call = function(self, t)
        setmetatable(t, self)
        self.__index = self
        return t
    end,
})

-- example game entity definition
entity = GameEntity{
    "ZombieFighter",
    Components = {
        Health{ 100, 0.1, },
        Animation{
            Spritesheet{ "spritesheet.gif", 80, 80 },
            Animations{
                {"stand",  {0,0,1,0,2,0,3,0}, 0.10},
                {"walk",   {4,0,5,0,6,0,7,0}, 0.10},
                {"attack", {8,0,9,0,10,0},    0.08},
            },
        },
    }
}

-- recursively walk the resulting table and print all key-value pairs
function print_table(t, prefix)
    prefix = prefix or ''
    for k, v in pairs(t) do
        print(prefix, k, v)
        if 'table' == type(v) then
            print_table(v, prefix..'\t')
        end
    end
end    
print_table(entity)