Help me with Unreal engine 5 multiplayer in c++ code.
There is interactive item on level. Which player can look at and pick it up. When it in level there is marker on it and it glow yellow. When player look at it marker disabling and it start glow green locally, when player stop looking it return to base state with marker and yellow glow. All this happening locally for player. But when we pickup it some logic goes for everyone and this doesn't works properly on listen server. I want to rewrite code and don't use this function HasHighlightedActionOfType(FSV_InteractionAction::Touch) is StartInteractive function because this works bad on listen server. I will provide you code which handle what i describe (when look at starttouch called, when stop look at endtouch called, when pickup called endTOuch and then startinteractive, when drop item called stopInteractive):
void
USV_HighlightInteractHandler::StartInteractive_Implementation(bool IsSlotLocallyControlled)
{
OnHand = true;
if(IsSlotLocallyControlled) SetEnableHighlightState(false);
else
if (!HasHighlightedActionOfType(FSV_InteractionAction::Touch)) SetEnableHighlightState(false);
}
Certainly! I'd be happy to help you with your Unreal Engine 5 multiplayer code in C++. From your description, it seems you're encountering issues with the HasHighlightedActionOfType(FSV_InteractionAction::Touch) function in the StartInteractive method, especially when running on a listen server. Let's work through the problem and find a solution.
Understanding the Problem:
Item States:
Base State: When the item is in the level (not picked up), it has a marker and glows yellow. This is visible to all players.
Looking at the Item: When a player looks at the item, the marker is disabled, and the item starts to glow green. This happens locally for the player looking at the item.
Not Looking at the Item: When the player stops looking, it returns to the base state with the marker and yellow glow.
Pickup Logic:
When a player picks up the item, some logic should execute for everyone (e.g., the item disappears from the world and possibly appears in the player's hand).
However, on a listen server, this logic isn't working properly, likely due to the use of HasHighlightedActionOfType(FSV_InteractionAction::Touch).
Objective:
Rewrite the code to avoid using HasHighlightedActionOfType(FSV_InteractionAction::Touch) in StartInteractive because it doesn't work properly on listen servers.
Ensure that the item interaction works correctly in a multiplayer environment, especially on listen servers.
Solution:
To achieve the desired behavior and fix the issues on a listen server, we'll:
Remove dependencies on HasHighlightedActionOfType.
Ensure proper handling of local and replicated logic.
Use Unreal Engine's networking features correctly.
Let's rewrite the code step by step.
1. Remove HasHighlightedActionOfType from StartInteractive
Given that HasHighlightedActionOfType(FSV_InteractionAction::Touch) is causing issues, we'll remove it and adjust the logic accordingly.
Check IsSlotLocallyControlled to ensure these changes happen only for the local player.
In StartTouch:
Update the material to glow green.
Disable the marker locally.
Enable highlight state locally.
In EndTouch:
Reset the material to stop glowing.
Enable the marker locally.
Set highlight state based on IsHighlightEnable.
This ensures that when a player looks at the item, the changes are local to that player and don't affect others.
4. Handle Replication Correctly
Proper replication is crucial for multiplayer gameplay. We need to make sure that any variables that should be synchronized across clients are replicated correctly.
// .cpp file
void USV_HighlightInteractHandler::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(USV_HighlightInteractHandler, OnHand);
}
void USV_HighlightInteractHandler::OnRep_OnHand()
{
if (OnHand)
{
// Item was picked up
SetEnableHighlightState(false);
SetMarkerEnabled(false);
}
else
{
// Item was dropped
SetMarkerEnabled(true);
SetEnableHighlightState(IsHighlightEnable);
}
}
Explanation:
Use UPROPERTY(ReplicatedUsing=...) to replicate the OnHand variable.
Implement GetLifetimeReplicatedProps to notify Unreal Engine about the variable.
Implement OnRep_OnHand to handle changes when OnHand updates on clients.
In OnRep_OnHand, update the item's state based on whether it's in hand.
5. Ensure Server Authorship
In multiplayer games, the server should be authoritative about the game state. This means that state changes like picking up or dropping an item should be initiated on the server.
After making these changes, thoroughly test your game:
On a Listen Server: Host a game as a player and ensure interactions work correctly.
On a Dedicated Server: Run a dedicated server and connect multiple clients.
Edge Cases: Test scenarios like two players interacting with the same item simultaneously.
8. Additional Tips
Logging: Use UE_LOG statements to debug and track the flow of your code during gameplay.
Replication Conditions: If certain variables should only replicate under specific conditions, use replication conditions.
Authority Checks: Always be cautious with HasAuthority() checks to ensure code runs where it's supposed to.
Conclusion
By removing the HasHighlightedActionOfType function and restructuring your code to use proper server-client authority and replication, you should resolve the issues you're experiencing with the listen server. The key points are:
Local interactions (like looking at the item) are handled locally and affect only the local player's view.
Server interactions (like picking up the item) are initiated on the server and replicated to all clients.
Replication is used effectively to keep all clients in sync regarding the item's state.
Feel free to let me know if you have any questions or need further assistance with any part of the code!