如何使一个函数永久地改变全局数组

How to make a function change a global array permanently

本文关键字:改变 全局 数组 何使一 函数      更新时间:2023-10-16

最近我一直致力于一款基于文本的游戏的库存系统,该系统使用一个全局数组作为库存系统,并使用相应的函数来读取该数组中的真或假。我遇到的问题是,我用来修改数组

的函数
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[]);

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

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 Riflen";
                break;
            case 1:
                cout << "2: Use the Scalpeln";
                break;
            case 2:
                break;
            case 3:
                cout << "3: Use the Fire Extingushern";
                break;
            case 4:
                cout << "4: Use the Life Raftn";
                break;
            default:
                cout << "Error";
                break;
            }
        }
        else
            cout << "Option Unavailiblen"; //if false print Option Unavailible
    }
编译了

后,声明了数组和枚举头文件,主文件看起来像这样:

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个Option Unavailable。我错过了什么?

你会想

//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在这里进行了更深入的研究,但更关心的是使全局变量扩展到单个文件之外

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

你可能想看看std::vector(可调整大小的数组)和std::map(这将允许你根据名称items["sword"].attackFoe(foe);查找项目)和std::set(这使得很容易看到玩家拥有什么(if (items.find("Vorpal Hand Grenade") != items.end()) BlowStuffUp();),而不是每次都要搜索每个项目。