@@ -256,26 +256,33 @@ public class BlockScript
256256
257257
258258 /// <summary>
259- /// Gets a block by name (checks NamedBlocks, MainBlock, ConstBlock, PubVarBlock , then LoopBlocks )
260- /// Note : LoopBlocks is checked last because its keys are parent block names (e.g., "MainBlock")
261- /// which would otherwise shadow the actual MainBlock when querying by name.
259+ /// Gets a block by name (checks LoopBlocks first by block name, then NamedBlocks , then standard blocks )
260+ /// IMPORTANT : LoopBlocks are checked FIRST because LoopBlock names (like "LoopBody") should NOT be
261+ /// shadowed by user-defined NamedBlocks with the same name.
262262 /// </summary>
263263 public BlockDefinition ? GetBlockByName ( string name )
264264 {
265- // First check NamedBlocks (user-defined blocks)
265+ // First check LoopBlocks by the block's own name (not parent block name)
266+ // This is critical because LoopBlocks are created for "NextBlock = Loop(...)" statements
267+ // and their names (like "LoopBody") should take precedence over user-defined blocks
268+ foreach ( var kvp in LoopBlocks )
269+ {
270+ if ( kvp . Value . Name == name )
271+ return kvp . Value ;
272+ }
273+
274+ // Then check NamedBlocks (user-defined blocks)
266275 if ( NamedBlocks . TryGetValue ( name , out var namedBlock ) )
267276 return namedBlock ;
277+
268278 // Then check the standard blocks by name match
269279 if ( MainBlock ? . Name == name )
270280 return MainBlock ;
271281 if ( ConstBlock ? . Name == name )
272282 return ConstBlock ;
273283 if ( PubVarBlock ? . Name == name )
274284 return PubVarBlock ;
275- // Finally check LoopBlocks - these are internal and should not shadow standard blocks
276- // LoopBlocks keys are parent block names (e.g., "MainBlock" -> LoopBlock for that parent)
277- if ( LoopBlocks . TryGetValue ( name , out var loopBlock ) )
278- return loopBlock ;
285+
279286 return null ;
280287 }
281288}
@@ -419,3 +426,4 @@ public class BlockExecutionResult
419426 ReturnValue = value
420427 } ;
421428}
429+
0 commit comments