简体   繁体   English

全局对象通过共享指针进行交互

[英]Global Object interacted with by shared pointers

So, I've been trying to use a Shared pointer to a struct to hold all the data in my program that can be shared between states in my state machine. 因此,我一直在尝试使用指向结构的Shared指针来保存程序中可以在状态机中的状态之间共享的所有数据。

struct GameData
{
    StateMachine machine;
    sf::RenderWindow window;
    AssetManager assets;
    InputManger input;
    const int x = 1;
};

typedef std::shared_ptr<GameData> GameDataRef;

this is one file that just provides an interface between shared data resources. 这是一个仅提供共享数据资源之间接口的文件。 called GameData.cpp 叫做GameData.cpp

GameDataRef _data = std::make_shared<GameData>();

In my game.cpp file, if I understand this correctly, make_shared creates the object and assigns the shared_ptr named _data to it. 在我的game.cpp文件中,如果我正确理解这一点, make_shared创建该对象并将其分配给名为_data的shared_ptr

StateRef _splash = std::make_unique<State_Splash>(_data);

This is the line that creates a base state to start the game with. 这是创建游戏开始的基本状态的行。 Never mind any part other than it takes the shared pointer as argument. 除了将共享指针作为参数,别无所求。

GameDataRef _data;

I make another shared pointer in the State_Splash.cpp called _data 我在State_Splash.cpp中创建了另一个共享指针,称为_data

State_Splash::State_Splash(GameDataRef GDR) : _data(GDR)

In the constructor, I (forgot the term for what comes after the : here) 在构造函数中,我(忘记了:这里后面的术语)

std::cout << _data->x;

this prints out 0 to the console, even though x is defined in the struct as 1. I ran this test because a texture I loaded into _data->assets in the Game.cpp was out of scope(blank white screen) when I tried to reference it to a sprite in the State_Splash.cpp. 即使x在结构中定义为1,它也会向控制台输出0。我运行此测试是因为尝试时在Game.cpp中加载到_data-> assets的纹理超出了范围(黑屏)将其引用到State_Splash.cpp中的sprite。

My question is, am I doing something wrong with shared pointers? 我的问题是,共享指针在做错什么吗? Or is there a better way to make some shared resource depot for the entire program? 还是有更好的方法为整个程序创建一些共享资源仓库?

Or is there a better way to make some shared resource depot for the entire program? 还是有更好的方法为整个程序创建一些共享资源仓库?

Singletons have bad reputation. 单身人士的声誉不好。 A lot of it comes from pre-C++11 era when it was hard to get them right. 很多原因来自C ++ 11以前的时代,那时很难正确地解决它们。 C++11 makes them safe. C ++ 11使它们安全。 It's also a global object - so it's subject to all considerations regarding globals. 它也是一个全局对象-因此要考虑有关全局变量的所有注意事项。 They should be avoided except when they make your program better. 应避免使用它们,除非它们可以使您的程序更好。

Singletons work pretty well in game applications. 单例在游戏应用程序中工作得很好。 I'd just create something like GameContext singleton and forget about shared pointers. 我只是创建类似GameContext singleton的东西,而忽略共享指针。 Your game data is global by design. 您的游戏数据在设计上是全局的。

However if your code is designed to have multiple such objects, then singleton won't work for you. 但是,如果您的代码设计为具有多个此类对象,则单例将对您不起作用。

A singleton or global of any kind trashes composition and testability. 单例或任何形式的全局都会破坏成分和可测试性。 There is almost never a good reason for such things. 这种事情几乎从来没有一个很好的理由。

Better to use dependency injection. 最好使用依赖项注入。 Then you can test the components of your game with only a model of the dependency. 然后,您可以仅使用依赖关系模型来测试游戏的组件。

#include <vector>

struct StateMachine{};
namespace sf { struct RenderWindow {}; }
struct AssetManager {};
struct InputManager {};

struct GameData
{
    StateMachine machine;
    sf::RenderWindow window;
    AssetManager assets;
    InputManager input;
    const int x = 1;
};

struct action {};
std::vector<action> checkInputs(InputManager&);

void runGame(GameData& gameData)
{
    while (1) {
        auto inputActions = checkInputs(gameData.input);
    }

}

int main()
{
    GameData gameData;

    runGame(gameData);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM