# \[Extension\] arcade-state-transitions

**URL:** <https://forum.makecode.com/t/extension-arcade-state-transitions/42480>\
**Category:** Arcade\
**Tags:** extension\
**Created:** [March 3, 2026, 8:44pm UTC](https://forum.makecode.com/t/extension-arcade-state-transitions/42480 "2026-03-03T20:44:07Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [March 3, 2026, 8:44pm UTC](https://forum.makecode.com/t/extension-arcade-state-transitions/42480/1 "2026-03-03T20:44:07Z")

</div>

Hey folks!

Today I’m posting about an extension that I actually made years ago, but apparently never got around to posting despite the fact that I use it all the time on livestream. In fact, this might be my favorite extension that I’ve ever written. I present: [riknoll/arcade-state-transitions](https://github.com/riknoll/arcade-state-transitions)

> **[GitHub - riknoll/arcade-state-transitions: A minimal state machine extension for Microsoft...](https://github.com/riknoll/arcade-state-transitions)**
>
> A minimal state machine extension for Microsoft MakeCode Arcade

This is a minimal state machine extension for Microsoft MakeCode Arcade. All the blocks have documentation (right click on a block and select “Help” to read more info on how to use it).

This extension is divided into two parts: **global** state and **sprite** state.

### Global state

A lot of times when you are making a game, you’ll have multiple “states” that your game is moving through.

For example, a turn-based RPG you might have:

- A state where you are walking around in an overworld
- A state when the player is in a battle
- A state when the pause menu is open
- A state when the player is buying something from the shop

Odds are that in your game, you’re going to have very different code running for each of these states. For example, in the “Battle” state pressing A might cause the player to swing a sword whereas in the “Overworld” state pressing A might be used to talk to an NPC. This extension lets you register separate `on game update`, `on button pressed`, and `forever` event handlers in your game that only run when a specific state is active.

This makes it way easier to organize your code than if you were to use a bunch of variables to keep track of what state you’re currently in.

### Sprite state

…but that’s not all! Sprites will often have their own state as well.

For example, let’s consider a guard in a stealth game that’s walking around with a flashlight. If we were to break down the states that this guard might have, it could look like this:

- A “normal” state where the guard is walking around on their patrol route
- An “alert” state where the guard has heard the player’s footsteps and is getting suspicious
- A “chasing” state where the guard has seen the player and is chasing after them

Just like with the global state, you’ll probably want very different code to run when the guard is in each of these states.

This extension makes it super easy to do so! Not only that, but each of the events is nestable so you can put all of logic for a sprite in a single function.

### See also

This extension pairs super well with [riknoll/arcade-improved-sprite-data](https://github.com/riknoll/arcade-improved-sprite-data) and [riknoll/arcade-multi-events](https://github.com/riknoll/arcade-multi-events). Try using all three of them together, especially if you’re making a very big game.

---

<div class="post-metadata">

**Author:** ![babol](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/babol/32/27355_2.png) [@babol](https://forum.makecode.com/u/babol)\
**Post date:** [March 3, 2026, 9:07pm UTC](https://forum.makecode.com/t/extension-arcade-state-transitions/42480/2 "2026-03-03T21:07:17Z")

</div>

huh. isn’t it super simple to make a state machine? you just make a long switch statement and run a function depending on the state variable, or if you can

```auto
state = state_normal

state()

```

and run the state variable like a function. well, unless this has state initialization and such too. i wouldn’t consider that too hard to implement either but if so, i guess i can see why this’ exist.

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [March 3, 2026, 9:11pm UTC](https://forum.makecode.com/t/extension-arcade-state-transitions/42480/3 "2026-03-03T21:11:32Z")

</div>

@babol this does indeed feature all of the state management you could ever want. You can run code when you enter a state, exit a state, each frame while a state is active, etc. You can even listen to state transitions and hijack them if necessary or register multiple event handlers with different weights that run in the background.

and while it may be easy to implement that for global state, you’re missing that this extension also allows you to register events that run only for a single sprite. in effect, each sprite can have its own `on game update`, `on button pressed`, etc. that will only run for the sprite on which they are registered. this makes it easy to, say, define the entire AI of a sprite in a single function instead of having to reference it all over your codebase. or even split your AI behaviors into separate functions and then mix-and-match them for each sprite kind.

---

<div class="post-metadata">

**Author:** ![Oblivious](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/oblivious/32/30718_2.png) [@Oblivious](https://forum.makecode.com/u/Oblivious)\
**Post date:** [March 3, 2026, 9:34pm UTC](https://forum.makecode.com/t/extension-arcade-state-transitions/42480/4 "2026-03-03T21:34:35Z")

</div>

Woah! This will 100% clean up my usage of variables and “If true” blocks for multiple button uses. Thanks for sharing this! 🥹

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [March 3, 2026, 9:56pm UTC](https://forum.makecode.com/t/extension-arcade-state-transitions/42480/5 "2026-03-03T21:56:18Z")

</div>

@babol to expand on that mix-and-match example I gave, one cool way to use this extension is to create functions that let you reuse AI between multiple types of sprites

for example, say i have three types of sprites in my game:

- An NPC that just wanders around randomly
- An enemy that wanders around randomly and attacks the player
- An enemy that follows the walls of the tilemap and attacks the player

if we break down the behavior of each sprite, it looks like this:

- the NPC has the behavior “wander randomly”
- the first enemy has two behaviors “wander randomly” and “attack player”
- the second enemy has two behaviors “follow wall” and “attack player”

in our game, we can define three functions: `addRandomWanderingBehavior`, `addWallFollowingBehavior`, and `addProjectileAttackBehavior`. then when we spawn each sprite, we can just reuse these functions like this:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/6/6/66a64a875be1768f4df0934ec72aa5cb15ddb327.png)

now we have a nice compact way to share code between each sprite without having to reference a bunch of sprite kinds and variables in some global on game update!

here’s the full code of that example:

> **[behavior-adding-example](https://arcade.makecode.com/94826-58224-38987-11344)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

---

<div class="post-metadata">

**Author:** ![randomuser](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/randomuser/32/32505_2.png) [@randomuser](https://forum.makecode.com/u/randomuser)\
**Post date:** [March 3, 2026, 10:50pm UTC](https://forum.makecode.com/t/extension-arcade-state-transitions/42480/6 "2026-03-03T22:50:49Z")

</div>

THANK YOU!! Check just about any game on my profile and you’ll see a clunky ‘state’ variable and long list of ‘if’ statements doing this exact thing… This is now a must-have, I’d wholeheartedly support adding it to the featured extensions list within makecode (I’m not sure the real name)!

---

<div class="post-metadata">

**Author:** ![RoboVacDev](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/robovacdev/32/29619_2.png) [@RoboVacDev](https://forum.makecode.com/u/RoboVacDev)\
**Post date:** [March 3, 2026, 10:50pm UTC](https://forum.makecode.com/t/extension-arcade-state-transitions/42480/7 "2026-03-03T22:50:52Z")

</div>

This might just replace some variables and true/false statements. Thanks bro!

---

<div class="post-metadata">

**Author:** ![TheEarth](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/theearth/32/34579_2.png) [@TheEarth](https://forum.makecode.com/u/TheEarth)\
**Post date:** [August 24, 2026, 5:25am UTC](https://forum.makecode.com/t/extension-arcade-state-transitions/42480/8 "2026-08-24T05:25:28Z")

</div>

I know this is reviving a topic but the help documentation doesn’t seem to help me [https://makecode.com/\_M1ULtrMYeD7L](https://makecode.com/_M1ULtrMYeD7L)

In this game I was making the boss try to use the on state event for the boss to trigger generate attack it enters the attack state but doesn’t work could you explain how I would make it work

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [August 24, 2026, 5:12pm UTC](https://forum.makecode.com/t/extension-arcade-state-transitions/42480/9 "2026-08-24T17:12:29Z")

</div>

@TheEarth you’re changing the state before you register the handler! switch these two blocks:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/a/d/ad1655a14a4cb12ebe0f0838254ccf09d7d8a6c6.png)

---

<div class="post-metadata">

**Author:** ![TheEarth](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/theearth/32/34579_2.png) [@TheEarth](https://forum.makecode.com/u/TheEarth)\
**Post date:** [August 24, 2026, 9:51pm UTC](https://forum.makecode.com/t/extension-arcade-state-transitions/42480/10 "2026-08-24T21:51:21Z")

</div>

One last question if I apply the same thing for a listener it doesn’t seem to work

> **[Anted](https://arcade.makecode.com/98664-59872-43975-30284)**
>
> A roguelike ant game

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [August 24, 2026, 9:54pm UTC](https://forum.makecode.com/t/extension-arcade-state-transitions/42480/11 "2026-08-24T21:54:45Z")

</div>

@TheEarth it’s a problem with your ordering again:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/7/d/7d1f84dce917a65f467f038db35b5d17806a635d.png)

the red arrow shows the order the code is executed in here. changing the state to battle causes the battle code to run immediately, which means the antlion’s state is changed to Attacking before the Attacking listener is registered

---

<div class="post-metadata">

**Author:** ![TheEarth](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/theearth/32/34579_2.png) [@TheEarth](https://forum.makecode.com/u/TheEarth)\
**Post date:** [August 24, 2026, 11:03pm UTC](https://forum.makecode.com/t/extension-arcade-state-transitions/42480/12 "2026-08-24T23:03:57Z")

</div>

ohok I might be a little slow I should have figured that out
