TwinShooter multiplayer, how should thiss be done?

Hi, I’ve been working in the twinshooter project to make my own and I need to start getting multiplayer working.

I’ve got almost no experience in networking but all I need to be able to do, for now, is replicate the ships location, which is APawn, and my projectiles location and the sships stats, health, shields and energy (all just floats), across the server to the clients.

I’ve set bReplicates, and bReplicateMovement but I understand I need to somehow inform the clients that this has changed. I’ve also got this for the values that I would need to send/recieve:

void AShip::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const {
	DOREPLIFETIME(AShip, health);
	DOREPLIFETIME(AShip, shield);
	DOREPLIFETIME(AShip, energy);
	DOREPLIFETIME(AShip, location);	
}

What should I do, in order to acheive this?

I’m not quite sure if the Movement is replicated for Pawns too (not just Characters), but even if not, you can do the following:

For your variables, you create an OnRep Function. For each!

So something like that:

Headerfile:

UPROPERTY(ReplicatedUsing=OnRep_LocationChanged, OTHER OPTIONAL PARAMETERS)
FVector location;




UFUNCTION()
void OnRep_LocationChanged();


CPP File:

void AShip::OnRep_LocationChanged()
{
      SetActorLocation(location);
}

What this does is: As soon as the Server changes the replicated variable “location”, the “OnRep_LocationChanged” function is called on the clients. And in it, you set the new ActorLocation to the changed location variable.

The OnRep will get called as soon as your Client receives the updated value.

Your health, shield and energy should work fine without an OnRep function, so a simple UPROPERTY(Replicated) is enough in the header.

Ok so that sort of works in some places. Currently I’m just going into the editor and hitting play with 2 people. one of them is replicated the other isn’t. not 100% if I’m doing it right, but it’s a great start thanks, I have a better understanding of it now.

I guess the server is not working or? You will need to call SetActorLocation(location) for him extra after changing the location variable, because OnRep is not called on the server.

I’ll have a look into it mate. If I find out I’ll tell you how I did it.