Przeglądaj źródła

Altering the method used to spawn network prefabs to use the standard multiplayer prefab spawning interface. This avoids issues related to the ticketing system.

Signed-off-by: kberg-amzn <[email protected]>
kberg-amzn 2 lat temu
rodzic
commit
4822107362

+ 1 - 2
Gem/Code/Source/AutoGen/EnergyCannonComponent.AutoComponent.xml

@@ -11,13 +11,12 @@
     <Include File="Source/Effects/GameEffect.h" />
 
     <ComponentRelation Constraint="Required" HasController="true" Name="NetworkTransformComponent" Namespace="Multiplayer" Include="Multiplayer/Components/NetworkTransformComponent.h" />
-    <ComponentRelation Constraint="Required" HasController="false" Name="NetworkPrefabSpawnerComponent" Namespace="MultiplayerSample" Include="Source/Components/PerfTest/NetworkPrefabSpawnerComponent.h" />
 
     <ArchetypeProperty Type="AZ::TimeMs" Name="RateOfFireMs" Init="AZ::TimeMs{ 0 }" Container="Object" ExposeToEditor="true" Description="Specifies the rate in milliseconds at which to fire projectiles." />
     <ArchetypeProperty Type="AZ::TimeMs" Name="BuildUpTimeMs" Init="AZ::TimeMs{ 0 }" Container="Object" ExposeToEditor="true" Description="Specifies the number of milliseconds to start the build-up effect before shooting the next energy ball." />
     <ArchetypeProperty Type="GameEffect" Name="FiringEffect" Init="" Container="Object" ExposeToEditor="true" Description="Specifies the effect to play upon firing." />
     <ArchetypeProperty Type="AZ::Vector3" Name="FireVector" Init="AZ::Vector3::CreateZero()" Container="Object" ExposeToEditor="true" Description="The direction of fire for projectiles." />
-    <ArchetypeProperty Type="AzFramework::SpawnableAsset" Name="ProjectileSpawnable" ExposeToEditor="true" Description="The projectile asset to spawn." />
+    <ArchetypeProperty Type="Multiplayer::NetworkSpawnable" Name="ProjectileSpawnable" ExposeToEditor="true" Description="The projectile asset to spawn." />
 
     <RemoteProcedure Name="RPC_TriggerBuildup" InvokeFrom="Authority" HandleOn="Client" IsPublic="true" IsReliable="true" GenerateEventBindings="true" Description="Triggered on clients to start the buildup event." />
 </Component>

+ 21 - 27
Gem/Code/Source/Components/Multiplayer/EnergyCannonComponent.cpp

@@ -86,34 +86,28 @@ namespace MultiplayerSample
         const AZ::Vector3 ballPosition = cannonTm.TransformPoint(effectOffset);
         const AZ::Vector3 forward = cannonTm.TransformVector(GetFireVector());
 
-        PrefabCallbacks callbacks;
-        callbacks.m_onActivateCallback = [this, ballPosition, forward](AZStd::shared_ptr<AzFramework::EntitySpawnTicket> ticket, AzFramework::SpawnableConstEntityContainerView view)
+        const Multiplayer::PrefabEntityId prefabEntityId(AZ::Name(GetProjectileSpawnable().m_spawnableAsset.GetHint().c_str()));
+
+        const AZ::Transform transform = AZ::Transform::CreateFromQuaternionAndTranslation(AZ::Quaternion::CreateIdentity(), ballPosition);
+
+        Multiplayer::INetworkEntityManager::EntityList entityList =
+            Multiplayer::GetNetworkEntityManager()->CreateEntitiesImmediate(prefabEntityId, Multiplayer::NetEntityRole::Authority, transform);
+
+        Multiplayer::NetworkEntityHandle spawnedEntity;
+        if (!entityList.empty())
+        {
+            spawnedEntity = entityList[0];
+        }
+        else
         {
-            if (view.empty())
-            {
-                return;
-            }
-
-            const auto ticketId = ticket->GetId();
-            for (const AZ::Entity* entity : view)
-            {
-                if (EnergyBallComponent* ballComponent = entity->FindComponent<EnergyBallComponent>())
-                {
-                    ballComponent->RPC_LaunchBall(ballPosition, forward, GetNetEntityId());
-                    m_triggerBuildupEvent.Enqueue(GetRateOfFireMs() - GetBuildUpTimeMs(), false);
-                }
-            }
-
-            // Save the spawn ticket, otherwise the prefab will immediately despawn due to the ticket's destruction
-            m_spawnedProjectiles.insert(AZStd::make_pair(ticketId, AZStd::move(ticket)));
-        };
-
-        GetParent().GetNetworkPrefabSpawnerComponent()->SpawnPrefabAsset
-        (
-            AZ::Transform::CreateFromQuaternionAndTranslation(AZ::Quaternion::CreateIdentity(), ballPosition),
-            GetProjectileSpawnable(),
-            AZStd::move(callbacks)
-        );
+            AZLOG_WARN("Attempt to spawn prefab %s failed. Check that prefab is network enabled.", prefabEntityId.m_prefabName.GetCStr());
+        }
+
+        if (EnergyBallComponent* ballComponent = spawnedEntity.FindComponent<EnergyBallComponent>())
+        {
+            ballComponent->RPC_LaunchBall(ballPosition, forward, GetNetEntityId());
+            m_triggerBuildupEvent.Enqueue(GetRateOfFireMs() - GetBuildUpTimeMs(), false);
+        }
     }
 #endif
 }

