如何向vector添加类类型对象?UE4井字棋开发报错求助
Hey there! Let's work through your Tic-Tac-Toe development issues with Unreal Engine 4 and Visual Studio. I'll cover both fixing your error (based on common UE4 pitfalls) and how to properly add your block objects to a vector (or UE4's recommended TArray).
First: Adding Class Objects to a Vector (or TArray)
Unreal Engine uses garbage collection for UObjects/Actor classes, so you can't store them by value in a container—you need to use pointers. Here's how to do it right, depending on which container you're using:
Option 1: Using std::vector (standard C++)
First, declare your vector as a pointer type for your block class (e.g., ATicTacToeBlock):
// In your header file (as a member variable of your GameMode/Board Actor) std::vector<ATicTacToeBlock*> BoardBlocks;
Then, when spawning your blocks, validate the spawned pointer before adding it:
// In your spawn loop ATicTacToeBlock* NewBlock = GetWorld()->SpawnActor<ATicTacToeBlock>(BlockClass, SpawnLocation, SpawnRotation); if (NewBlock) { // Always check for null! Spawn can fail (e.g., invalid class) BoardBlocks.push_back(NewBlock); // Optional: Store row/column index on the block for win condition checks later NewBlock->SetBoardPosition(Row, Column); }
Option 2: Using UE4's TArray (Recommended)
Unreal has its own container system that plays nicely with garbage collection. Declare your array with UPROPERTY() to ensure Unreal tracks references (prevents blocks from being accidentally garbage collected):
// In your header file UPROPERTY() TArray<ATicTacToeBlock*> BoardBlocks; // If you want to edit the array in the editor, add EditAnywhere: // UPROPERTY(EditAnywhere, Category = "Tic Tac Toe")
Adding blocks is similar, using the Add() method:
ATicTacToeBlock* NewBlock = GetWorld()->SpawnActor<ATicTacToeBlock>(BlockClass, SpawnLocation, SpawnRotation); if (NewBlock) { BoardBlocks.Add(NewBlock); NewBlock->SetBoardPosition(Row, Column); }
Troubleshooting Common Errors (Based on UE4 Spawn/Container Issues)
Since I don't have your error screenshot, here are the most frequent problems that cause errors when adding blocks to a container:
- Type Mismatch: If your vector is declared as
std::vector<ATicTacToeBlock>(value type) instead of pointer type, you'll get a compile error. UE4 Actors/UObjects can't be copied by value—always use pointers. - Null Pointer Addition: If
SpawnActorreturnsnullptr(e.g., you forgot to assign theBlockClassin the editor), adding it to the container will cause crashes later. Always include thatif (NewBlock)check. - Garbage Collection Issues: If your container is a local variable, Unreal's GC might delete your blocks when the function ends. Make sure the container is a member variable of an Actor/Object, and use
UPROPERTY()forTArrayto track references. - Access Permissions: If you're trying to set variables on your block class that aren't
publicor marked withUPROPERTY(), you'll get compile errors. Expose necessary variables/functions in your block's header:// In ATicTacToeBlock.h UPROPERTY(VisibleAnywhere, Category = "Board Position") int32 RowIndex; UPROPERTY(VisibleAnywhere, Category = "Board Position") int32 ColIndex; public: void SetBoardPosition(int32 Row, int32 Col) { RowIndex = Row; ColIndex = Col; }
Quick Win Condition Prep
Once your blocks are stored in the container, you can easily iterate through them to check for wins. For example, to check all rows:
bool CheckForWin() { const int32 BoardSize = 3; // Check rows for (int32 Row = 0; Row < BoardSize; Row++) { ATicTacToeBlock* FirstBlock = nullptr; bool RowMatch = true; for (int32 Col = 0; Col < BoardSize; Col++) { // Find the block at this row/column (you can map indices or store them directly) ATicTacToeBlock* CurrentBlock = GetBlockAtPosition(Row, Col); if (!CurrentBlock) { RowMatch = false; break; } if (!FirstBlock) { FirstBlock = CurrentBlock; } else if (FirstBlock->GetCurrentPlayer() != CurrentBlock->GetCurrentPlayer()) { RowMatch = false; break; } } if (RowMatch && FirstBlock) { return true; // We have a winner! } } // Repeat logic for columns and diagonals... return false; }
内容的提问来源于stack exchange,提问作者Damonlaws

