Skip to content

Commit adc1bb3

Browse files
feat: Add OnSpawned callback to Coordinated Spawns SpawnInfos (#551)
Add OnSpawned callback to coordinated SpawnInfo
1 parent 39b5499 commit adc1bb3

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

Nautilus/Handlers/CoordinatedSpawnsHandler.cs

+35-11
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,23 @@ public struct SpawnInfo : IEquatable<SpawnInfo>
7777
internal Vector3 Scale { get; }
7878
// For the sake of backwards compatibility, a scale of 0x0x0 is automatically converted to 1x1x1. Sorry, no 0x scale entities allowed.
7979
internal Vector3 ActualScale => Scale == default ? Vector3.one : Scale;
80+
internal Action<GameObject> OnSpawned { get; }
8081

8182
/// <summary>
8283
/// Initializes a new <see cref="SpawnInfo"/>.
8384
/// </summary>
8485
/// <param name="techType">TechType to spawn.</param>
8586
/// <param name="spawnPosition">Position to spawn into.</param>
8687
public SpawnInfo(TechType techType, Vector3 spawnPosition)
87-
: this(default, techType, spawnPosition, Quaternion.identity, Vector3.one) { }
88+
: this(default, techType, spawnPosition, Quaternion.identity, Vector3.one, null) { }
8889

8990
/// <summary>
9091
/// Initializes a new <see cref="SpawnInfo"/>.
9192
/// </summary>
9293
/// <param name="classId">ClassID to spawn.</param>
9394
/// <param name="spawnPosition">Position to spawn into.</param>
9495
public SpawnInfo(string classId, Vector3 spawnPosition)
95-
: this(classId, default, spawnPosition, Quaternion.identity, Vector3.one) { }
96+
: this(classId, default, spawnPosition, Quaternion.identity, Vector3.one, null) { }
9697

9798
/// <summary>
9899
/// Initializes a new <see cref="SpawnInfo"/>.
@@ -101,7 +102,7 @@ public SpawnInfo(string classId, Vector3 spawnPosition)
101102
/// <param name="spawnPosition">Position to spawn into.</param>
102103
/// <param name="rotation">Rotation to spawn at.</param>
103104
public SpawnInfo(TechType techType, Vector3 spawnPosition, Quaternion rotation)
104-
: this(default, techType, spawnPosition, rotation, Vector3.one) { }
105+
: this(default, techType, spawnPosition, rotation, Vector3.one, null) { }
105106

106107
/// <summary>
107108
/// Initializes a new <see cref="SpawnInfo"/>.
@@ -110,7 +111,7 @@ public SpawnInfo(TechType techType, Vector3 spawnPosition, Quaternion rotation)
110111
/// <param name="spawnPosition">Position to spawn into.</param>
111112
/// <param name="rotation">Rotation to spawn at.</param>
112113
public SpawnInfo(string classId, Vector3 spawnPosition, Quaternion rotation)
113-
: this(classId, default, spawnPosition, rotation, Vector3.one) { }
114+
: this(classId, default, spawnPosition, rotation, Vector3.one, null) { }
114115

115116
/// <summary>
116117
/// Initializes a new <see cref="SpawnInfo"/>.
@@ -120,7 +121,7 @@ public SpawnInfo(string classId, Vector3 spawnPosition, Quaternion rotation)
120121
/// <param name="rotation">Rotation to spawn at.</param>
121122
/// <param name="scale">Scale to spawn with.</param>
122123
public SpawnInfo(TechType techType, Vector3 spawnPosition, Quaternion rotation, Vector3 scale)
123-
: this(default, techType, spawnPosition, rotation, scale) { }
124+
: this(default, techType, spawnPosition, rotation, scale, null) { }
124125

125126
/// <summary>
126127
/// Initializes a new <see cref="SpawnInfo"/>.
@@ -130,7 +131,7 @@ public SpawnInfo(TechType techType, Vector3 spawnPosition, Quaternion rotation,
130131
/// <param name="rotation">Rotation to spawn at.</param>
131132
/// <param name="scale">Scale to spawn with.</param>
132133
public SpawnInfo(string classId, Vector3 spawnPosition, Quaternion rotation, Vector3 scale)
133-
: this(classId, default, spawnPosition, rotation, scale) { }
134+
: this(classId, default, spawnPosition, rotation, scale, null) { }
134135

135136
/// <summary>
136137
/// Initializes a new <see cref="SpawnInfo"/>.
@@ -139,7 +140,7 @@ public SpawnInfo(string classId, Vector3 spawnPosition, Quaternion rotation, Vec
139140
/// <param name="spawnPosition">Position to spawn into.</param>
140141
/// <param name="rotation">Rotation to spawn at.</param>
141142
public SpawnInfo(TechType techType, Vector3 spawnPosition, Vector3 rotation)
142-
: this(default, techType, spawnPosition, Quaternion.Euler(rotation), Vector3.one) { }
143+
: this(default, techType, spawnPosition, Quaternion.Euler(rotation), Vector3.one, null) { }
143144

144145
/// <summary>
145146
/// Initializes a new <see cref="SpawnInfo"/>.
@@ -148,7 +149,7 @@ public SpawnInfo(TechType techType, Vector3 spawnPosition, Vector3 rotation)
148149
/// <param name="spawnPosition">Position to spawn into.</param>
149150
/// <param name="rotation">Rotation to spawn at.</param>
150151
public SpawnInfo(string classId, Vector3 spawnPosition, Vector3 rotation)
151-
: this(classId, default, spawnPosition, Quaternion.Euler(rotation), Vector3.one) { }
152+
: this(classId, default, spawnPosition, Quaternion.Euler(rotation), Vector3.one, null) { }
152153

153154
/// <summary>
154155
/// Initializes a new <see cref="SpawnInfo"/>.
@@ -158,7 +159,7 @@ public SpawnInfo(string classId, Vector3 spawnPosition, Vector3 rotation)
158159
/// <param name="rotation">Rotation to spawn at.</param>
159160
/// <param name="scale">Scale to spawn with.</param>
160161
public SpawnInfo(TechType techType, Vector3 spawnPosition, Vector3 rotation, Vector3 scale)
161-
: this(default, techType, spawnPosition, Quaternion.Euler(rotation), scale) { }
162+
: this(default, techType, spawnPosition, Quaternion.Euler(rotation), scale, null) { }
162163

163164
/// <summary>
164165
/// Initializes a new <see cref="SpawnInfo"/>.
@@ -168,10 +169,32 @@ public SpawnInfo(TechType techType, Vector3 spawnPosition, Vector3 rotation, Vec
168169
/// <param name="rotation">Rotation to spawn at.</param>
169170
/// <param name="scale">Scale to spawn with.</param>
170171
public SpawnInfo(string classId, Vector3 spawnPosition, Vector3 rotation, Vector3 scale)
171-
: this(classId, default, spawnPosition, Quaternion.Euler(rotation), scale) { }
172+
: this(classId, default, spawnPosition, Quaternion.Euler(rotation), scale, null) { }
173+
174+
/// <summary>
175+
/// Initializes a new <see cref="SpawnInfo"/>.
176+
/// </summary>
177+
/// <param name="techType">TechType to spawn.</param>
178+
/// <param name="spawnPosition">Position to spawn into.</param>
179+
/// <param name="rotation">Rotation to spawn at.</param>
180+
/// <param name="scale">Scale to spawn with.</param>
181+
/// <param name="onSpawned">Callback that is used when the object is successfully spawned.</param>
182+
public SpawnInfo(TechType techType, Vector3 spawnPosition, Quaternion rotation, Vector3 scale, Action<GameObject> onSpawned)
183+
: this(default, techType, spawnPosition, rotation, scale, onSpawned) { }
172184

185+
/// <summary>
186+
/// Initializes a new <see cref="SpawnInfo"/>.
187+
/// </summary>
188+
/// <param name="classId">ClassID to spawn.</param>
189+
/// <param name="spawnPosition">Position to spawn into.</param>
190+
/// <param name="rotation">Rotation to spawn at.</param>
191+
/// <param name="scale">Scale to spawn with.</param>
192+
/// <param name="onSpawned">Callback that is used when the object is successfully spawned.</param>
193+
public SpawnInfo(string classId, Vector3 spawnPosition, Quaternion rotation, Vector3 scale, Action<GameObject> onSpawned)
194+
: this(classId, default, spawnPosition, rotation, scale, onSpawned) { }
195+
173196
[JsonConstructor]
174-
internal SpawnInfo(string classId, TechType techType, Vector3 spawnPosition, Quaternion rotation, Vector3 scale)
197+
internal SpawnInfo(string classId, TechType techType, Vector3 spawnPosition, Quaternion rotation, Vector3 scale, Action<GameObject> onSpawned)
175198
{
176199
ClassId = classId;
177200
TechType = techType;
@@ -183,6 +206,7 @@ internal SpawnInfo(string classId, TechType techType, Vector3 spawnPosition, Qua
183206
_ => SpawnType.TechType
184207
};
185208
Scale = scale;
209+
OnSpawned = onSpawned;
186210
}
187211

188212
/// <summary>

Nautilus/MonoBehaviours/EntitySpawner.cs

+2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ private IEnumerator SpawnAsync()
6868

6969
obj.SetActive(true);
7070

71+
spawnInfo.OnSpawned?.Invoke(obj);
72+
7173
LargeWorldEntity.Register(obj);
7274

7375
LargeWorldStreamerPatcher.SavedSpawnInfos.Add(spawnInfo);

0 commit comments

Comments
 (0)