Loop

From mugen-net
Jump to navigation Jump to search

A loop, in traditional computer programming, allows for a sequence of statements to be declared only once, yet those statements may be executed several times in succession.[1] This allows for code reuse, leading to fewer possible sources of bugs.

Limitations[edit]

M.U.G.E.N itself does not provide a straightforward method to users for creating loops, however, they are possible. The only limitation is that the number of iterations cannot exceed 2500, as it relies on ChangeState at Time <= 0 to accomplish the task. 2500 is an arbitrary limit chosen by Elecbyte in order to limit the number of times a user can change into the same state from itself in order to prevent the user from accidentally creating an indefinite loop, which could otherwise freeze execution of the engine, causing one or more players to not respond to any further commands from either the player or the developer.

Usage[edit]

Loops are useful for iterating over data (typically in an array, which is roughly equivalent to the variable list M.U.G.E.N provides developers) and performing some operation on that data one element at a time. In M.U.G.E.N, this can be useful for generating super effects without having to copy and paste code for Explods and Helpers repeatedly.

The following example spawns 16 Explods with a random angle. Vel X is used as a loop variable and terminator.

[Statedef 8500]

[State 8500, VarRandom]
type = VarRandom
trigger1 = !Time
v = 59
range = 0,359

[State 8500, Loop Increment]
type = VelSet
trigger1 = PrevStateNo = StateNo ; If we just changed into this state from the same state.
x = Vel X + 1

[State 8500, VarSet]
type = VarSet
trigger1 = Time = 0
v = floor(Vel X)
value = 6 + ((var(59)*534131) / 224) % 360

[State 8500, SuperPause FX]
type = Explod
trigger1 = Time = 0
anim = 8503
ID = 8501
posType = P1
pos = 0,0
superMoveTime = 255
scale = 0.325,1
angle = var(floor(Vel X))
removetime = 9
sprpriority = 3
ownPal = 1

[State 8500, Loop]
type = ChangeState
trigger1 = floor(Vel X) < 16 ; Must use floor to cast to an Integer
value = stateNo              ; Change back to the same state.
[State 8500, Exit]
type = ChangeState
trigger1 = 1                 ; Trigger doesn't matter since anything outside of the Loop will be executed from this point forward.
value = stateNo+1            ; Change to next state

Any code block executed within the loop will almost always execute in 1 tick or less, meaning a full iteration over many data sets is possible, so long as the iteration does not exceed 2500 loops.

References[edit]

Template:Reflist