1

我还是 C++ 新手,所以如果我的问题非常明显,请原谅我。我正在(尝试)编写一个 RPG 游戏。目前一切都很好地编译,但是我现在必须考虑如何在游戏打开时加载我的所有对象(对于单元类、项目等)。

我已经看到有关在头文件中使用 extern 的 DLL 和全局对象的线程,但我想知道哪个是最实用/最有效的方法?(因为游戏本身只会这么大)

就目前而言,我的代码非常笨拙且实现得不好(在尝试使用函数在单独的函数中声明我的对象并想知道为什么它们不可访问之前我什至没有想过),我应该使用 extern 而不是必须在 main() 中声明我的对象才能实例化它们(见下文)。

#include <iostream>
#include <string>
#include "Unit.h"
#include "UClass.h"

using namespace std;

// Seperate function at program start to load class list.
void loadClass(UClass& obj)
{
    obj.load("Peasant");

}

int main() {

    //Load classes...
    cout << "Creating peasant...\n";
    UClass peasant;
    loadClass(peasant);
    cout << peasant.show() << endl;

    cout << "\nCreating unit...\n";
    Unit man;
    man.SetLvl();
    man.SetClass(peasant);
    man.ShowInfo();

    return 0;
}

再次抱歉,这是我第一次尝试多文件项目,提前感谢您!

4

1 回答 1

0

Take a look into std library where you have defined such structures as for example list. My idea would be for you to create list of objects that you load to your game, and while in resolving geometry or other logic you want you would iterate through the list. I think this is good for start. If I got your concerns correctly.

于 2013-04-20T07:48:36.593 回答