Lua和C ++:职责分离 [英] Lua and C++: separation of duties

查看:189
本文介绍了Lua和C ++:职责分离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助分类组织C ++ / Lua游戏代码的方式,并分离他们的职责。



例如,Lua只能用于初始化C ++对象,或者在每次游戏循环迭代时使用。它只能用于游戏逻辑或图形。一些游戏引擎提供从脚本所有subytems的完全控制!我真的不喜欢这种方法(没有分离)。



将所有游戏对象(npc,位置)实现为没有C ++对象的Lua表是一个好主意吗?或者最好是镜像它们(Lua表来控制C ++对象)?

strong> http://stackoverflow.com/questions/2674462/lua-



主题的延续: http://stackoverflow.com/questions/2685636/lua-game-state-and-game-loop

解决方案

我的做法是尽可能限制暴露给Lua的内容。我从来没有发现需要一个主或其他这样的函数,每次渲染(或更多)时调用场景。一些Lua引擎(如LOVE)这样做。我喜欢用可能希望对象响应的公共事件的可选回调函数来定义对象,例如碰撞,鼠标点击,进入或离开游戏世界等。



最终结果是非常具有声明性,几乎是对象的配置文件。我有一个函数用于创建类或类型的对象,另一个用于创建基于这些类型的对象。对象然后具有可以在响应各种事件时被调用的方法的集合。所有这些Lua方法映射到C / C ++方法,这反过来修改对象的私有属性。下面是一个可以捕获球对象的bucket对象示例:

  define {
name ='ball'
texture = png('images / orb.png');
model ='active';
shape ='circle';
radius = 16;
mass = 1.0;
elastic = .7;
friction = .4;
}

define {
name ='bucket';
model ='active';
mass = 1;
shape ='rect';
width = 60;
height = 52;
texture = png('images / bucket.png');
elastic = .5;
friction = .4;
oncontact = function(self,data)
if data.subject:type()=='ball'then
local a = data.subject:angleTo(self:getxy b $ b如果a < 130和a> 50 then
--update score等。
end
end
end;
}



我不会把这作为一个真正的方式来实现你的脚本API。 Lua的一个美女是它支持许多不同风格的API。这是我发现的工作以及我做的游戏 - 基于2D物理的游戏。


Please help to classify ways of organizing C++/Lua game code and to separate their duties. What are the most convenient ways, which one do you use?

For example, Lua can be used for initializing C++ objects only or at every game loop iteration. It can be used for game logic only or for graphics, too. Some game engines provide full control to all subsytems from scripts! I really do not like this approach (no separation at all).

Is it a good idea to implement all game objects (npc, locations) as Lua tables without C++ objects? Or it is better to mirror them (Lua tables to control C++ objects)? Or something else?

Thank you.

Edit. My classification: http://stackoverflow.com/questions/2674462/lua-and-c-separation-of-duties/2685693#2685693.

Topic's continuation: http://stackoverflow.com/questions/2685636/lua-game-state-and-game-loop

解决方案

My approach has been to limit what is exposed to Lua as much as possible. I have never found a need for a "main" or other such function which is called every time the scene is rendered (or more). Some Lua engines (like LOVE) do this however. I prefer to define objects with optional callback functions for common events that you may want the object to respond to such as a collision, mouse click, entering or leaving the game world, etc..

The end result is very declarative, almost a config file for objects. I have a function for creating classes or types of objects and another for creating objects based on these types. The objects then have a collection of methods which can be called when responding to various events. All of these Lua methods map to C/C++ methods which in turn modify the object's private properties. Here is an example of a bucket object which can capture ball objects:

define {
    name='ball';
    texture=png('images/orb.png');
    model='active';
    shape='circle';
    radius=16;
    mass=1.0; 
    elastic=.7;
    friction=.4; 
}

define {
    name='bucket';
    model='active';
    mass=1;
    shape='rect';
    width=60;
    height=52;
    texture=png('images/bucket.png');
    elastic=.5;
    friction=.4; 
    oncontact = function(self, data)
        if data.subject:type() == 'ball' then
            local a = data.subject:angleTo(self:getxy())
            if a < 130 and a > 50 then
                --update score etc..
            end
        end
    end;
}

I would not take this as the "one true way" to implement your scripting API. One of the beauties of Lua is that it supports many different styles of API. This is just what I have found works well for the games that I make - 2D physics based games.

这篇关于Lua和C ++:职责分离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