Accessing Variables In Struct Array Outside of Scope

92 Views Asked by At

I am brand new to SO, and it all looks very helpful.

My code is being used for cryengine, but this seems to be a good all around c++ problem. And lets face it, the official CE forums blow.

The trouble I'm having is accessing the const char* variable of a struct array outside of the scope I'm assigning the variable in.

BuildingManager.h    

class CBuildingManager  {
public:
     struct SBuilding {
        const char* name;
    };

    struct SBuilding buildings[999];

    //string buildingName;
    const char* buildingList[999];

};

BuildingManager.cpp

void CBuildingManager::LoadBuildingXML() {
    int n = -1;

    const char *name;
    const char *value;

    //XML iterator is long and not necessary for example. n++ per iteration. 
    //it just works


                //last part of iterator
                for (size_t j = 0; j < tags->getNumAttributes(); j++) {

                    //Get building name in XML. This works
                    tags->getAttributeByIndex(j, &name, &value);

                    //assign building name to name in struct
                    buildings[n].name = value;

                    CryLog("%s", buildings[n].name);

                    buildingList[n] = buildings[n].name;
                }
            }   
        }
    }
}

void CBuildingManager::LogAction(int x) {

    //x modified by input. also works
    CryLog("%c", buildingList[x]); //this, however, does not

}

So basically, I can print the building name as a string inside of the iterator, and it prints the whole building name (ie. "House")

But when I call LogAction, the building name will only print as a char, and will only show a single random symbol.

How can I convert the name in the struct to a string or otherwise get it to print as a whole word outside of the iterator?

Please let me know if my question is vague or shaky, and I will do my best to clean it up.

-Moose

0

There are 0 best solutions below