Lua,游戏状态和游戏循环 [英] Lua, game state and game loop

查看:155
本文介绍了Lua,游戏状态和游戏循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 在每个游戏循环迭代中调用main.lua脚本 - 是好还是坏设计?


  2. a 维持游戏状态。 C ++主机程序或 b 。从Lua脚本或 c


(有关主题的上一个问题: http://stackoverflow.com/questions/2674462/lua-and-c-separation-of-uties



(我为每个答案投票,最佳答案将被接受。)


关于lua的最好的事情是它有一个轻量级的VM,并且在块被预编译之后,在VM中运行它们实际上是相当快的,但仍然不是快速作为一个C ++代码,我不认为调用lua每个渲染框架是一个好主意。



我把游戏状态放在C ++中,并在lua中添加可以到达和修改状态的函数。基于事件的方法几乎更好,其中事件注册应该在lua中完成(优选地仅在游戏开始时或在特定游戏事件时,但是每分钟不超过几次),但实际事件应当通过C ++代码。用户输入也是事件,它们通常不发生在每一帧(除了可能MouseMove,但应该仔细使用,因为这一点)。处理用户输入事件的方式(无论是处理lua中的一切(例如按下哪个键等),还是键盘上的每个键都有独立的事件(在极端情况下),取决于游戏, (一个基于回合的游戏可能只有一个事件处理器为所有事件,一个RTS应该有更多的事件,一个FPS应该处理(主要是因为移动鼠标将发生每一帧))。一般来说,如果你需要处理的真实事件实际上是由更多独立的编程级事件触发的,那么它就会变得越困难这可能实际上会降低性能,因为lua代码需要更复杂)。



或者,如果性能非常重要,你可以通过添加新的操作码它(我已经看到一些公司这样做,但主要是使编译的lua块的反编译更难),这实际上不是一件难事。如果你有lua代码需要做很多事情(如事件注册,事件运行或更改游戏状态),你可能想在lua VM中实现它们,所以代替多个 getglobal setglobal 操作码他们只需要一两个(例如你可以做一个SETSTATE操作码0-255和0 -65535参数,其中第一个参数描述要修改的状态,第二个参数描述状态的新值。当然,这只有在你有最多255个事件,最多有2 ^ 16个值时才有效,但是在某些情况下可能就足够了。事实上,这只需要一个操作码意味着代码将运行得更快)。这也会使反编译更难,如果你打算掩盖你的lua代码(虽然没有太多的人知道lua的内部工作)。每帧运行几个操作码(大约30-40顶部)不会打击你的表现,严重。但是如果你需要做真正复杂的事情(一个简单的if-then-else可以采取多达10-20或更多的操作码,根据表达式),在lua VM中30-40个操作码不会让你远。 >

  1. Call main.lua script at each game loop iteration - is it good or bad design? How does it affect on the performance (relatively)?

  2. Maintain game state from a. C++ host-program or b. from Lua scripts or c. from both and synchronise them?

(Previous question on the topic: http://stackoverflow.com/questions/2674462/lua-and-c-separation-of-duties)

(I vote for every answer. The best answer will be accepted.)

解决方案

The best thing about lua is that it has a lightweight VM, and after the chunks get precompiled running them in the VM is actually quite fast, but still not as fast as a C++ code would be, and I don't think calling lua every rendered frame would be a good idea.

I'd put the game state in C++, and add functions in lua that can reach, and modify the state. An event based approach is almost better, where event registering should be done in lua (preferably only at the start of the game or at specific game events, but no more than a few times per minute), but the actual events should be fired by C++ code. User inputs are events too, and they don't usually happen every frame (except for maybe MouseMove but which should be used carefully because of this). The way you handle user input events (whether you handle everything (like which key was pressed, etc) in lua, or whether there are for example separate events for each keys on the keyboard (in an extreme case) depends on the game you're trying to make (a turn based game might have only one event handler for all events, an RTS should have more events, and an FPS should be dealt with care (mainly because moving the mouse will happen every frame)). Generally the more separate kinds of events you have, the less you have to code in lua (which will increase performance), but the more difficult it gets if a "real event" you need to handle is actually triggered by more separate "programming level events" (which might actually decrease performance, because the lua code needs to be more complex).

Alternatively if performance is really important you can actually improve the lua VM by adding new opcodes to it (I've seen some of the companies to do this, but mainly to make decompilation of the compiled lua chunks more harder), which is actually not a hard thing to do. If you have something that the lua code needs to do a lot of times (like event registering, event running, or changing the state of the game) you might want to implement them in the lua VM, so instead of multiple getglobal and setglobal opcodes they would only take one or two (for example you could make a SETSTATE opcode with a 0-255 and a 0-65535 parameter, where the first parameter descibes which state to modify, and the second desribes the new value of the state. Of course this only works if you have a maximum of 255 events, with a maximum of 2^16 values, but it might be enough in some cases. And the fact that this only takes one opcode means that the code will run faster). This would also make decompilation more harder if you intend to obscure your lua code (although not much to someone who knows the inner workings of lua). Running a few opcodes per frame (around 30-40 tops) won't hit your performance that badly. But 30-40 opcodes in the lua VM won't get you far if you need to do really complex things (a simple if-then-else can take up to 10-20 or more opcodes depending on the expression).

这篇关于Lua,游戏状态和游戏循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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