Skip to content
Bit Quirky Round Trip
Visual Scripting

Search documentation

All topics and APIs

Close returns focus to Search.

What are you looking for?

Try a common topic or a C# feature.

Browse the reference instead

Esc closes · Tab reaches results · Enter opens the focused link

v1.0.0
Menu

Loops

Repeat work with a simple From to count, then explore the advanced C-Style view for general C# for-loop headers.

Want a row of ten coins? Start with for in its From to view: choose where to start counting, where to stop, and how much to move the count each time. Connect the work once; the loop repeats it. You do not need to write a C# expression.

Round Trip makes this common case simple on purpose. The advanced C-Style view keeps the flexibility of a C# for loop for cases that are not a simple count, without making everyone work with that extra detail. They are two views of the same kind of loop, not two programming languages. From to is the place to start.

Jump to: Build a counted loop · Advanced: C-Style · Other loop shapes · C# reference.

In Unity, open Tools > Round Trip > Graph view. Press Cmd/Ctrl+O, choose FlowExamples from the Platformer sample, then choose SpawnCoinRow under Methods in the left rail.

Start with a simple count

Select a screenshot to enlarge it.

FlowExamples.SpawnCoinRow selected in Graph view, with the counted FOR node between its entry and SpawnCoin, and Loop settings on the right.
Figure 1. Find the loop and its details The left rail selects the method, the middle shows what repeats, and the right rail describes the selected yellow node. This is SpawnCoinRow in a disposable copy of the Platformer. It is not a new blank method.
Selected FOR node titled i. Its first index is 0, its last index is wired and its step is 1. Its outputs are the loop body, the completed path and the index.
Figure 2. Read the count without reading code Start at 0 and move by 1. The wire supplies the stopping boundary from count. Loop body runs SpawnCoin; index gives it the current position. Completed is a separate route for work after the loop.
The selected loop inspector has Loop type From to, Inclusive false, first index 0, last index from SpawnCoinRow.count, and step 1.
Figure 3. Keep the everyday controls Loop type is From to and Inclusive is off. The Inputs section also shows where each value comes from; you do not need to understand the smaller C# expressions to set the count.
The same inspector with Inclusive true and a Previewing your changes banner above the For loop card.
Figure 4. Recognize a behavior change even though the view looks the same Turning Inclusive on creates a draft. With count 10 and step 1, it would also visit position 10. The preview banner means the change is not saved yet. This demonstration was undone before the advanced comparison.
The same selected loop now says FOR INCLUSIVE in its yellow header; its first index, last index and step pins remain.
Figure 5. See the boundary choice on the canvas INCLUSIVE marks the same unsaved draft on the node. For a collection count, leave this off: the item at the count itself is past the end.
Control Plain-language meaning
first index The number to start at. Use 0 for the first position in a list.
last index The boundary to stop at. A wire can supply a value such as count or collision.contactCount; it need not be a typed number.
step How far the counter moves on each pass. 1 visits each position.
index output The current number to give to the work inside the loop.
Loop body output The work to repeat. White Exec wires say what runs next.
Completed output What runs once after the loop finishes.

Select the yellow node, then find Loop in the right-hand details rail.

Build the simple counted loop first. Start in a method whose canvas holds only its magenta entry node, titled with the method’s name. The steps below use FlowExamples.SpawnCoinRow as the finished comparison.

