Download

Question on scope

Sorry if this is a dumb question but been out of C++ for a few years.

So say I have a struct that has members of itself like so:



struct audioSelectTreeItem {
        FString folderName;
        FString folderPath;
        struct audioSelectTreeItem* folderParentItem;
        TArray<struct audioSelectTreeItem*> childFolderItems;
    };


Then inside a function there is a loop that adds more structs recursively. Simplified example:



void AMyPlayerController::showAudioPicks() {
...
     for (int counter = startCount; counter < fileStringArray.Num(); counter++) {
            audioSelectTreeItem newTreeItem;
            newTreeItem.folderPath = fileStringArray[counter];
            mainListTree.childFolderItems.Add(&newTreeItem);
     ...
     }


Now if I try to access the mainListTree.childFolderItems in another function it wouldn’t work because they don’t exist. However if I add them to a TArray:



for (int counter = startCount; counter < fileStringArray.Num(); counter++) {

            audioSelectTreeItem newTreeItem;
            newTreeItem.folderPath = fileStringArray[counter];
            currentTreeArray.Add(newTreeItem);


When I try to look at currentTreeArray[index] there is something there. Does a TArray create a copy of a temporary variable then or is this just memory chance and it’s going to break?

I’m basically trying to read a file that has folders and subfolders a user selected earlier for music. These need to be in some variable or container during the game instead of reading from drive every time.