简体   繁体   English

从程序中的任何位置访问变量的最佳方法是什么?

[英]What's the best way to have a variable be accessible from anywhere in your program?

I'm making a game right now and pretty much everything has its own class. 我现在正在制作游戏,几乎所有东西都有自己的类。 The main classes I feel that I have a problem with are my 'Level' and 'Object' class. 我觉得我遇到问题的主要班级是我的'Level'和'Object'类。 The level class contains images to every image in the level, and the Object class contains the image for each object on the screen(an object is pretty much anything: your player, an enemy, an item, etc). 关卡类包含关卡中每个图像的图像,而对象类包含屏幕上每个对象的图像(一个对象几乎是任何东西:玩家,敌人,物品等)。

The way I have it right now is that the Object class has an Image and when you create a new object, you load a new image into it. 我现在拥有的方式是Object类具有一个Image,并且在创建新对象时,将新图像加载到其中。 So lets say you have two enemies that use the same image, both instances of the object will separately load the image and I'll have two of the same images in memory. 假设您有两个使用相同图像的敌人,则该对象的两个实例将分别加载该图像,而我在内存中将拥有两个相同的图像。 This seems like a really bad idea and later when my game gets more complicated it will slow it down a lot. 这似乎是一个非常糟糕的主意,后来当我的游戏变得更加复杂时,它将使速度大大降低。

So what I was thinking about doing is having something like a Resource Manager class that would hold all of the images, and than each object would just ask the resource manager for the image it needs. 因此,我正在考虑做的事情是拥有一个类似Resource Manager的类,该类可以保存所有图像,然后每个对象只会向资源管理器询问所需的图像。 That way it would only store each image once and save some space. 这样,它将只存储每个图像一次并节省一些空间。

I could probably easily do this with a static variable in the Object class, but since the Level class also needs to use images it would also need access to the Resource Manager. 我可能可以使用Object类中的静态变量轻松地执行此操作,但是由于Level类还需要使用图像,因此还需要访问Resource Manager。 Would it be best to send a pointer to a resource manager to each instance of an object/level(or any other class I later make that would need it) and access it that way? 最好将一个指向资源管理器的指针发送到一个对象/级别的每个实例(或以后我需要的其他任何类)并以这种方式访问​​它吗? Or would there be a better way to do this? 还是会有更好的方法来做到这一点?

Such problems are generally solved with the singleton pattern. 这些问题通常通过单例模式解决。 Make sure to review the various issues that pattern has before applying any particular version of it. 在应用任何特定版本之前,请确保检查该模式存在的各种问题。

You should usually avoid "making variables accessible from anywhere" - see here for why. 您通常应该避免“使变量可从任何地方访问”-请在此处查看原因。

In your case, that means it's a bad idea to make the ResourceManager accessible from everywhere. 在您的情况下,这意味着从任何地方访问ResourceManager都是一个坏主意。 I suggest you do as you say in the post - have every game object keep a pointer to the resource manager. 我建议您按照您在帖子中所说的那样做-让每个游戏对象都保留一个指向资源管理器的指针。

In the future it may turn out your game objects need to know about other core objects as well. 将来可能会发现您的游戏对象也需要了解其他核心对象。 For example your game object may need to call methods on a central game state. 例如,您的游戏对象可能需要在中央游戏状态下调用方法。 Then it makes sense to create a core class like this: 然后创建这样的核心类是有意义的:

class Core {  
   public:
   ...
   ResourceManager& resourceManager();
   GameState& currentGame();
   // and so on
};

And your base GameObject class keep a pointer to just the core. 而且您的基本GameObject类仅保留指向核心的指针。 Then you can create game objects like this: 然后,您可以创建如下游戏对象:

Core core; // performs some initialization
core.startGame();
Enemy enemy(&core); // it's easy to pass the core to the game object

Have the base GameObject class require a Core& in its constructor, and provide a protected Core& GameObject::core() function. 让基本GameObject类在其构造函数中需要Core&,并提供受保护的Core& GameObject::core()函数。

I think i would do it this way: 我想我会这样:

1) Have a resource manager class responsible for loading everything you need. 1)有一个资源管理器类,负责加载您需要的所有内容。

2) Each object (image or anything else) has an associated string, in order to be able to do something like : 2)每个对象(图像或其他任何对象)都有一个关联的字符串,以便能够执行以下操作:

manager->setResource("levelImage"); manager-> setResource(“ levelImage”);

A registerResource could be used, a freeResource, or whatever else you like. 可以使用registerResource,freeResource或其他任何您喜欢的东西。

This way you can easily access everything you need i think :) 这样,您可以轻松访问所需的一切,我认为:)

I would do this using the singleton pattern. 我将使用单例模式进行此操作。 I usually create a generic singleton class that I subclass later. 我通常创建一个通用的单例类,稍后再进行子类化。 Something like this: 像这样:

template<class Derived>
    class Singleton
    {

        public:

            static Derived& instance();

        protected:

            Singleton() {};


        private:

            Singleton(const Singleton&) {};

            Singleton& operator=(const Singleton&) { return *this; };

        private:

            static std::auto_ptr<Derived> _instance;
    };

Then the resource class can do: 然后,资源类可以执行以下操作:

class ResourceManager : public Singleton<ResourceManager>
{

    public:

        friend class Singleton<ResourceManager>;

    protected:

        ResourceManager() {};
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有可变数量的模板参数的最佳方法是什么? - What's the best way to have a variable number of template parameters? 在 class 中拥有“多类型”成员变量的最佳方法是什么? - What's the best way to have a "multitype" member variable in a class? 在Mac上编写c ++的最佳方法是什么? - What's the best way to program c++ on a Mac? 收集全部从同一基类派生的类实例的最佳方法是什么? - What's the best way to have a collection of instances of classes all derived from the same base class? 在C ++ DLL中以可从DLL的任何导出函数中访问的方式构造类对象的最佳方法是什么? - What is the best way to construct a class object in a C++ DLL in a way that it is accessible from within any exported function of DLL? 终止程序并输出选择的错误消息的最佳方法是什么? - What is the best way to terminate my program and have it output an error message of my choosing? 由于我无法返回局部变量,从C或C ++函数返回字符串的最佳方法是什么? - Since I can't return a local variable, what's the best way to return a string from a C or C++ function? 并行化这个的最佳方法是什么? - What’s the best way to parallelize this? 从几个 map 中搜索的最佳方法是什么<key,value> ?</key,value> - What's the best way to search from several map<key,value>? 从MinGW程序调用Postgres的最简单方法是什么? - What's the easiest way to call Postgres from a MinGW program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM