YesNoOk
avatar

Vans Loop (Read 14201 times)

Started by inktrebuchet, April 24, 2019, 05:03:08 pm
Share this topic:
Vans Loop
New #1  April 24, 2019, 05:03:08 pm
  • ****
    • USA
    • twitter.com/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.

If there are questions or other examples you would like to share please leave a comment.

Code:
;===============================================================================
; 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 ;blank
ctrl = 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 = Varset
trigger1 = var(1) = 0
var(1) = 2500 ;max loops allowed in 1 tick.

[State 500001, 1] ;loop counter
type = VarAdd
trigger1 = time = 0
var(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 = Explod
trigger1 = time = 0
ID = 10000
anim = 40
pos = var(0)*10, 0 ;<<< explods will be set 10 pixels apart.
removetime = -1

;==================================
;if condition is true, exit loop
;==================================
[State 500001, 3]
type = ChangeState
;condition
trigger1 = numexplod(10000) >= 44 ; once this condition is met the loop ends.                     

;reset loop for next time.
trigger1 = var(1) := 0 || 1
trigger1 = var(0) := 0 || 1

value = 500002 ;end state

;==================================
;if condition is false, loop   
;==================================
[State 500001, 4]
type = ChangeState
trigger1 = time = 0
trigger1 = 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 = ChangeState
trigger1 = time = 1
trigger1 = var(1) := var(1) + 2500  ; increase loop limit.
value = stateno
;---------------------------------------------------------------------------------------------

[Statedef 500002] ;end state

[state 500002, 0]
type = destroyself
trigger1 = time > 20



Last Edit: April 24, 2019, 09:26:14 pm by ink