The SMBX Spawn Algorithm

Share guides you have written for SMBX, and related tools here.
Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9722
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

The SMBX Spawn Algorithm

Postby Emral » 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.

Layers
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.

Spawn-chaining
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:
Image
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.
Image
So a structure like this will spawn all of these NPCs.
Spoiler: show
Image
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
Image
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...
Image
We can basically spawn anything for 3600 frames a.k.a. 56 seconds from anywhere.
Spoiler: show
Image
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
Image
Image

Archived
Van De Graf
Van De Graf
Posts: 2166
Joined: Tue Mar 13, 2018 6:15 pm

Re: The SMBX Spawn Algorithm

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

Archived
Last edited by Archived on Mon Jan 30, 2023 11:03 pm, edited 1 time in total.

PdizzleParker
Ripper II
Ripper II
Posts: 322
Joined: Fri Aug 10, 2018 11:26 am
Flair: doin your mom
Pronouns: they/them
Contact:

Re: The SMBX Spawn Algorithm

Postby PdizzleParker » Sun Aug 16, 2020 12:34 am

While using Spawnzones is obviously more efficient, I honestly think it's really cool that vanilla SMBX accounts for this as well. I had no idea that it did this.

RudeGuy
Fry Guy
Fry Guy
Posts: 4993
Joined: Fri Dec 27, 2013 7:36 am
Flair: local guy

Re: The SMBX Spawn Algorithm

Postby RudeGuy » Mon Aug 17, 2020 7:59 am

The spawn chaining thing actually sounds really cool. I wonder if you could use it as a level gimmick?

MECHDRAGON777
Pink Yoshi Egg
Pink Yoshi Egg
Posts: 6422
Joined: Fri Dec 20, 2013 6:40 pm
Flair: Nuclear Queen of Reversion.
Contact:

Re: The SMBX Spawn Algorithm

Postby MECHDRAGON777 » Sun Aug 23, 2020 3:11 am

I actually used this trick in "Carrot Frenzy" back in July. It is quite useful.

Archived
Van De Graf
Van De Graf
Posts: 2166
Joined: Tue Mar 13, 2018 6:15 pm

Re: The SMBX Spawn Algorithm

Postby Archived » Mon Aug 24, 2020 8:33 pm

Archived
Last edited by Archived on Mon Jan 30, 2023 11:06 pm, edited 1 time in total.

MECHDRAGON777
Pink Yoshi Egg
Pink Yoshi Egg
Posts: 6422
Joined: Fri Dec 20, 2013 6:40 pm
Flair: Nuclear Queen of Reversion.
Contact:

Re: The SMBX Spawn Algorithm

Postby MECHDRAGON777 » Mon Aug 24, 2020 11:03 pm

Eclipsed wrote:
Mon Aug 24, 2020 8:33 pm
MECHDRAGON777 wrote:
Sun Aug 23, 2020 3:11 am
I actually used this trick in "Carrot Frenzy" back in July. It is quite useful.
what did you use it for?
I used it to get the King Bills to spawn a bit further back.

litchh
Eerie
Eerie
Posts: 716
Joined: Sun Dec 29, 2013 6:10 am

Re: The SMBX Spawn Algorithm

Postby litchh » Sat Sep 12, 2020 5:37 am

Enjl wrote:
Thu Jul 23, 2020 5:41 pm
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.
Boom Boom & Birdo can despawn if "Legacy" flag is not active for them.


Return to “Guides”

Who is online

Users browsing this forum: No registered users and 4 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari