fix/terraria 1.4.5.7 port - #283
Conversation
42dec5f to
240129d
Compare
| static void OnCreate(object sender, Hooks.NPC.CreateEventArgs e) | ||
| { | ||
| // OTAPI's fallback returns a detached NPC; this one lands in the slot with a fresh generation. | ||
| if (e.Npc == null && e.Slot >= 0 && e.Slot < Main.npc.Length) | ||
| e.Npc = NPC.NewNPCInstanceInSlot(e.Slot, (byte)e.Generation); | ||
| } | ||
|
|
There was a problem hiding this comment.
this can be removed - otapi 3.3.13 fixes this internally.
|
Hi @SignatureBeef, I'm the author of #282, which overlaps heavily with this PR. I'd like to fold my changes into #283 so we don't keep two parallel 1.4.5.7 ports open. What I've verified so far with OTAPI.Upcoming 3.3.13 (the fix for the NPC spawning issue):
Since 3.3.13 fixes the NPC create path internally, the How would you prefer to proceed?
I'll close #282 either way. Happy to help with whatever shape you prefer. |
3.3.13 restores the NPC create fallback internally, so the OnCreate workaround this branch added is no longer needed.
240129d to
54fdfaf
Compare
|
@hufang360 option 1 works for me, and it's done — #283 is now your commit plus a one-line bump to 3.3.13, with the Thanks for verifying 3.3.13. TShock side is Pryaxis/TShock#3301, also bumped to 3.3.13. |
Stacked on #282 (@hufang360) — that PR gets 1.4.5.7 building, this makes NPCs actually spawn. Reduces to one commit once #282 merges.
Bug (from #281): server boots and clients connect, but no enemies ever spawn. Town NPCs are fine.
Cause: OTAPI 3.3.12 changed Hooks.NPC.InvokeCreate to take the slot and generation, so the hook is now responsible for placing the NPC in Main.npc. 3.3.11 did that inside NewNPC (Main.npc[index] = nPC; nPC.whoAmI = index; nPC.ResetForNewNPC();). 3.3.12 dropped those lines but its fallback still returns a bare new NPC(). Nothing subscribes to Hooks.NPC.Create, so every spawn applied SetDefaults/active/timeLeft to a detached instance that was never stored Main.npc[slot] stayed empty and GetAvailableNPCSlot handed back the same slot forever.
Fix: subscribe to Hooks.NPC.Create and use the engine's own factory NPC.NewNPCInstanceInSlot(slot, generation) — it stores the instance, sets whoAmI, bumps the generation clients use for recycled slots, and clears stale cross-references via ResetNPCSlotData. OTAPI should arguably fix its fallback too; this makes TSAPI correct either way. Tested: .NET 9, linux-arm64, vanilla 1.4.5.7 client. Before: zero non-town NPCs, NewNPC called repeatedly always returning the same slot. After: enemies spawn, despawn and sync normally.
Thanks @hufang360 for #282, @ZakFahey for the TShock-side port in Pryaxis/TShock#3299, and @bryldd for digging into the PR before.