如何使一个函数永久地改变全局数组
How to make a function change a global array permanently
最近我一直致力于一款基于文本的游戏的库存系统,该系统使用一个全局数组作为库存系统,并使用相应的函数来读取该数组中的真或假。我遇到的问题是,我用来修改数组
的函数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();
),而不是每次都要搜索每个项目。
- 为什么在全局范围内使用"extern int a"似乎不行?
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 当vector是tje全局变量时,c++中vector的内存管理
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 变量没有改变?通过向量的函数调用
- 如何创建一个空的全局类并在启动时实例化它
- 内联函数中具有内部链接的全局变量
- 为什么虚函数不能是静态的和全局的?
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- 如何改变c++应用程序的视觉效果
- 全局作用域中函数指针的赋值
- C++在不同线程中改变向量
- g++用户定义的动态链接库上的全局new和delete运算符
- 在命名空间中使用全局命名空间中的函数
- 如何访问和改变存储在矢量C++中的对象
- 全局变量的顺序会改变C /OpenGL的性能
- 告诉gcc优化器值在全局函数中改变了
- 如何使一个函数永久地改变全局数组
- 模板函数内的全局值不改变[c++]
- 在C中声明全局变量会改变入口点吗?