Hello UE4 Community!
We’re in the process of creating a basic multiplayer online game and we decided to make some of the actions available to the players to be controlled by different classes that we set as “modules” for the PlayerController.
To show you the issue we’re facing I’ll use one of these classes as an example: ChatModule.
So we have our class ChatModule inherited from AInfo. When the player connects to the server we need to instance ChatModule and set a reference to that instance in a variable in the PlayerController. In this case I’m using the variable AChatModule* ChatModule;
In the BeginPlay method of PlayerController I’m doing:
FActorSpawnParameters SpawnInfo;
SpawnInfo.Owner = this;
ChatModule = GetWorld()->SpawnActor<AChatModule>(AChatModule::StaticClass(), SpawnInfo);
The issue with this is that ChatModule is being instanced one time in the server but two times in the client. Would be great if someone can tell me why.
The next thing I tried is this:
if (HasAuthority())
{
FActorSpawnParameters SpawnInfo;
SpawnInfo.Owner = this;
ChatModule = GetWorld()->SpawnActor<ASChatModule>(ASChatModule::StaticClass(), SpawnInfo);
}
Now the issue is that ChatModule is being set only on the server. ChatModule variable on the client is null, so I can’t get a reference to the AChatModule instance from the client.
I resolved this by adding the following method to PlayerController:
void ASPlayerController::SetChatModule(ASChatModule* ChatModule)
{
this->ChatModule = ChatModule;
}
and in the chatModule BeginPlay() method I made this:
void ASChatModule::BeginPlay()
{
Super::BeginPlay();
((ASPlayerController*)this->GetOwner())->SetChatModule(this);
}
This is working as expected, but doesn’t seems to be the right way to do what we need.
How should we do this?