Build a counted for loop

  1. Place a for loop

    Right-click empty canvas to the right of the entry node. In All actions, pick for under flow control, or type for in Search and pick the for row.

    Clip: the pointer right-clicks empty canvas to the right of the entry node; the all actions menu opens, and typing for narrows the list. Clicking the for row lands a yellow FOR node, in From To, with first index 0, last index 10 and step 1, unwired. While the pointer picks it, the for row of the menu is highlighted, with an arrow pointing at it.
  2. Wire it after the entry node

    Drag from the entry node’s white Exec output to the loop’s Exec input.

    Clip: the pointer drags a white wire from the entry node's exec output pin onto the FOR node's exec input pin; a white wire joins them. While the pointer drags, the entry node's exec output and the loop's exec input are highlighted, with an arrow pointing at them.
  3. Set the range

    Set the range. Type into first index, last index or step, or wire a number into a range pin.

    The three range boxes on the FOR node are highlighted, with an arrow pointing at them. Last index holds a typed 5, replacing its default 10; first index stays 0 and step stays 1.
  4. Place the repeated work

    Place the work that repeats, then wire Loop body to its Exec input. Chain any other repeated work after it.

    Clip: the pointer drags a white wire from the FOR node's loop body output pin onto empty canvas; the statement menu opens there, and typing SpawnCoin and clicking its row lands a SpawnCoin call at the drop point, wired from loop body. The loop body output, then the SpawnCoin row of the menu, are highlighted in turn, each with an arrow pointing at it.
  5. Feed the current position

    Wire index to the input that needs the current number. If something should happen once afterwards, wire Completed to it.

    Clip: the pointer drags a green wire from the FOR node's index output pin to SpawnCoin's index input pin; the wire joins them and the hollow pin fills in. The loop's index output, then SpawnCoin's index input, are highlighted in turn, each with an arrow pointing at it.
    The FOR node's completed output pin, highlighted with an arrow pointing at it, stays hollow; nothing follows it, while loop body and index both reach SpawnCoin.
  6. Check the loop's details

    Select the loop. In the right rail’s Loop section, keep Loop type on From to and check Inclusive against the boundary you intended.

    The FOR node selected; the right rail's Loop section reads Loop type From To and Inclusive off, highlighted, with first index 0, last index 5 and step 1 below.
  7. Compile

    Press Compile in the top toolbar. Then check that the loop covers the range, repeats the work, and continues to the right place afterward.

    The whole Graph view window before Compile, showing this page's NewMethod draft: the entry node, a FOR node and a SpawnCoin call on one row, with the Previewing your changes notice at the top of the right rail. The gold Compile button in the top toolbar is highlighted, with an arrow pointing at it.
    After pressing Compile, the canvas reloads from the written class: the entry node leads into the FOR node, whose loop body reaches a SpawnCoin call and whose index output feeds the call's index pin.

A simple count is the most common use, but C# for loops can do more: set up several variables, test a compound condition, use a calculated update, or omit a header clause. Forcing all of those into first index / last index / step would either lose their meaning or make the everyday counting node harder to use.

From To keeps the common case easy; C-Style preserves the more general C# structure. The name refers to the three-part loop notation shared with C, not to a change of language: Round Trip still reads and writes C#. This design avoids restricting loops to a simplified range; it is not a promise that every other C# feature is editable. See supported C# for that boundary.

C-Style part When it runs What it means
initializer Once, before the first check Set up the loop’s starting state. Declared variables and their starting-value inputs are shown where the header has them.
Condition Before each pass A true/false test: repeat only while it is true. An omitted condition has no condition pin.
iterator After each completed pass, including a continue Update the loop’s state before testing again. Follow this exec output to the update nodes.
Loop body / Completed Repeated work / after the loop The same roles as in From To.

For example, “place coins while there are positions left and the row is not full” combines a numeric boundary with an additional check. The condition requires both checks to pass. A break inside a simple From To body is also a valid way to stop early; you do not have to use C-Style just because the body makes a decision.

  1. Open the sample’s FlowExamples.SpawnCoinRow and select its yellow FOR node.
  2. In the right rail, open Loop > Loop type and choose C-Style.
  3. Choose From to to return to the compact count.

Advanced: inspect the same count as C-Style

Select a screenshot to enlarge it.

The real Loop type dropdown for SpawnCoinRow shows From to and C-Style.
Figure 6. Choose the advanced view This source loop is a recognized range, so both choices are available. Select C-Style only when you want to inspect the separate setup, test and update parts.
SpawnCoinRow in C-Style with its original SpawnCoin body plus LESS THAN and POST INCREMENT nodes.
Figure 7. Keep the same work; expose the loop mechanics This is the same SpawnCoinRow. It is not a different algorithm. The nodes were moved apart for readability. LESS THAN supplies the condition; iterator reaches POST INCREMENT; loop body still reaches SpawnCoin.
The FOR node in C-Style has initial i set to 0 and a red condition input, with initializer, iterator, loop body, completed and index outputs.
Figure 8. Recognize the advanced pins Initial i is the starting value. Condition decides whether to repeat. Initializer and iterator separate setup and update work from the repeated body. Initializer is unwired here because the starting value is already supplied by initial i.
A LESS THAN node receives two green number wires and sends a red result toward the loop condition.
Figure 9. Read the test: is index still below count? The loop index feeds left; count feeds right. The red result is the true/false answer that lets the next pass begin. From to expresses this same test through last index and Inclusive.
POST INCREMENT receives a white exec wire from iterator and the green index value on operand.
Figure 10. Read the update: advance the counter After a body pass, iterator reaches POST INCREMENT. This is i++: increase the counter by one before the next condition check. It is not another call on loop body.
Loop type is C-Style and Inputs lists initial i as 0 and condition as i less than count; there is no Inclusive control.
Figure 11. Compare the inspector, then switch back if you prefer The inspector now shows the initial value and condition instead of the three range inputs. Inclusive is specific to From to. Returning this unchanged range to From to restores its range pins and bound wire.

