如何使函数永久更改全局数组

How to make a function change a global array permanently

最近我一直在为 text-based 游戏开发库存系统,该游戏使用库存系统的全局数组和相应的函数来读取所述数组中的 true 或 false。我遇到的问题 运行 是这个,我用来修改数组的函数

void playerGet(bool items[], int itemNumber) //this function takes an assigned argument of the array indices variable, and changes that array indices from true, to false. 
{
    items[itemNumber] = true;
}

仅在其所在函数范围内修改数组。数组在 .cpp 文件中定义如下:

void inventoryArray(bool items[]) //This function establishes all the items in the game, the true false statement expresses whether or not the item is in the player's inventory. 
{
    items[WEAPON_RELIC_RIFLE] = false;
    items[WEAPON_SCALPEL] = false;
    items[MISC_ACTION_FIGURE] = false;
    items[MISC_FIRE_EXTINGUISHER] = false;
    items[MISC_LIFE_RAFT] = false;
}

然后在 .h 文件中声明如下:

void inventoryArray(bool items[]);

数组中使用的枚举在 header 文件中定义如下:

enum equipment //This declares a list of enums for each item in the game, consumables, not included. 
{
    WEAPON_RELIC_RIFLE, // = 0
    WEAPON_SCALPEL, // = 1
    MISC_ACTION_FIGURE, // = 2
    MISC_FIRE_EXTINGUISHER, // = 3
    MISC_LIFE_RAFT, // = 4
    MAX_EQUIPMENT
};

读取库存数组的函数是这样的:

void twoScavengerCombat(bool items[])
{
    for (int item = 0; item < MAX_EQUIPMENT; ++item)
    {
        if (items[item] == true) //if true proceed
        {
            switch (item)
            {
            case 0: //if array indices identifier = 0, print relic rifle
                cout << "1: Use the Relic Rifle\n";
                break;
            case 1:
                cout << "2: Use the Scalpel\n";
                break;
            case 2:
                break;
            case 3:
                cout << "3: Use the Fire Extingusher\n";
                break;
            case 4:
                cout << "4: Use the Life Raft\n";
                break;
            default:
                cout << "Error";
                break;
            }
        }
        else
            cout << "Option Unavailible\n"; //if false print Option Unavailible
    }

已编译,使用数组和枚举 headers 声明主文件如下所示:

int toolSearch()
{
   bool items[MAX_EQUIPMENT];
    inventoryArray(items);
   playerGet(items, 0);
}

void twoScavengerCombat(bool items[])\ declared in this file, but since its just above here i left it as a forward declaration to save space

int main()
{
   toolSearch();
   twoScavengerCombat(items);
   return 0;
}

理想情况下,这会产生结果:使用 Relic Rifle 选项不可用 选项不可用 选项不可用 选项不可用

但它却产生了 5 个选项不可用。我错过了什么?

你会想要

//bunch of #include<> directives
bool items[MAX_EQUIPMENT];

int toolSearch()
{
    inventoryArray();
    playerGet( 0);
}

void twoScavengerCombat()
...
// other functions here

int main()
{
   toolSearch();
   twoScavengerCombat();
   return 0;
}

请注意,bool items[MAX_EQUIPMENT]; 未在函数中定义。在其下方定义的任何内容的清晰视图中,它在文件顶部自行关闭。这就是全球化的意义。任何人和每个人都可以访问它,如果他们知道它在哪里,或者您使用 extern 语句告诉他们它在哪里。它在程序启动时创建(甚至在 main 之前,如果变量的初始化逻辑有问题,这可能会导致一些非常有趣的调试),并且仅在程序运行时结束。

Lightness Races in Orbit delves a bit deeper here,但更关心使全局变量扩展到单个文件之外

不需要将物品传递给任何函数,因为每个人都可以看到 items 缺点是只有一个 items 所以如果你有多个玩家,每个玩家都有不同的物品列表,你会有问题的。

您可能需要查看 std::vector(可调整大小的数组)和 std::map(这将允许您按名称 items["sword"].attackFoe(foe); 查找项目)和 std::set(这使得查看玩家拥有的东西变得非常容易 (if (items.find("Vorpal Hand Grenade") != items.end()) BlowStuffUp();),而不必每次都搜索每个项目。