# Vans Loop (Read 3040 times)

Started by ink, April 24, 2019, 05:03:08 pm

#### ink

##### Vans Loop
New #1  April 24, 2019, 05:03:08 pm
• inktrebuchet
I wanted to document this recursion technique I learned from Vans. He first used it 7-8 years ago to program an alphabet system after noticing memory utilization taking a hit if he chucked a bunch of sctrls in a single state.

You have probably seen or heard about this error message from mugen "State machine stuck in loop (stopped after 2500 loops)", this loop technique looks at that further and takes advantage of it. Mugen allows a state to loop 2500 times in 1 tick before closing with that error message so if a counter is added to the state, it can keep track and stop the loop before it exceeds 2500 or if the loop need to exceed 2500 it can pause the loop for 1 tick and continue. This technique is great for reducing the amount of sctrls in a single state.

I've added some to this idea and shared a simple helper example below thatâ€™s organized with plenty of notes. There are some extras that aren't needed for this particular example but will help in understanding how the loop works for other applications. This code spawns 44 explods, 10 pixels apart from each other in 1 tick, but could spawn 2500 in 1 tick or 5000 in 2 ticks if the explod limit allowed it.

Without using this technique 44 separate explod sctrls would need to be in the state for them all to trigger on the same tick.

`;===============================================================================; Vans loop example;===============================================================================; Variable usage:; This is a record of the variables that are used;; var(0) - loop counter; var(1) - Loop limit to avoid infinite loop crash;===============================================================================[Statedef 500001]anim = 9741 ;blankctrl = 0[State 500001, 0] ;set loop limit. (only needed if loops are to exceed 2500, make changes to "if condition is false, loop" section if not needed.)type = Varsettrigger1 = var(1) = 0var(1) = 2500 ;max loops allowed in 1 tick.[State 500001, 1] ;loop countertype = VarAddtrigger1 = time = 0var(0) = 1;==================================;conditional code;==================================;if you read below in the "if condition is true, exit loop" section you will see this loop only ends after 44 explods are spawned.[State 500001, 2] type = Explodtrigger1 = time = 0ID = 10000 anim = 40pos = var(0)*10, 0 ;<<< explods will be set 10 pixels apart.removetime = -1;==================================;if condition is true, exit loop;==================================[State 500001, 3] type = ChangeState;conditiontrigger1 = numexplod(10000) >= 44 ; once this condition is met the loop ends.                     ;reset loop for next time.trigger1 = var(1) := 0 || 1trigger1 = var(0) := 0 || 1value = 500002 ;end state;==================================;if condition is false, loop   ;==================================[State 500001, 4]type = ChangeStatetrigger1 = time = 0trigger1 = var(0) < var(1) ; var(0) < 2500 ; loops < loop limit value = stateno [State 500001, 5] ;if loop limit was met, wait 1 tick and then raise limit. (only needed if loops are to exceed 2500)type = ChangeStatetrigger1 = time = 1trigger1 = var(1) := var(1) + 2500  ; increase loop limit.value = stateno ;---------------------------------------------------------------------------------------------[Statedef 500002] ;end state[state 500002, 0]type = destroyselftrigger1 = time > 20`