Not every loop offers both choices. Round Trip offers From to when it recognizes one counter moving by a fixed step toward a bound, including bounds supplied by values. A more general source loop offers only C-Style: inventing a range for it would misrepresent the code.

If a switch would lose a value or wire you authored on a mode-specific pin, Round Trip refuses it with Can’t change for loop type and a reason. Keep the current view, or deliberately undo the affected edit before switching; do not disconnect useful work just to make a screenshot match.

Finish an Inclusive edit before comparing views. Switching to C-Style cancels a pending From to Inclusive change and returns to the source’s comparison. Compile the intended behavior first, or undo that draft before exploring the other view.

This separate documentation variation, FlowExamples.SpawnCoinRowUntilFull, adds “and the row is not full” to the condition. It was captured in an isolated Platformer copy; it is not an extra method shipped in the sample. The actual selector offers only C-Style, and the Inputs section spells out the compound test.

SpawnCoinRowUntilFull inspector: the open Loop type menu contains only C-Style, and condition reads i less than count and not RowIsFull of i.
Figure 12. SpawnCoinRowUntilFull inspector: the open Loop type menu contains only C-Style, and condition reads i less than count and not RowIsFull of i.
C# example: the three header parts and the compound condition

The parts between the parentheses are initializer int i = 0, Condition i < count && !RowIsFull(i), and iterator i++. && means both conditions must hold; ! means “not.” The body runs only while there are positions left and the row is not full. Round Trip preserves this header rather than pretending the loop has just a numeric end boundary.

Use for when you need a count or a position number. The flow control action menu also offers:

  • foreach: visit each item in a list or array. Wire the collection into array and use Array element for the current item.
  • while: repeat while a true/false Condition holds, checking before every pass. It can run zero times.
  • do while: do the work once, then check Condition before repeating. It always runs at least once, even when the initial condition is false.

For while and do while, wire a Boolean result into the red Condition pin. A new unwired, unticked condition means false: while skips its body, whereas do while still runs its first pass.

Inside any loop body, break leaves the innermost loop and continues from Completed; continue skips the rest of that pass and moves to the next one. EXIT ends the whole method instead. FlowExamples.FillCoinRow and CollectRemaining show break and continue respectively.

Compare these separate FlowExamples methods; the slides are alternatives, not successive edits. The counted for and break examples sit beside their C# in What it writes.

Compare loop shapes

Select a screenshot to enlarge it.

CollectAll wires coins into FOREACH array and the current element into Collect target.
Figure 14. Visit each item: CollectAll Follow loop body to Collect. The purple array element wire supplies the current coin; completed is separate from the repeated work.
AwardPendingCoins tests pending coins with GREATER THAN, then repeats AwardPendingCoin before continuing to RefreshHud.
Figure 15. Test before repeating: AwardPendingCoins The red comparison enters condition. AwardPendingCoin hangs from loop body, while RefreshHud hangs from completed and runs after the loop.
PickSpawnPoint wires loop body through RollSpawnPoint and SpawnPointBlocked, whose bool result returns to condition.
Figure 16. Do the work before testing: PickSpawnPoint RollSpawnPoint runs before SpawnPointBlocked supplies the next condition. This is a do-while. It is not a while that might skip its first pass.
CollectRemaining branches on the current coin IsCollected value, reaching CONTINUE on true or Collect on false.
Figure 17. Skip one item: CollectRemaining Already-collected coins take true to CONTINUE. False reaches Collect for this item; the next pass still belongs to the FOREACH.

If you also work in C#, this is what lands in the script. FlowExamples.SpawnCoinRow and FlowExamples.FillCoinRow are written as:

The nodes reached by Loop body become the block inside the loop; Completed leads to the statement after it. A break node writes break; and a continue node writes continue;. Both From to and C-Style write a C# for statement. The view choice itself is layout state. It is not a second implementation of the loop.

When it goes wrong

SymptomCheckFix
Work that should run once runs on every pass.Follow its white wire back. It probably starts at loop body.Drag the wire end off loop body and onto completed.
A loop node shows a condition pin instead of last index.Select the loop and read Loop > Loop type in the right rail.Choose From to if offered and you want the compact count. C-Style is the advanced view. It is not an error; a source loop that is not a recognized range offers only C-Style.
A while loop never runs.Look at its condition pin. An unticked checkbox with no wire means false; a gray dash means no value has been given yet.Wire a red value into condition, or tick the box if you mean to stop the loop with a break inside it.
A break or continue affects the wrong loop.Follow its white wire back to see which loop body it hangs from.Rewire it onto the body of the loop you meant.
  • Branch: decide inside the body whether to act on this pass.
  • Switch: send one value down one of several paths.
  • Scan a list: build the contact scan step by step.
Full-size image