在 objc 和 lua 之间建立一个简单的桥梁?
我已将 Lua 与我的 ObjC 代码(iphone 游戏)集成。设置非常简单,但现在我在桥接方面遇到了一些问题。我已经用谷歌搜索了结果等...似乎没有任何东西可以在不修改的情况下工作。我的意思是,我检查过 luaobjc 桥(它看起来很旧并且已经停产),我听说过 LuaCocoa 但它似乎不适用于 iPhone,而且蜡太厚了。
我的需求非常少,我只需要能够从 lua 调用 objc 方法,并且不介意必须做额外的工作才能使其工作(我不需要完全自动的桥接系统)。
所以,我决定自己根据这个页面建立一座小桥 http://anti-alias.me /?p=36。它包含有关如何完成我需要的内容的关键信息,但教程尚未完成,并且我对从 lua 等调用时如何处理方法重载有一些疑问...
有人知道 objc 之间是否存在任何工作桥梁吗 ?和 iPhone 上的 lua 或者是否很难完成上述网站提供的桥梁?
任何信息都将受到欢迎。
I have integrated Lua with my ObjC code (iphone game). The setup was pretty easy, but now, I have a little problem with the bridging. I have googled for results, etc... and it seems there isn't anything that could work without modifications. I mean, I have checked luaobjc bridge (it seems pretty old and dicontinued), I heard about LuaCocoa but it seems not to work on iphone, and wax is too thick.
My needs are pretty spare, I just need to be able to call objc methods from lua and don't mind having to do extra work to make it work (I don't need a totally authomatic bridging system).
So, I have decided to build a little bridge myself based on this page http://anti-alias.me/?p=36. It has key information about how to accomplish what I need, but the tutorial is not completed and I have some doubts about how to deal with method overloading when called from lua, etc...
Do anybody know if there exist any working bridge between objc and lua on the iphone or if it could be so hard to complete the bridge the above site offers?
Any information will be welcomed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,聚会有点晚了,但以防万一其他人也迟到了这篇文章,这里有另一种方法来添加可用的选择:手动编码您的 LUA API。
我做了一个关于这个主题的讲座,我在一个小时内编写了一些基本的 LUA 绑定。这并不难。从讲座中我制作了一组视频教程展示如何开始。
如果您已经拥有您需要调用的用 Objective-C 编写的 API,那么使用 SWIG 这样的绑定生成工具的方法是一个很好的方法,并且将所有都带入是有意义的。 strong> 这些相同的 API 已转移到 LUA 中。
手动编码方法的优点:
最后一点是,如果您拥有仅在引擎级别有意义的详细功能,并且您不想在编写游戏时看到这些功能你需要告诉 SWIG 不要来绑定那些。
史蒂芬斯的答案是完美的,这种方法只是另一种选择,根据项目的不同,它可能更适合某些人。
OK, bit late to the party but in case others come late also to this post here's another approach to add to the choices available: hand-code your LUA APIs.
I did a lecture on this topic where I live coded some basic LUA bindings in an hour. Its not hard. From the lecture I made a set of video tutorials that shows how to get started.
The approach of using a bindings generation tool like SWIG is a good one if you already have exactly the APIs that you need to call written in Objective-C and it makes sense to bring all those same API's over into LUA.
The pros of the hand-coding approach:
The last point is just that if you have detail functions that only make sense at the engine level and you don't want to see those when coding the game play you'll need to tell SWIG not to bind those.
Steffens answer is perfect and this approach is just another option, that may suit some folks better depending on the project.
不要重新发明轮子!
首先,您认为 luaobjc 和其他一些变体已经过时了,这是正确的。可以在 LuaCocoa 页面上找到很好的概述。 LuaCocoa 很好,但显然不支持 iPhone 开发,所以唯一的其他选择是 蜡。 LuaCocoa 和 Wax 都是运行时桥梁,这意味着(理论上)您可以访问 Lua 中的每个 Objective-C 类和方法,但会牺牲运行时性能。
对于游戏来说,根据我的经验,运行时性能开销非常大,以至于不保证使用任何运行时绑定库。从为什么人们会使用脚本语言的角度来看,这两个库都违背了优先使用脚本语言而不是低级语言的目的:它们不提供 DSL 解决方案 - 这意味着您仍然要编写本质上 Objective-C 代码,但语法略有不同,没有运行时调试支持,也没有代码Xcode 中的编辑支持。换句话说:运行时 Lua 绑定充其量是一个有问题的解决方案,并且有很多缺点。运行时 Lua 绑定特别不适合追求持续高帧率的快节奏动作游戏。
您想要的是静态绑定。静态绑定至少要求您声明 Lua 代码中可用的方法类型。一些绑定库会扫描您的头文件,另一些则要求您提供类似于头文件的特殊声明文件。大多数绑定库都可以使用这两种方法。好处是最佳的运行时性能,并且能够实际设计 Lua 脚本可以访问哪些类、方法和变量。
目前只有 3 个候选者可以将 Lua 代码绑定到 iPhone 应用程序。公平地说,还有很多,但大多数都有一个或多个关键缺陷,或者根本不稳定,或者仅用于特殊目的,或者根本不适用于 iPhone 应用程序。候选者是:
所有 Lua 静态绑定库共有的一个大缺点:没有一个它们可以直接绑定到 Objective-C 代码。所有这些都需要有一个额外的 C 或 C++ 层,最终与您的 Objective-C 代码交互。这与 Objective-C 作为一种语言的工作原理以及在将 Lua 嵌入到 Objective-C 应用程序中时所扮演的角色(到目前为止)有多小有关。
我最近评估了所有三个绑定库并开始喜欢 SWIG。它文档非常详细,但有一些学习曲线。但我相信学习曲线是有必要的,因为 SWIG 可以用来结合几乎任何编程和脚本语言,了解如何在未来的项目中使用 SWIG 是有利的。另外,一旦您了解了它们的定义文件实现,就会发现它非常简单(特别是与 luabind 相比),并且比 tolua 更加灵活。
Don't reinvent the wheel!
First, you are correct that luaobjc and some other variants are outdated. A good overview can be found on the LuaCocoa page. LuaCocoa is fine but apparently doesn't support iPhone development, so the only other choice is Wax. Both LuaCocoa and Wax are runtime bridges, which means that you can (in theory) access every Objective-C class and method in Lua at the expense of runtime performance.
For games and from my experience the runtime performance overhead is so significant that it doesn't warrant the use of any runtime binding library. From a perspective of why one would use a scripting language, both libraries defy the purpose of favoring a scripting language over a lower-level language: they don't provide a DSL solution - which means you're still going to write what is essentially Objective-C code but with a slightly different syntax, no runtime debugging support, and no code editing support in Xcode. In other words: runtime Lua binding is a questionable solution at best, and has lots of cons going against it. Runtime Lua bindings are particularly unsuited for fast-paced action games aiming at a constantly high framerate.
What you want is a static binding. Static bindings at a minimum require you to declare what kind of methods will be available in Lua code. Some binding libraries scan your header files, others require you to provide a special declaration file similar to a header file. Most binding libraries can use both approaches. The benefit is optimal runtime performance, and being able to actually design what classes, methods and variables Lua scripts have access to.
There are but 3 candidates to bind Lua code to an iPhone app. To be fair, there are a lot more but most have one or more crucial flaws or are simply not stable or for special purposes only, or simply don't work for iPhone apps. The candidates are:
Big disadvantage shared by all Lua static binding libraries: none of them can bind directly to Objective-C code. All require to have an additional C or C++ layer available that ultimately interfaces with your Objective-C code. This has to do with how Objective-C works as a language and how small a role it has played (so far) when it comes to embedding Lua in Objective-C apps.
I recently evaluated all three binding libraries and came to enjoy SWIG. It is very well documented but has a bit of a learning curve. But I believe that learning curve is warranted because SWIG can be used to combine nearly any programming and scripting language, it can be advantageous to know how to use SWIG for future projects. Plus, once you understand their definition file implementation it turns out to be very easy (especially when compared to luabind) and considerably more flexible than tolua.