C++ 中类似 GameMaker 的功能
当我年轻的时候,我经常使用一个叫做 Game Maker 的工具。我从中开始学习编程。我现在已经远远超出了这个范围,但回顾起来,它的一些功能和设计非常有趣。我想知道 - 我如何使用 C++ 实现与此工具类似的功能? 我想知道:
- 对象/类
Game Maker 有一个您要创建的“对象”列表,这些对象本质上只是不同的类,全部派生自同一基类(我现在将其称为 GameObject)以及一个名为'instance_create' 会将对象类型作为参数。 在 C++ 中,这看起来像这样(尽管语法上非常不正确):
class MyGameObject : GameObject
{
//...
}
GameObject instance_create(class objecttype)
{
objecttype newinstance = new objecttype();
return newinstance
}
GameObject* gameobjectinstance = instance_create(MyGameObject);
我将如何实现它?
- 系统变量/函数
Game Maker 拥有可以从任何地方访问的系统变量和函数。时期。任何地方。我正在考虑全局,但我知道这是糟糕的设计。我正在考虑拥有一个全局类,并将变量/函数设置为静态,但随后它们就无法更改。我该怎么做呢?
- var
Game Maker 只有一种数据类型 - var。它可以是字符串、整数、小数,任何东西。并且有用于这些之间转换的系统函数。
最后,如何在某种脚本中定义对象类型?就像,如果我想添加新类型的对象,创建一个新脚本?我不认为 C++ 可以在运行时创建对象类型,那么我该怎么做呢?
When I was younger, I used a tool called Game Maker a lot. I started to learn to program from it. I'm far beyond that now, but looking back on it, some of it's features and designs are quite interesting. I'm wondering- How would I implement functionality similar to this tool using C++?
I'm wondering about:
- Objects/classes
Game Maker had a list of 'Objects' that you would create which were essentially just different classes all derived from the same base class (I'll call it GameObject for now) amd a system function called 'instance_create' that would take an object type as a paramater.
In c++ this would look something like this (Though syntatically very incorrect):
class MyGameObject : GameObject
{
//...
}
GameObject instance_create(class objecttype)
{
objecttype newinstance = new objecttype();
return newinstance
}
GameObject* gameobjectinstance = instance_create(MyGameObject);
How would I go about implementing that?
- system variables/functions
Game Maker had system variables and functions that could be accessed from anywhere. Period. Anywhere. I'm thinking globals, but I know that's bad design. I'm thinking Having a global class, and have the variables/functions as static, but then they cannot be altered. How would I do that?
- var
Game Maker had only one data type- a var. It could be a string, an integer, a decimal, anything. And there were system functions for conversion between those.
Lastly, how could I define the object types in some kind of script? Like, if I want to add a new type of object, create a new script? I don't think C++ can create object types at runtime, so how would I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用模板。
然而,您指定的设计非常有问题(充其量是这样)并且绝对不适合 C++。您应该努力实现一个设计良好且适合该语言的系统,而不是重新创建过去的系统。
我特别认为,既然你提到了脚本的运行时解释,实际上 GameMaker 类和 C++ 类彼此无关。而且您肯定不能在运行时创建C++类,也不能在运行时传递类型,也不能在运行时实例化模板。
您最适合简单地编写一种脚本语言,例如 Lua,并仅用 C++ 编写必要的高性能组件。
Using a template.
However, the design you have specified is highly questionable (at best) and definitely not suited to C++. You should strive to implement a well-designed system, and one appropriate to the language, not re-create a system from the past.
I especially think that since you mention run-time interpretation of scripts, that in fact the GameMaker classes and C++ classes have nothing to do with each other. And you definitely cannot create C++ classes at run-time, nor can you pass types around at run-time, nor can you instantiate templates at run-time.
You would be best suited simply whipping out a scripting language, such as Lua, and writing only the necessary high-performance components in C++.
Game Maker 使我们能够通过“对象”控制游戏功能,每个“对象”都由“事件”组成,这些“事件”在游戏过程中的特定时间触发。事件内有“动作”。首先,值得注意的是,将 Game Maker 开发与 C++ 进行比较就像比较粉笔和奶酪。然而,从理论上讲,我想你可以在 C++ 中镜像 GM 功能(尽管效率非常低),如下所示:
基对象类可能看起来像这样:
你将从此类派生并重写函数(“事件”)与您的对象相关(组成这些函数的语句是您的“操作”)。然后会有某种对象实例管理器单例类,它保存当前“房间”中所有对象实例的列表,并循环遍历每个帧(并处理实例),通过调用各自的函数来触发相关事件。
有趣的是,这实际上大致说明了为什么像 Game Maker 这样的系统缺乏一定程度的效率。为了让开发人员有更多选择,存在额外的、不必要的开销。所有对象派生自的臃肿的基础对象对于特定情况来说通常是多余的。例如,想象一个对象在 50 个事件中只使用了两个事件 - 对象管理器仍然盲目地检查所有其他事件,即使它们没有被使用。显然可以进行优化,但总的来说,引擎的广度最终会导致性能下降。
至于与单个“var”类型相关的查询,如前所述,这更多是脚本的特征,而不是 C++ 的特征。这证明Game Maker不能简单地仅用C++来建模。
Game Maker enables us to control game functionality through "objects", each composed out of "events", which are triggered at certain times during a game. Within events are "actions". Firstly, it's worth noting that comparing Game Maker development with C++ is like comparing chalk and cheese. However, theoretically speaking, I'd imagine you could mirror GM functionality (albeit very inefficiently) in C++ as follows:
The base object class could look something like this:
You'd derive from this class and override the functions ("events") that are relevant to your object (the statements that compose these functions are your "actions"). Then there would be some sort of object instance manager singleton class which holds a list of all object instances in the current "room" and loops through each every frame (and handles instancing), triggering relevant events by calling their respective functions.
Interestingly, this actually roughly demonstrates why a system like Game Maker lacks a degree of efficiency. There is additional, unnecessary overhead that exists in order to keep options open for the developer. The bloated base object that all objects derive from is often overkill for specific situations. For example, imagine an object with just two events used out of 50 - the object manager still blindly checks for all these other events even if they aren't utilised. Obviously optimisations can be made, but overall, the breadth of the engine ultimately results in reduced performance.
As for your query relating to a single 'var' type, as has been stated already, this is more a characteristic of scripting, not C++. This proves that Game Maker cannot simply be modeled by C++ alone.