[Solved] Trouble Finding Pawns from GameMode

Hey guys, let me try to keep this short:

In GameMode::BeginPlay I have the following:



TActorIterator<AContagionPawn> PawnIterator(GetWorld());

for (auto const Pawn = *PawnIterator; PawnIterator; ++PawnIterator)
{
    Pawns.Add(Pawn);
}


**No matter how I do it, it seems that PawnIterator only gets a single pawn when I actually have two in the scene. What is wrong? **

(I’m also seeing an issue when I get two entries from the Iterator, but both of them point to the same pawn)


Now for the longer discussion (you may skip this):

  • I do have some suspicions - I’ve read that GameMode runs on the server, and I don’t really know if searching for actors in the game world from it is alright.
  • I moved this code around a bit - I’m just trying it on a custom GameState class, which is supposed to run both on server and clients, and now I get the duplicated items issue.

What is “Pawns” ? Is it a global or static variable? If so, it will not work right when running client and server (or editor) in a single process.

Another question, why keep track of these Pawns? I’m only asking because most likely you are trying to approach the problem from a wrong angle.

The “Pawn” is just my specialized version of APawn. In this particular instance, I have two pawns in the game representing Player 1 and Player 2. As you can see from my code snippet, I’m using a TActorIterator to retrieve them from the World. I do this only once at game startup.

I might as well be! I’m new to Unreal but I’m really interested in following the Game Framework correctly. I’m building a very simple two-player board game that should be played locally with a single mouse. For that, I have a single player controller and two pawns. The pawns interact with the game board, while the controller swaps between the pawns (possess and un-possess) each turn.

My approach was to have the GameMode subscribe to a TurnEnded event (which right now is located at the pawn) and then switch between pawns. I thought that the GameMode would be the place to write this kind of broad gameplay rules, such as switching players, so: where should I write this kind of game logic? Consider that right now I’m creating a local game, but I’ll later on make it online multiplayer.

And guys, if GameMode isn’t the place to do this, can someone elaborate why it isn’t finding the pawns in the GameWorld? Does it have to do with online replication?

Thanks for the help!

[quote]
The “Pawn” is just my specialized version of APawn

[quote]

I didn’t ask about [FONT=Courier New]Pawn, I asked about the collection [FONT=Courier New]Pawns that you add them to.

You should be able to get your pawns like this



for (auto PCIter = GetWorld()->GetPlayerControllerIterator(); PCIter; ++PCIter)
{
    const APlayerController* YourPC = PCIter->Get();

   //can use either one
    Pawns.Add(YourPC->GetPawn());
    Pawns.AddUnique(YourPC->GetPawn());
}

That is because they are depreciating the pawn GetPawnIterator()