Skip to content

Commit

Permalink
Merge pull request #15 from DerpyNewbie/feature/object-pool-spawn-ove…
Browse files Browse the repository at this point in the history
…rride

Add spawn override for ObjectPoolSpawner
  • Loading branch information
DerpyNewbie authored Nov 3, 2024
2 parents a6cf58c + 83719eb commit 177bb43
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace DerpyNewbie.Common.ObjectPool
public class ObjectPoolProxy : UdonSharpBehaviour
{
private VRCObjectPool _pool;

[SerializeField]
private Transform spawnTarget;

Expand All @@ -33,26 +34,29 @@ public void ReturnAll()
SendCustomNetworkEvent(NetworkEventTarget.Owner, nameof(OwnerOnly_ReturnAll));
}

public void OwnerOnly_Spawn()
[CanBeNull]
public GameObject OwnerOnly_Spawn()
{
if (!Networking.IsOwner(gameObject))
return;
return null;

var obj = _pool.TryToSpawn();
if (obj == null)
{
Debug.LogError("[ObjectPoolProxy] Could not spawn object");
return;
return null;
}

if (spawnTarget != null)
obj.transform.SetPositionAndRotation(spawnTarget.position, spawnTarget.rotation);

return obj;
}

public void OwnerOnly_ReturnAll()
public bool OwnerOnly_ReturnAll()
{
if (!Networking.IsOwner(gameObject))
return;
return false;

var objs = _pool.Pool;
foreach (var o in objs)
Expand All @@ -62,6 +66,8 @@ public void OwnerOnly_ReturnAll()
o.GetComponent<VRC_Pickup>().Drop();
_pool.Return(o);
}

return true;
}

public void OwnerOnly_Return(GameObject target)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ MonoBehaviour:
Data:
- Name:
Entry: 12
Data: 1
Data: 2
- Name:
Entry: 7
Data:
Expand Down Expand Up @@ -111,6 +111,66 @@ MonoBehaviour:
- Name:
Entry: 8
Data:
- Name:
Entry: 7
Data:
- Name: $k
Entry: 1
Data: spawnOverride
- Name: $v
Entry: 7
Data: 7|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: spawnOverride
- Name: <UserType>k__BackingField
Entry: 7
Data: 8|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: UnityEngine.Transform, UnityEngine.CoreModule
- Name:
Entry: 8
Data:
- Name: <SystemType>k__BackingField
Entry: 9
Data: 8
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
- Name:
Entry: 6
Data:
- Name:
Entry: 8
Data:
- Name: <IsSerialized>k__BackingField
Entry: 5
Data: true
- Name: _fieldAttributes
Entry: 7
Data: 9|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 1
- Name:
Entry: 7
Data: 10|UnityEngine.SerializeField, UnityEngine.CoreModule
- Name:
Entry: 8
Data:
- Name:
Entry: 13
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 13
Data:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using JetBrains.Annotations;
using UdonSharp;
using UnityEngine;
using VRC.SDK3.Components;
using VRC.SDKBase;
using VRC.Udon.Common.Interfaces;

namespace DerpyNewbie.Common.ObjectPool
{
[UdonBehaviourSyncMode(BehaviourSyncMode.None)]
public class ObjectPoolSpawner : UdonSharpBehaviour
{
[SerializeField]
private ObjectPoolProxy targetProxy;
[SerializeField] private ObjectPoolProxy targetProxy;
[SerializeField] private Transform spawnOverride;

public override void Interact()
{
Expand All @@ -18,7 +21,23 @@ public override void Interact()
[PublicAPI]
public void TryToSpawn()
{
targetProxy.TryToSpawn();
SendCustomNetworkEvent(NetworkEventTarget.All, nameof(Internal_Networked_SpawnGlobally));
}

public void Internal_Networked_SpawnGlobally()
{
var obj = targetProxy.OwnerOnly_Spawn();
if (obj == null) return;

if (spawnOverride != null)
{
if (!Networking.IsOwner(obj)) Networking.SetOwner(Networking.LocalPlayer, obj);

var objSync = obj.GetComponent<VRCObjectSync>();
if (objSync != null) objSync.FlagDiscontinuity();

obj.transform.SetPositionAndRotation(spawnOverride.position, spawnOverride.rotation);
}
}
}
}

0 comments on commit 177bb43

Please sign in to comment.