在游戏编程中是全局变量坏吗? [英] In game programming are global variables bad?

查看:141
本文介绍了在游戏编程中是全局变量坏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我对全局变量的反应是坏!但在我在大学全球使用的两个游戏开发课程被广泛使用,现在在DirectX 9游戏编程教程我使用(www.directxtutorial.com)我被告知全球化在游戏编程中是可以的。 ..?网站还建议只使用结构,如果你可以在做游戏编程,以帮助保持简单。



我真的很困惑这个问题,一直试图做是很混乱。我意识到,当使用全局变量(线程问题,他们使代码更难维护,它们的状态很难跟踪等)有问题,但也有一个与不使用全局变量相关的成本,我不得不传递一个loooot (这是我第一次用C ++编写一个游戏)。无论如何,我意识到可能没有正确或者错误 错误的答案在这里,因为两种方式工作,但我想让我的代码是正确的,因为任何输入都是好的,非常感谢!


<游戏和全局的麻烦在于游戏(现在)在引擎级别上是线程化的。使用引擎的游戏开发者使用引擎的抽象而不是直接编程并发(IIRC)。在许多高级语言如C ++中,线程共享状态是复杂的。当许多并发进程共享一个共同的资源,他们必须确保他们不会踏上eachother的脚趾。



为了解决这个问题,你使用并发控制如互斥和各种锁。这实际上使得代码的异步关键部分以写入的同步方式访问共享状态。



如果线程使用全局变量运行,它会使调试非常困难,因为并发错误是一个



是例外。在游戏编程API如OpenGL和DX。如果你的共享数据/全局变量是指向DX或OpenGL图形上下文的指针,那么通常这会映射到GPU操作,这不会受到同样的麻烦。



小心。保持代表player或zombie或任何东西的对象,并且在线程之间共享它们可能是棘手的。生成播放器线程和僵尸组线程,并在它们之间基于消息传递而不是访问这些对象的状态跨线程/临界区边界之间的鲁棒的并发抽象。



说的一切,我同意下面的不要说全球化



有关复杂性的更多信息线程和共享状态请参见:



1 POSIX线程API - 我知道是POSIX,但提供了一个好主意,可以转换为其他API

2 维基百科的优秀范围关于并发控制机制的文章

3 餐饮哲学家的问题(以及许多其他人)

4 ThreadMentor教程和线程文章 < br>
5 英特尔另一篇文章,但更多的营销内容。

6 关于构建多线程游戏的ACM文章发动机


I know my gut reaction to global variables is "badd!" but in the two game development courses I've taken at my college globals were used extensively, and now in the DirectX 9 game programming tutorial I am using (www.directxtutorial.com) I'm being told globals are okay in game programming ...? The site also recommends using only structs if you can when doing game programming to help keep things simple.

I'm really confused on this issue, and all the research I've been trying to do is very confusing. I realize there are issues when using global variables (threading issues, they make code harder to maintain, the state of them is hard to track etc) but also there is a cost associated with not using globals, I'd have to pass a loooot of information around very often which can be confusing and I imagine time-costing, although I guess pointers would speed the process up (this is my first time writing a game in C++.) Anyway, I realize there is probably no "right" or "wrong" answer here since both ways work, but I want my code to be as proper as I can so any input would be good, thank you very much!

解决方案

The trouble with games and globals is that games (nowadays) are threaded at engine level. Game developers using an engine use the engine's abstractions rather than directly programming concurrency (IIRC). In many of the highlevel languages such as C++, threads sharing state is complex. When many concurrent processes share a common resource they have to make sure they don't tread on eachother's toes.

To get around this, you use concurrency control such as mutex and various locks. This in effect makes asynchronous critical sections of code access shared state in a synchronous manner for writing. The topic of concurrency control is too much to explain fully here.

Suffice to say, if threads run with global variables, it makes debugging very hard, as concurrency bugs are a nightmare (think, "which thread wrote that? Who holds that lock?").

There are exceptions in games programming API such as OpenGL and DX. If your shared data/globals are pointers to DX or OpenGL graphics contexts then typically this maps down to GPU operations which don't suffer so much from the same trouble.

Just be careful. Keeping objects representing 'player' or 'zombie' or whatever, and sharing them between threads can be tricky. Spawn 'player' threads and 'zombie group' threads instead and have a robust concurrency abstraction between them based on message passing rather than accessing those object's state across the thread/critical section boundary.

Saying all that, I do agree with the "Say no to globals" point made below.

For more on the complexities of threads and shared state see:

1 POSIX Threads API - I know it is POSIX, but provides a good idea that translates to other API
2 Wikipedia's excellent range of articles on concurrency control mechanisms
3 The Dining Philosopher's problem (and many others)
4 ThreadMentor tutorials and articles on threading
5 Another Intel article, but more of a marketing thing.
6 An ACM article on building multi-threaded game engines

这篇关于在游戏编程中是全局变量坏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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