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

发布于 2024-09-17 06:22:05 字数 1494 浏览 4 评论 0原文

我真的很想将 Lua 脚本添加到我的游戏引擎中。我正在使用 Lua 并使用 luabind 将其绑定到 C++,但我需要帮助来设计使用 Lua 构建游戏实体的方式。

引擎信息:

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

所以,这是一个困境:

假设我有这个 Lua 文件来定义一个 GameEntity 及其组件:

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},
   },
  },
 },
}

如您所见,这个 GameEntity 由 2 个组件定义,“compHealth”和“compAnimatedSprite””。这两个完全不同的组件需要完全不同的初始化参数。健康需要一个整数和一个浮点数(总计和再生),另一方面,动画需要精灵表名称,并定义所有动画(帧、持续时间等)。

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

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

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

I really want to add Lua Scripting to my Game Engine. I am working with Lua and bound it to C++ using luabind, but I need help to design the way I will construct my Game Entities using Lua.

Engine Information:

Component Oriented, basically each GameEntity is a list of components that are updated in a delta T interval. Basically Game Scenes are composed by collections of Game Entities.

So, here's the dilemma:

Let's say I have this Lua file to define a GameEntity and it's components:

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},
   },
  },
 },
}

As you can see, this GameEntity is defined by 2 components, "compHealth" and "compAnimatedSprite". Those two totally different components require totally different initialization parameters. Health requiring an integer and a float ( total and regeneration ), and on the other side, animations requiring a sprite sheet name , and to define all of the animations ( the frames, durations, etc ).

I would love to make some kind of abstract class with some virtual initializer method that could be used by all my components that require Lua binding in order to facilitate initialization from Lua, but it seems difficult, because the virtual class is not going to have one virtual init method. This is because all of the component initializers (or most of them) require different init parameters (health component requires different init than Animated Sprite component, or AI component).

What do you suggest to make the Lua bindings to the constructors of this components easier ? or how would you do it ?

PS: I must use C++ for this project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单挑你×的.吻 2024-09-24 06:22:05

我建议为您的游戏实体使用复合结构。在解析 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)

I'd suggest using a composite structure instead for your game entities. Add objects inheriting from a common game entity component to each game entity as you encounter them while parsing the Lua configuration table. This task is a perfect candidate for the factory method. Note that composing your entities in this way still requires all methods to be implemented in the GameEntity class unless you use an alternate dispatch method like message passing (see the visitor pattern)

On the Lua side, I find it is more convenient to use callback function with a single table argument instead of traversing a complex table structure in C/C++. Here is a pure Lua example of what I mean using your data.

-- 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文