The SMBX Spawn Algorithm

Share guides you have written for SMBX, and related tools here.
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 8541
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Orphion Egamalenitar Osmos IV, Esq.

The SMBX Spawn Algorithm

Postby Enjl » Thu Jul 23, 2020 5:41 pm

Historically, the spawn algorithm in SMBX has been a royal pain to work with. Fundamentally it seems like something that just checks when an NPC comes onscreen and then despawns it a few seconds after it leaves it, but there's a lot of nuance to this system. I dove into the source code a little and found some interesting tidbits:

NPCs cannot respawn on the frame they despawn
The countdown timer ticks down every frame the NPC is offscreen. Once it reaches 0, the NPC despawns, and only once it reaches -1 can the NPC respawn. This causes a frame-perfect glitch where NPCs despawn on the frame they would come onscreen.

Default timers
Note: A second is approximately 64.12 frames. All numbers provided are in frames.
Most npcs spawn at: 180 frames
Generated NPCs: 100 frames
Conveyor Belt (57): 100 frames
Upon spawning: Wart Bubbles (202), Hammers (30), and Rinkas (211): 50 frames
Reserve item: 200 frames
Yoshis, Boots, All powerups & 1-ups, Billy Gun (22), Poison Mushroom (153), 3-up moon (188), SMB3 Wood Platform (104) and the flipped Rainbow Shell (195): 3600 frames (180 * 20)

Special Cases
Some NPCs do stuff when they spawn. Here's a brief list:
The Goal Tape (197) checks for blocks up to 8000 pixels below its top edge to see where the nearest ground is to determine its travel distance. It does this every time it spawns.
The Blaarg (199) will move 36 pixels below its placement position.
The Hammer and Tanooki Suit powerups (169 & 170) will set their frame.
Bullet Bills (17, 18), Cheep Cheeps with AI set to "Projectile", and Eeries are incapable of spawning when the level begins. They will only spawn upon coming onscreen. Additionally, they will only spawn if they face towards the screen.
Bullet Blasters (21) spawn with a firing timer of 100 (ai1).
Bowser Statues (84, 181) randomly set their firing timer to a value between 0 and 200 (ai1).
Legacy Bosses will play their boss music.

Despawn exceptions
Not all NPCs despawn. For various reasons. Here are some exceptions:
Switch platforms (60, 62, 64, 66) have their timer frozen at 100 once spawned.
The Clown Car (56) cannot despawn.
Bullet Bills (17, 18) will despawn after 3 frames of being offscreen.
The Koopalings (267, 268, 280, 281) lock their despawn timers to 180 and cannot despawn.
SMB3 Bowser (86), Boom Boom (15), Lakitu (47, 284), Birdo (39), Roto Discs (259), Vine Heads (225, 226, 227), Wart (201), SMB1 Bowser (200), Mouser (268), Mother Brain (209), Mother Brain Jar (208) and Firebars (260) lock their despawn timers to 100 and cannot despawn.
Held NPCs cannot despawn.
Goals cannot despawn.

No Turn Back
In a No Turn Back section, enemies will be killed permanently when despawning.

Section Switch
When leaving a section, most NPCs instantly despawn. Even those that usually cannot despawn (see previous section). Exceptions:
Fireballs (13), Projectile Cheep Cheeps, Toothie (50), Clown Car (56), Switch Platforms (60, 62, 64, 66), Yoshis, and held NPCs.

NPCs spawn when they come onscreen. Usually this happens when the screen moves. Sometimes this happens when the NPC's layer moves. While inactive, all NPCs will move with their respective layers. Once spawned, only few NPCs retain this property. This trick to spawning NPCs can be useful if you want offscreen enemies to come in at certain intervals toward an unmoving screen.

This is lowkey why I'm making this topic.
In addition to "coming onscreen", there is a second way in which NPCs can spawn: Through being spawned by another NPC. To illustrate:
When an NPC spawns, it checks in an area around it to see if there are any nearby NPCs. The exact measurements of this box are the size of the NPC + 32 pixels in all 4 directions. So one block. If an NPC is found, that one is also set to active. The cool bit is: This system has chain reaction handling.
So a structure like this will spawn all of these NPCs.
Spoiler: show
And it does!
Worth noting however is that there is a limit: The despawn timer. Remember how it counts down by 1 while NPCs are offscreen. On occasion I could only get 8 Goombas to show up instead of all 10, because the other 2 despawned before they reached the screen.
When spawn-chaining, the despawn timer of the chained NPCs are set to the despawn timer of the source NPC, so you can get different results based on the type of NPC at the start of the chain (see default timers section).
Spoiler: show
With Yoshi's despawn timer, it feels like these NPCs never go away!
The following IDs cannot spawn-chain: 46 (Donut Block), 57 (Conveyor Belt), 212 (Other Donut Block), Coins (all types).
I've also gotten inconsistent results with spawn-chaining NPCs like bullet bills, so perhaps some other prior exceptions inadvertently circumvent the spawn-chaining code.

The Activate Event
Upon spawning, an NPC immediately executes its Activate event. Now, why should we care if we have the trigger zone NPC? I raise you this: We should care precisely BECAUSE we have the trigger zone NPC. The trigger zone NPC can spawnchain. So if we attach it to a Moon with an Activate event that hides the moon, and then draw a line to something we want to spawn...
We can basically spawn anything for 3600 frames a.k.a. 56 seconds from anywhere.
Spoiler: show
In the above gif I have additionally set the King Bill's activate event to show the pipe piece. If the chain of trigger zone NPCs were to be hidden after the bill spawns to prevent future remote-spawning, the King Bill should handle that in its activate event. If the moon does it, the chain despawns before it can spawn the King Bill.

That's about all I could find regarding the system. One more thing maybe worth noting: the 180 frames despawn timer is a global variable that appears to have been introduced later on. A lot of NPCs hardcode their timers to 100 still. It seems Redigit began a makeover at some point and never got to complete it, leading to inconsistent results where most spawned NPCs have a shorter lifespan.

Additional note: If this algorithm isn't doing it for you, you can always use spawnzones to make the spawn rules easier to work with.

I'll leave you with this idea for an application of spawn-chaining to spawn in enemies that don't exist within any section:
Spoiler: show
The ABC of Creation

Posts: 1817
Joined: Tue Mar 13, 2018 6:15 pm
Chat Username: Eclipsed

Re: The SMBX Spawn Algorithm

Postby Eclipsed » Wed Aug 05, 2020 1:01 am

Enjl wrote:
Thu Jul 23, 2020 5:41 pm

I'll leave you with this idea for an application of spawn-chaining to spawn in enemies that don't exist within any section:
Spoiler: show
That's really cool that you can get offscreen enemies to spawn through chaining

Return to “Guides”

Who is online

Users browsing this forum: No registered users and 2 guests