如何为多个实体共享/重用Lua脚本? [英] How to share/reuse a Lua script for multiple entities?

查看:105
本文介绍了如何为多个实体共享/重用Lua脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Lua脚本进行C ++游戏的设计/骨架编码阶段,但是遇到了一个设计问题:

I'm in the design/skeleton coding phase of my C++ game with Lua scripting, but I have run into a design issue:

游戏将具有相同种类的实体的许多副本,并且行为由相同的脚本控制.有没有一种简单的方法可以在单个lua_state中的相同类型的实体之间共享脚本?我只能在互联网上几次问到这个问题.我读过关于在不同的lua_state中加载同一脚本是否是一个好主意的混合反馈,而不是关于替代方案的深入反馈.

The game will have many copies of the same kind of entities, with behavior controlled by the same script. Is there a straightforward way I can share the script between entities of the same type in a single lua_state? I have only been able to find this question asked a couple of times on the Internet; I have read mixed feedback on whether or not it's a good idea to load the same script in different lua_state's, and not in-depth feedback on alternatives.

这是简单且防弹的,但是我认为使用创建的相同实体类型的每个实例加载,编译和存储相同字节代码的附加副本是一种悲惨的浪费,因此我想找出一个更明智的解决方案

It's simple and bullet-proof, but I think loading, compiling, and storing addition copies of the same byte code with each instance of the same entity type created is a tragic waste, so I would like to figure out a smarter solution.

这是我想到的两个解决方案.我对编程或C或OO概念并不陌生,但是我仍然在学习Lua,尤其是Lua/C API.我认为我的想法不错,但我什至不确定如何实施它们.:

These are the two solutions I have thought of. I'm not new to programming or C or OO concepts but I am still learning when it comes to Lua and especially the Lua/C API. I think my ideas are sound but I am not even sure how I would go about implementing them.:

  1. 在Lua脚本中实现OO,并用Lua对象表示每个实体;所有Lua逻辑都会作用于对象.这也将允许任何实体改变全球环境的好处(或好处").

  1. Implement OO in the Lua script and have each entity be represented by a Lua object; all the Lua logic would act on the object. This would also have the benefit (or the "benefit") of allowing the global environment to be changed by anything single entity.

使用setfenv将每个实体封装在其自己的环境中,并从全局空间复制所有功能的引用.据我了解,env只是与默认全局表不同的表,但是我研究了setfenv,但我不知道该怎么做.

Encapsulate each entity in its own environment using setfenv and copy references of all of the functions from the global space. As I understand it the env is just a different table than the default global, but I've looked into setfenv but I don't know how I would do that.

推荐答案

1和2或多或少只是同一枚硬币的不同面.只是对象走到哪里的问题.在类型1中,对象是Lua脚本的显式部分.这意味着脚本将决定如何设置其对象.

1 and 2 are just different sides of the same coin, more or less. It's simply a matter of where the object goes. In type 1, the object is an explicit part of the Lua script. Which means the script decides how it wants to set up its objects.

在类型2中,对象是环境.它仍然是一个Lua表,但是是由外部代码为其创建的一个表.该脚本无法摆脱此对象的限制,除非外部代码允许.

In type 2, the object is the environment. It is still a Lua table, but one created for it by the external code. The script cannot break free of the confines of this object, except in the ways that the external code allows.

对我来说,实现类型1的最简单方法是使用 Luabind .我将有一个AI对象作为C ++类,Lua可以从中继承该对象.为该AI运行主脚本"将创建该类的实例.您将传递脚本参数,例如它所控制的实体的名称,也许它可以用来控制它的引用等.

The easiest way for me to implement type 1 would be with Luabind. I'd have an AI object as a C++ class, which Lua would be able to derive from. Running the "main script" for that AI would create an instance of that class. You would pass the script parameters, like the name of the entity it controls, maybe a reference it can use to control it, etc.

类型2非常简单.首先,通过创建一个空表并使用希望用户能够访问的全局变量填充该表来创建新环境.这些将用于诸如与游戏状态对话(在场景中查找其他对象等),移动有问题的实体的方式等等.您可以玩一些可变的技巧,以有效地使这些值不变且不变,以便用户以后无法修改它们.

Type 2 is fairly simple. First, you create the new environment by creating an empty table and populating it with the global variables that you want the user to be able to have access to. These would be for things like talking to game-state (find other objects in the scene, etc), ways to move the entity in question around, and so forth. There are metatable tricks you can play to effectively make these values immutable and constant, so the user can't modify them later.

然后,用lua_loadstringlua_loadfile加载脚本.这会将表示该Lua脚本的函数放在Lua堆栈上.然后,使用lua_setfenv将此表用作该脚本函数的环境.然后,您可以运行该脚本,并传递所需的任何变量(实体名称等).

Then, you load the script with lua_loadstring or lua_loadfile. This puts a function on the Lua stack that represents that Lua script. Then you apply this table as that script function's environment with lua_setfenv. You can then run that script, passing whatever variables you wish (the name of the entity, etc).

这篇关于如何为多个实体共享/重用Lua脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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