+ 0 - 2
Gem/Code/Source/Components/Multiplayer/EnergyCannonComponent.h

@@ -55,8 +55,6 @@ namespace MultiplayerSample
         {
             OnFireEnergyBall();
         }, AZ::Name("FireEnergyCannon")};
-
-        AZStd::unordered_map<AzFramework::EntitySpawnTicket::Id, AZStd::shared_ptr<AzFramework::EntitySpawnTicket>> m_spawnedProjectiles;
 #endif
     };
 }

+ 7 - 12
Prefabs/BubbleCannon.prefab

@@ -125,11 +125,13 @@
                             0.8659999966621399
                         ],
                         "ProjectileSpawnable": {
-                            "assetId": {
-                                "guid": "{10056BEC-1313-5F27-90E0-9BE6E69A171D}",
-                                "subId": 1668259951
-                            },
-                            "assetHint": "prefabs/bubbleball.spawnable"
+                            "Spawnable": {
+                                "assetId": {
+                                    "guid": "{10056BEC-1313-5F27-90E0-9BE6E69A171D}",
+                                    "subId": 2235376842
+                                },
+                                "assetHint": "prefabs/bubbleball.network.spawnable"
+                            }
                         }
                     }
                 },
@@ -150,13 +152,6 @@
                     "Id": 3533828349650165605,
                     "Parent Entity": "ContainerEntity"
                 },
-                "Component_[372014937584893874]": {
-                    "$type": "GenericComponentWrapper",
-                    "Id": 372014937584893874,
-                    "m_template": {
-                        "$type": "NetworkPrefabSpawnerComponent"
-                    }
-                },
                 "Component_[398718130345139058]": {
                     "$type": "EditorEntitySortComponent",
                     "Id": 398718130345139058

+ 7 - 12
Prefabs/Energy_Cannon_with_Energy_Ball.prefab

@@ -125,11 +125,13 @@
                             0.0
                         ],
                         "ProjectileSpawnable": {
-                            "assetId": {
-                                "guid": "{EF1C280A-0044-5786-918F-8613DC58EF30}",
-                                "subId": 1578967396
-                            },
-                            "assetHint": "prefabs/energy_ball.spawnable"
+                            "Spawnable": {
+                                "assetId": {
+                                    "guid": "{EF1C280A-0044-5786-918F-8613DC58EF30}",
+                                    "subId": 2851397279
+                                },
+                                "assetHint": "prefabs/energy_ball.network.spawnable"
+                            }
                         }
                     }
                 },
@@ -153,13 +155,6 @@
                         "UniformScale": 2.0
                     }
                 },
-                "Component_[372014937584893874]": {
-                    "$type": "GenericComponentWrapper",
-                    "Id": 372014937584893874,
-                    "m_template": {
-                        "$type": "NetworkPrefabSpawnerComponent"
-                    }
-                },
                 "Component_[398718130345139058]": {
                     "$type": "EditorEntitySortComponent",
                     "Id": 398718130345139058