Multiple files

This is a rough idea of my code, What I have now works wonderfully. My problem is it takes 6-8 minutes to compile every time I need to make a minor change.
I looked into splitting this into multiple files. I have tried to do it, and I get errors, lots and lots of errors... I was thinking a file for the rooms, a file for the verbs, and a file for the nouns. I've tried .cpp files, .h files, and every which a way. I get errors from file not found, not declared in this scope, wrong type, not defined and so on.. Once I swear my compiler asked me if I was stoned or just plain stupid...

I'm obviously not doing something right. I'm not even sure this is the right answer for my problem. May I impose on someone to show me how to do this properly please? I thank you in advance.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <cctype>
#include <Windows.h>

using namespace std;

enum en_DIRS {NORTH, SOUTH, EAST, WEST, NORTHEAST, SOUTHEAST, NORTHWEST, SOUTHWEST, UP, DOWN}
enum en_ROOMS { LIMBO,  other room names listed here  };
enum en_VERBS { GET , DROP , GO , other verbs here    };
enum en_NOUNS{ Nouns are listed here};

const int NONE = -1;
const int DIRS = 10;
const int ROOMS = 373;
const int VERBS = 31;
const int NOUNS = 92;


struct word
{
string word;
int code;
};

struct room
{
    string shdesc;
    string description;
    string room_name;
    int exits_to_room[DIRS];
    string comein;
    string goout;
    int flag1;
    int flag2;
};

struct noun
{
    string word;
    string description;
    string verbose;
    int code;
    int location;
    bool can_carry;
    string pickup;
    string dropoff;
    bool flag;
};

//  Room data 

void set_rooms(room *rms)
{
    rms[LIMBO].room_name.assign("LIMBO");  
    rms[LIMBO].shdesc.assign("");
    rms[LIMBO].description.assign("");
    rms[LIMBO].exits_to_room[NORTH] = NONE;
    rms[LIMBO].exits_to_room[EAST] = NONE;
    rms[LIMBO].exits_to_room[SOUTH] = NONE;
    rms[LIMBO].exits_to_room[WEST] = NONE;
    rms[LIMBO].exits_to_room[NORTHEAST] = NONE;
    rms[LIMBO].exits_to_room[SOUTHEAST] = NONE;
    rms[LIMBO].exits_to_room[SOUTHWEST] = NONE;
    rms[LIMBO].exits_to_room[NORTHWEST] = NONE;
    rms[LIMBO].exits_to_room[UP] = NONE;
    rms[LIMBO].exits_to_room[DOWN] = NONE;
    rms[LIMBO].comein.assign ("");
    rms[LIMBO].goout.assign("");
    rms[LIMBO ].flag1=0;
    rms[LIMBO].flag2=0;

//   and on and on....
}
 
Last edited on
The problem is that you are hard-coding level layouts. It takes a long time to compile because not only are you essentially embedding entire levels into your application, you're doing it using a language which is nutorious for long compilation times.

Instead, devise a level format that you can load from files at runtime. You can change the levels without recompiling your program this way.

Ok...

I thought that is what I was asking for, to put the rms data into a separate file so it would compile faster... otherwise, you advice escapes me. Can you please give me a small example, some pseudo code or something to get my brain engaged in that direction?
I do not mean separate source code file, I mean separate data file - it would not be compiled into the program.
LB, I thank you for your advice, I am not unappreciative I assure you. As i stated in my original post, I have tried making separate data files, and failed. That's why I came here. Your answer "devise a level format that you can load from files at runtime." is like Someone asking why the space shuttle didn't launch and you telling them "You need to use the dew hickey"...
Though you answered the question, (Thank you), but you also leave me at the same place I was before I posted the original question. That's why I asked for "a small example, some pseudo code or something" Could you please be just a tad more specific? Like
" Make an .dat file, put the pointer array in their, make sure it reads this way, or that way..."
I'm not asking you to write my program, but more or less give me a guide that I can use.
Thank you so much sir...


level.txt:
Diamond Shovel
Saddle
Milk Bucket
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

int main()
{
    std::vector<std::string> items;

    {
        std::ifstream in ("level.txt");
        std::string item;
        while(std::getline(in, item))
        {
            items.push_back(item);
        }
    }

    std::cout << "Loaded " << items.size() << " items from level.txt" << std::endl;

    while(true)
    {
        std::cout << "Which item do you want to look at? " << std::flush;
        std::size_t num;
        std::cin >> num;
        if(num < items.size())
        {
            std::cout << "Item #" << num << " is " << items[num] << std::endl;
        }
        else
        {
            std::cout << "There is no item #" << num << std::endl;
        }
    }
}
Obviously it's not exactly what someone would consider a level, but it is a start. Ask a question on every line you do not understand.
Last edited on
Topic archived. No new replies allowed.